Drawable的getIntrinsicHeight()和getIntrinsicWidth()
版权声明:本文为博主原创文章,未经博主允许不得转载。
今天遇到一个问题,一个Bitmap封装到BitmapDrawable中 ,BitmapDrawable drawable = new BitmapDrawable(bmp),
Bitmap.getWidth() != BitmapDrawable.getIntrinsicWidth().导致一些问题:
查看源代码,问题如下:
在BitmapDrawable中,给mBitmapWidth赋值时,要根据density缩放,其默认值是160,mdpi的情况:
mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
而在Bitmap的density是240情况下,将缩放:
公式约等于为:drawableDensity * bmpWidth / bmpDensity ======>> 160 * 72 / 240 ,所以getIntrinsicHeight()为48
在BitmapDrawable中:
- private void computeBitmapSize() {
- mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
- mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
- }
private void computeBitmapSize() {
mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
}
- @Override
- public int getIntrinsicWidth() {
- return mBitmapWidth;
- }
- @Override
- public int getIntrinsicHeight() {
- return mBitmapHeight;
- }
@Override
public int getIntrinsicWidth() {
return mBitmapWidth;
} @Override
public int getIntrinsicHeight() {
return mBitmapHeight;
}
- private BitmapDrawable(BitmapState state, Resources res) {
- mBitmapState = state;
- if (res != null) {
- mTargetDensity = res.getDisplayMetrics().densityDpi;
- } else if (state != null) {
- mTargetDensity = state.mTargetDensity;
- } else {
- mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
- }
- setBitmap(state.mBitmap);
- }
private BitmapDrawable(BitmapState state, Resources res) {
mBitmapState = state;
if (res != null) {
mTargetDensity = res.getDisplayMetrics().densityDpi;
} else if (state != null) {
mTargetDensity = state.mTargetDensity;
} else {
mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
}
setBitmap(state.mBitmap);
}
在ButtonState中,mTargetDensity的值默认为:
int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
注意:res == null时,且state != null时,mTargetDensity = state.mTargetDensity;
- /**
- * Create an empty drawable, setting initial target density based on
- * the display metrics of the resources.
- */
- public BitmapDrawable(Resources res) {
- mBitmapState = new BitmapState((Bitmap) null);
- mBitmapState.mTargetDensity = mTargetDensity;
- }
- /**
- * Create drawable from a bitmap, not dealing with density.
- * @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure
- * that the drawable has correctly set its target density.
- */
- @Deprecated
- public BitmapDrawable(Bitmap bitmap) {
- this(new BitmapState(bitmap), null);
- }
/**
* Create an empty drawable, setting initial target density based on
* the display metrics of the resources.
*/
public BitmapDrawable(Resources res) {
mBitmapState = new BitmapState((Bitmap) null);
mBitmapState.mTargetDensity = mTargetDensity;
} /**
* Create drawable from a bitmap, not dealing with density.
* @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure
* that the drawable has correctly set its target density.
*/
@Deprecated
public BitmapDrawable(Bitmap bitmap) {
this(new BitmapState(bitmap), null);
}
- /**
- * Create drawable from a bitmap, setting initial target density based on
- * the display metrics of the resources.
- */
- public BitmapDrawable(Resources res, Bitmap bitmap) {
- this(new BitmapState(bitmap), res);
- mBitmapState.mTargetDensity = mTargetDensity;
- }
/**
* Create drawable from a bitmap, setting initial target density based on
* the display metrics of the resources.
*/
public BitmapDrawable(Resources res, Bitmap bitmap) {
this(new BitmapState(bitmap), res);
mBitmapState.mTargetDensity = mTargetDensity;
}
其中,BitmapDrawable(Bitmap bmp)已经被弃用,如果使用 BitmapDrawable(Bitmap bmp,Resources res)构造函数
在DisplayMetrics:
- public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
在Bitmap中:
- /**
- * Convenience method that returns the width of this bitmap divided
- * by the density scale factor.
- *
- * @param targetDensity The density of the target canvas of the bitmap.
- * @return The scaled width of this bitmap, according to the density scale factor.
- */
- public int getScaledWidth(int targetDensity) {
- return scaleFromDensity(getWidth(), mDensity, targetDensity);
- }
- /**
- * Convenience method that returns the height of this bitmap divided
- * by the density scale factor.
- *
- * @param targetDensity The density of the target canvas of the bitmap.
- * @return The scaled height of this bitmap, according to the density scale factor.
- */
- public int getScaledHeight(int targetDensity) {
- return scaleFromDensity(getHeight(), mDensity, targetDensity);
- }
- /**
- * @hide
- */
- static public int scaleFromDensity(int size, int sdensity, int tdensity) {
- if (sdensity == DENSITY_NONE || sdensity == tdensity) {
- return size;
- }
- // Scale by tdensity / sdensity, rounding up.
- return ( (size * tdensity) + (sdensity >> 1) ) / sdensity;
- }
/**
* Convenience method that returns the width of this bitmap divided
* by the density scale factor.
*
* @param targetDensity The density of the target canvas of the bitmap.
* @return The scaled width of this bitmap, according to the density scale factor.
*/
public int getScaledWidth(int targetDensity) {
return scaleFromDensity(getWidth(), mDensity, targetDensity);
} /**
* Convenience method that returns the height of this bitmap divided
* by the density scale factor.
*
* @param targetDensity The density of the target canvas of the bitmap.
* @return The scaled height of this bitmap, according to the density scale factor.
*/
public int getScaledHeight(int targetDensity) {
return scaleFromDensity(getHeight(), mDensity, targetDensity);
} /**
* @hide
*/
static public int scaleFromDensity(int size, int sdensity, int tdensity) {
if (sdensity == DENSITY_NONE || sdensity == tdensity) {
return size;
} // Scale by tdensity / sdensity, rounding up.
return ( (size * tdensity) + (sdensity >> 1) ) / sdensity;
}
如此,只有做如下改动:
方法一:
BitmapDrawable bmpDrawable = new BitmapDrawable(bmp,getResources);
方法二:
BitmapDrawable bmpDrawable = new BitmapDrawable(bmp);
bmpDrawable.setTargetDensity(getResources().getResources().getDisplayMetrics());
借鉴: http://blog.csdn.net/jason_wks/article/details/8283224
Drawable的getIntrinsicHeight()和getIntrinsicWidth()的更多相关文章
- 玩转Android之Drawable的使用
Drawable天天用,可你是否对Drawable家族有一个完整的认知?今天我们就来系统的学习一下Drawable的使用. 1.概述 用过Drawable的筒子都知道Drawable有很多种,有的时候 ...
- android的Drawable详解
Drawable简介 Drawable有很多种,用来表示一种图像的概念,但他们又不完全是图像,他们是用过颜色构建出来的各种图像的表现形式.Drawable一般都是通过xml来定义的 ,当然我们也可以通 ...
- [转]自定义Drawable实现灵动的红鲤鱼动画(上篇)
此篇中的小鱼动画是模仿国外一个大牛做的flash动画,第一眼就爱上它了,简约灵动又不失美学,于是抽空试着尝试了一下,如下是我用Android实现的效果图: 小鱼儿 由于整个绘制分析过程比较繁琐所以 ...
- Android 自定义列表指示器
在联系人界面 可以看到这种界面 手指快速滑动右边滑动条时 可以显示相应的字母图标 android里提供了android.widget.SectionIndexer这个接口去实现该效果 可是只能显示字母 ...
- 星座物语APP
效果图: 这里的后台管理用的是duducat,大家可以去百度看说明.图片,文字都在duducat后台服务器上,可以自己修改的.(PS:图片这里是随便找的) http://www.duducat.com ...
- Android静态图片人脸识别的完整demo(附完整源码)
Demo功能:利用android自带的人脸识别进行识别,标记出眼睛和人脸位置.点击按键后进行人脸识别,完毕后显示到imageview上. 第一部分:布局文件activity_main.xml < ...
- Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
在Android的开发中,凡是需要画图的地方大都离不开类Drawable.Android的官方文档中介绍这个类就是被设计用来表示可以被画的东西.A Drawable is a ge ...
- Drawable: getIntrinsicWidth()和getIntrinsicHeight()方法的使用误区
经常会使用上述两个API来获取ImageView中显示图片的大小,但是在某些情况下,这两个API返回的大小可能与原图的大小不一致,比如原图大小是72*72,分别把原图放置在xhdpi,xxhdpi,x ...
- Android Drawable 那些不为人知的高效用法
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...
随机推荐
- Mysql查询语句的运行流程
我们先看一下MYsql的基本架构示意图: 大体来说,MySQL 可以分为 Server 层和存储引擎层两部分. Server 层包括连接器.查询缓存.分析器.优化器.执行器等,涵盖 MySQL 的大多 ...
- java.lang.reflect.UndeclaredThrowableExceptionjiang
实例包含由调用处理程序抛出的经过检查的未声明异常,可以使用 getUndeclaredThrowable() 方法获取 String msg = null; if (e instanceof Unde ...
- JS模块化规范CMD之SeaJS
1. 在接触规范之前,我们用模块化来封装代码大多为如下: ;(function (形参模块名, 依赖项, 依赖项) { // 通过 形参模块名 修改模块 window.模块名 = 形参模块名 })(w ...
- js学习笔记1:语法、数据类型与转换、运算符与运算
注意: 上部代码错误,将停止运行,下部的代码无法显示 typeof 用来定义内容类型,不会输出内容只会输出类型 一.js输出语法 1. 弹窗输出('')内的内容: ...
- MySQL 中的日期时间类型
日期时间类型中包含以下几种数据类型: DATE TIME DATETIME TIMESTAMP YEAR 各类型都有具体的取值范围,超出或非法的其他值时,MySQL 会回退到 0.TIMESTAMP ...
- thinkphp5.0 配置
ThinkPHP提供了灵活的全局配置功能,采用最有效率的PHP返回数组方式定义,支持惯例配置.公共配置.模块配置.场景配置和动态配置. 对于有些简单的应用,你无需配置任何配置文件,而对于复杂的要求,你 ...
- 转:php防止sql注入的一点心得
转:http://blog.csdn.net/sky_zhe/article/details/9702489 转:http://zhangxugg-163-com.iteye.com/blog/183 ...
- Python编程举例-装饰器
装饰器的通常用途是扩展已定义好的函数的功能 一个浅显的装饰器编程例子 #装饰器函数 def outer(fun): def wrapper(): #添加新的功能 print('验证') fun() r ...
- java -jar demo.jar
部署springboot项目 生成jar包其实还是依赖springboot的jar才能跑起来,为什么呢? 1.在C盘手工创建了一个文件夹,是拷贝了demo.jar这个jar包运行是报错的. 2.在D: ...
- Array.reduce()方法的使用
起因是学习异步函数的串行与并行写法时,发现reduce方法可以简化写法,然后看到一篇博客里面这样一段代码: var array = [1, [2, [3, 4], 5], 6]; function f ...