inDensity,inTargetDensity,inScreenDensity关系详解
来源:互联网 发布:windows vista界面 编辑:程序博客网 时间:2024/06/10 09:46
如果接触Android中的Bitmap较深的话,肯定会知道标题上的这三个属性值,那么这三个属性值的关系和作用是什么呢?下面我们就来详细解读一下。
这三个属性值在Bitmap和BitmapFactory中都有不同程度的出现,其中Bitmap中只有mDensity(对应inDensity),BitmapFactory中这三个都有,那么可以知道这三个值主要是在BitmapFactory中活动,于是我就查看了BitmapFactory中的源码,从而知道了这三个值的目的是为了确定这个Bitmap的宽高和density。
下面我们从源码角度来分析一下,首先,这个Bitmap通过BitmapFactory创建出来一般经历两个方法,一个是解码的native方法,一个是BitmapFactory中的内部方法setDensityFromOptions。参看以下源码:
try { bm = nativeDecodeByteArray(data, offset, length, opts); if (bm == null && opts != null && opts.inBitmap != null) { throw new IllegalArgumentException("Problem decoding into existing bitmap"); } setDensityFromOptions(bm, opts); } finally { Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS); }
我们先来分析native方法,以nativeDecodeByteArray方法为例:
引用摘自http://blog.csdn.net/bigconvience/article/details/27054639的nativeDecodeByteArray方法源码片段:
可以看到此方法中调用了doDecode方法,我们再来看doDecode方法:
通过源码可以发现,inDensity,inTargetDensity,inScreenDensity这三个值的主要目的就是确定这个Bitmap的是否会被缩放,如果被缩放,缩放之后的宽高是多少。
再来看setDensityFromOptions这个方法的源码:
/** * Set the newly decoded bitmap's density based on the Options. */ private static void setDensityFromOptions(Bitmap outputBitmap, Options opts) { if (outputBitmap == null || opts == null) return; final int density = opts.inDensity; if (density != 0) { outputBitmap.setDensity(density); final int targetDensity = opts.inTargetDensity; if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) { return; } byte[] np = outputBitmap.getNinePatchChunk(); final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np); if (opts.inScaled || isNinePatch) { outputBitmap.setDensity(targetDensity); } } else if (opts.inBitmap != null) { // bitmap was reused, ensure density is reset outputBitmap.setDensity(Bitmap.getDefaultDensity()); } }从setDensityFromOptions方法中可以得知,会根据Options中的inDensity,inTargetDensity,inScreenDensity三个值和是否被缩放标识inScaled来确定这个Bitmap的mDensity(对应inDensity)。
从以上的分析可以得知,inDensity,inTargetDensity,inScreenDensity这三个值的目的就是为了确定这个Bitmap的宽高和density。
- inDensity,inTargetDensity,inScreenDensity关系详解
- inDensity,inTargetDensity,inScreenDensity关系详解
- android使用inSampleSize、inScaled、inDensity、inTargetDensity对图片进行缩放
- android使用inSampleSize、inScaled、inDensity、inTargetDensity对图片进行缩放
- decode图片时BitmapFactory.Options中的inDensity和inTargetDensity
- android使用inSampleSize、inScaled、inDensity、inTargetDensity对图片进行缩放
- 依赖关系详解
- Hibernate对应关系详解
- Hibernate对应关系详解
- Hibernate关系映射详解
- Hibernate关系映射详解
- 继承关系映射详解
- 类图关系详解
- UML图 关系详解
- socket通信协议关系详解
- UML图关系详解
- UML图 关系详解
- 用例图中三种关系详解
- C++ 输入输出总结
- LeetCode 14 Longest Common Prefix 0ms实现的算法
- LESS
- floyd + 记录路径 hdu 1385
- HTML生成一个二维码,微信扫一扫,可以对网页分享
- inDensity,inTargetDensity,inScreenDensity关系详解
- nutch 过滤字符
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30) 中序遍历往BST中填数据
- 有符号char的+、-法位运算过程
- hdu 5333 Undirected Graph (LCT)
- qt学习第二课:建立链接
- HDU 4983/BC 6C Goffi and GCD
- ASP.NET MsChart 控件出错:为 ChartImg.axd 执行子请求时出错
- android的内容提供者