我看到好多博客都是这样计算的,可是这样算对吗?有没有哥们试验过这种方法正确性?我以为看博客要对博主表示怀疑,论证别人写的是否正确。更多详细能够看个人GitHub:https://github.com/yangchong211
- 说出个人结论:上面1.1这种说法也对,可是不全对,没有说明场景,同时也忽略了一个影响项:Density。接下来看看源代码。
- inDensity默认为图片所在文件夹对应的密度;inTargetDensity为当前系统密度。
- 加载一张本地资源图片,那么它占用的内存 = width * height * nTargetDensity/inDensity * nTargetDensity/inDensity * 一个像素所占的内存。
@Nullable
public static Bitmap decodeResourceStream(@Nullable Resources res, @Nullable TypedValue value,
@Nullable InputStream is, @Nullable Rect pad, @Nullable Options opts) {
validate(opts);
if (opts == null) {
opts = new Options();
}
if (opts.inDensity == 0 && value != null) {
final int density = value.density;
if (density == TypedValue.DENSITY_DEFAULT) {
opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
} else if (density != TypedValue.DENSITY_NONE) {
opts.inDensity = density;
}
}
if (opts.inTargetDensity == 0 && res != null) {
opts.inTargetDensity = res.getDisplayMetrics().densityDpi;
}
return decodeStream(is, pad, opts);
}