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在我们平时的 ...
随机推荐
- CentOS7.5安装Mysql5.7.22
一 下载 下载地址:https://www.mysql.com/downloads/ 1 点击第一个链接地址,进入MySQL官方网站,单击“Downloads”下载Tab页,进入下载界面 2 找到Co ...
- day4迭代器&生成器&正则表达式
一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不能后退,不过这也没什么,因为人们很少在迭代途中后退.另外,迭代器的一大优点 ...
- 机器寻径引导算法C#(最短路径表)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- 洛谷P2680 运输计划 [LCA,树上差分,二分答案]
题目传送门 运输计划 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n?1 条双向航道,每条航道建立在两个星球之间, 这 n?1 条航道连通了 L 国的所 ...
- 洛谷P3434 [POI2006]KRA-The Disks [模拟]
题目传送门 KRA 题目描述 For his birthday present little Johnny has received from his parents a new plaything ...
- 【转】全面了解Mysql中的事务
为什么要有事务? 事务广泛的运用于订单系统.银行系统等多种场景.如果有以下一个场景:A用户和B用户是银行的储户.现在A要给B转账500元.那么需要做以下几件事: 1. 检查A的账户余额>500元 ...
- mac系统终端下忽略大小写 与 git自动补全(git auto completion)
1.下载git-completion.bash 并放到home 目录下: curl https://raw.githubusercontent.com/git/git/master/contrib/c ...
- [NOI2007]货币兑换 --- DP + 斜率优化(CDQ分治)
[NOI2007]货币兑换 题目描述: 小 Y 最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A 纪念券(以下简称 A 券)和 B 纪念券(以下简称 B 券). 每个持有金券的顾客都有一个 ...
- [BZOJ4240]有趣的家庭菜园(贪心+树状数组)
最后数列一定是单峰的,问题就是最小化最后的位置序列的逆序对数. 从大到小加数,每次贪心看放左边和右边哪个产生的逆序对数更少,树状数组即可. 由于大数放哪对小数不产生影响,所以正确性显然. 注意相同数之 ...
- stl upper_bound()
http://blog.csdn.net/niushuai666/article/details/6734650 upper_bound( a , b , k )返回有序升序序列[a,b)中能放下 ...