Android BitmapDrawable()的使用
来源:互联网 发布:php 截断上传 编辑:程序博客网 时间:2024/05/29 15:37
前段时间项目中碰到一个问题,界面绘制闪了一下,最后分析代码感觉可能是创建BitmapDrawable对象出了问题,然后
查看源码BitmapDrawable.java。BitmapDrawable有若干个构造方法。
New BitmapDrawable(Bitmap bitmap)是早期的一个构造方法,在android 4.0已经过时,部分代码如下:
this(new BitmapState(bitmap), null);
而google提倡使用new BitmapDrawable(Bitmap bitmap,Resources res),其部分代码如下:
this(new BitmapState(bitmap), res);
可见以上两个方法均调用了一个私有的方法: BitmapDrawable(BitmapState state, Resources res),不同的是前者传入一个Resources的NULL值,后者传入了一个非NULL值。在BitmapDrawable(BitmapState state, Resources res)方法中部分代码如下:
if (res != null) {
mTargetDensity = res.getDisplayMetrics().densityDpi;// BitmapDrawable(Bitmap,Resources ) go here
} else {
mTargetDensity = state.mTargetDensity; // BitmapDrawable(Bitmap) go here
}
看来当前BitmapDrawable是保存一个目标密度,这个密度如果传入了Resources对象,会根据Resources确定一个正确的密度(S4为480)。否则会采用BitmapState的目标密度,而它的目标密度会有一个默认值:
int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
再查看DisplayMetrics.java源码,有如下定义:
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;//这个值为240
总上所述,在S4 ,new BitmapDrawable(Bitmap)得到了一个240的目标密度。而new BitmapDrawable(Bitmap bitmap,Resources res)得到一个480的目标密度。当计算Bitmap的宽高有:
mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
总结,因为两个构造方法参数不同,得到两个不同的宽高值,于是在绘制中就出现界面异常。(具体为什么算出来的Width和height差异很大还需要继续调查,有兴趣的可以再查查)
- android BitmapDrawable的使用
- android BitmapDrawable的使用
- android BitmapDrawable的使用
- android BitmapDrawable的使用
- android BitmapDrawable的使用
- Android BitmapDrawable()的使用
- Android BitmapDrawable()的使用
- Android BitmapDrawable使用场景
- android bitmapdrawable基本使用
- bitmapdrawable的使用
- Android的Drawable之BitmapDrawable
- Android中BitmapDrawable的一个bug
- Android Bitmap 和 BitmapDrawable的区别
- BitmapDrawable和Bitmap.XML的使用
- Bitmap,BitmapDrawable,BitmapFactory的简单使用,以及AssetManager的使用
- Android-drawable资源-BitmapDrawable
- android--BitmapDrawable之BitmapShader
- 6.1 Android Framework - BitmapDrawable
- 欢迎使用CSDN-markdown编辑器
- 波特率编程
- 干货|基于Ansible的Ceph自动化部署解析
- WinDbg学习笔记六
- VMware之EXSI安装虚拟机-yellowcong
- Android BitmapDrawable()的使用
- GPIO Init structure definition
- 函数,变量名命名的三种方法
- Netty(一) 简单入门
- 复选框单选
- JAVA_Math类常用
- 基于SSM使用ueditor编辑框文本、图片上传功能实现
- VS2010 MFC读取Excel文件中数据
- H5页面出现广告。。。。。