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在我们平时的 ...
随机推荐
- First Missing Positive——数学类
转:http://blog.csdn.net/nanjunxiao/article/details/12973173 Given an unsorted integer array, find the ...
- fedora安装后的配置
fedora安装后的一些配置 (mirror)源 换源 默认从fedora官网下载太慢,考虑换用国内的源(镜像站点),推荐中科大.阿里云.浙大.网易等的源. 比如我用浙大ZJU的源http://mir ...
- 摄像机distortion vector、project matrix、camera matrix
关于标定后图像如何校正:http://wiki.ros.org/image_pipeline/CameraInfo ros distortion vector 参数顺序:http://docs.ros ...
- 【hdoj_1865】1sting(递推+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1865 本题的关键是找递推关系式,由题目,可知前几个序列的结果,序列长度为n=1,2,3,4,5的结果分别是 ...
- 计算 Python (list or array) 相同索引元素相等的个数
上代码: a = [2, 3, 3, 1, 1, 3, 3, 3, 2, 1, 3, 1, 3, 2, 2, 2, 3, 1, 2, 3, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, ...
- Mysql修改语句的运行流程
执行修改语句前要先连接数据库,这是连接器的工作. 接下来,分析器会通过词法和语法解析知道这是一条更新语句.优化器决定要使用 ID 这个索引.然后,执行器负责具体执行,找到这一行,然后更新. Mysql ...
- [你必须知道的.NET]第二十三回:品味细节,深入.NET的类型构造器
发布日期:2008.11.2 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 今天Artech兄在<关于Type Init ...
- 【PAT】1004. 成绩排名 (20)
1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 ...
- Web前端开发最佳实践(1):前端开发概述
引言 我从07年开始进入博客园,从最开始阅读别人的文章到自己开始尝试表达一些自己对技术的看法.可以说,博客园是我参与技术讨论的一个主要的平台.在这其间,随着接触技术的广度和深度的增加,也写了一些得到了 ...
- kali2.0安装VMware Tools
1.加载VM tools镜像 加载完成出现这个 2.复制vm tools安装包到~目录 cd /media/cdrom -.tar.gz ~ 已经复制到了~目录下 3.解压安装包 cd ~ -.tar ...