Android Lollipop 的位图解码问题
Bitmap decode issues with Android Lollipop
我在 Lollipop 设备中解码图像时遇到问题。
下图显示了一个"testing app",它解码了 4 个 png 文件。
左 = kitkat 4.4.4,右 = lollypop (5.0.1)
两者的设备类型相同:MOTO G 2nd Generation。
相关代码:
Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
options.inScaled = true;
options.inDensity = 160;
options.inTargetDensity = 320;
Bitmap bmp = BitmapFactory.decodeStream(new ByteArrayInputStream(imageAsBytes), null, options);
屏幕指标:
Density......: 2
ScaledDensity: 2
DensityDPI...: 320
Size.........: 720x1184
我在网上找到了任何解释。
谁能告诉我为什么会这样?
编辑:
因为它只发生在棒棒糖 + TargetDensity = 320 + Density = 160 时,我做了以下解决方法:
if ((android.os.Build.VERSION.SDK_INT >= 21) && (options.inTargetDensity == 320) && (options.inDensity = 160))
{
options.inDensity = options.inTargetDensity;
}
但我真的很感谢一些提示来弄清楚发生了什么......
在我的案例中,下面的解决方案是(添加 setDensity 并使用在 Density 上查看的新 BitmapDrawable)。希望它也能帮助你。 (顺便说一句,这 2 行,确实花了我 3 天 :)
final Bitmap output = Bitmap.createBitmap(44, 65, Bitmap.Config.ARGB_8888);
/*NEW -> */ output.setDensity(320);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
// BitmapDrawable bmd = new BitmapDrawable(output);
/*Change into*/
BitmapDrawable bmd = new BitmapDrawable(Resources.getSystem(),output);
我在 Lollipop 设备中解码图像时遇到问题。
下图显示了一个"testing app",它解码了 4 个 png 文件。
左 = kitkat 4.4.4,右 = lollypop (5.0.1)
两者的设备类型相同:MOTO G 2nd Generation。
相关代码:
Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
options.inScaled = true;
options.inDensity = 160;
options.inTargetDensity = 320;
Bitmap bmp = BitmapFactory.decodeStream(new ByteArrayInputStream(imageAsBytes), null, options);
屏幕指标:
Density......: 2
ScaledDensity: 2
DensityDPI...: 320
Size.........: 720x1184
我在网上找到了任何解释。
谁能告诉我为什么会这样?
编辑:
因为它只发生在棒棒糖 + TargetDensity = 320 + Density = 160 时,我做了以下解决方法:
if ((android.os.Build.VERSION.SDK_INT >= 21) && (options.inTargetDensity == 320) && (options.inDensity = 160))
{
options.inDensity = options.inTargetDensity;
}
但我真的很感谢一些提示来弄清楚发生了什么......
在我的案例中,下面的解决方案是(添加 setDensity 并使用在 Density 上查看的新 BitmapDrawable)。希望它也能帮助你。 (顺便说一句,这 2 行,确实花了我 3 天 :)
final Bitmap output = Bitmap.createBitmap(44, 65, Bitmap.Config.ARGB_8888);
/*NEW -> */ output.setDensity(320);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
// BitmapDrawable bmd = new BitmapDrawable(output);
/*Change into*/
BitmapDrawable bmd = new BitmapDrawable(Resources.getSystem(),output);