这三个属性值在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.youkuaiyun.com/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());
- }
- }
从以上的分析可以得知,inDensity,inTargetDensity,inScreenDensity这三个值的目的就是为了确定这个Bitmap的宽高和density。