android - Android 4.1.2 上 ImageView 中的倾斜图像

标签 android android-canvas android-imageview android-4.2-jelly-bean android-bitmap

我有一个在 ImageView 中显示图像的应用程序,但我遇到了专门针对 Android 4.1.2 的问题。已确认无法在三个独立的 4.1.2 设备上运行,而在 2.3.7、4.2.1、4.3 和 4.4.2 上运行。该错误发生在几个不同的图像上,但不是全部。某些特定的 JPEG 文件似乎无法按预期工作。

它的实际外观以及它在 Android 4.1.2 上的显示方式:

How the image looks How it looks on 4.1.2

上图(左)就是这样一个有问题的图像文件。

设置显示图像背后的代码摘要是:

Bitmap bitmap, background;
ImageView imageView = (ImageView)findViewById(R.id.imageView);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog, options);

background = bitmap.copy(Bitmap.Config.RGB_565, true);

Canvas canvas = new Canvas(background);
canvas.drawBitmap(bitmap, 0, 0, null);
// Some calls to canvas.drawText(....) here, but doesn't have to happen for the error to occur

imageView.setImageBitmap(background);

我想我可以在 Photoshop 中调整上述照片的大小并重新保存它以使其正常工作,但不知道为什么。因为我有几个,所以我宁愿不必这样做。

我想知道 Android 4.1.2 上这个错误的来源是什么,是否有一些编程方法可以修复它?

我在谷歌 View “倾斜”、“倾斜”、“扭曲”和类似的情况下试过运气,但很少有人提到它,也没有修复。这是我发现的带有屏幕截图的提及:

最佳答案

基于 rupps 的评论我改变了:

bitmap.copy(Bitmap.Config.RGB_565, true)

收件人:

bitmap.copy(Bitmap.Config.ARGB_8888, true)

这确实解决了 4.1.2 设备的问题,同时所有其他测试设备的功能保持相似。这确实以编程方式解决了我的问题。但是请注意,它确实需要双倍的内存,因为每个像素都存储在 4 个字节而不是 2 个字节上。

至于问题的根源,我是从the documentation of RGB_565上看到的那:

This configuration can produce slight visual artifacts depending on the configuration of the source.

我认为这主要与 strip /颜色/抖动问题有关,这并不能解释特定于版本的问题,但也许为什么这个设置很麻烦。

关于android - Android 4.1.2 上 ImageView 中的倾斜图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25921458/

相关文章:

android - 无法从 build.gradle 更新 Android Studio for mac 中的版本代码

android - AndroidX 中的 Canvas clipOutRect 兼容性

java - 向图像添加 Intent 以链接到另一个页面

java - 标签内的谷歌地图 fragment

android - 增加 androidTest 的 targetSdkVersion

android - Android 中基于 1 位的位图

android - 如何在 Android 自定义 Canvas 中绘制圆形?

java - 我怎样才能检测到所有在 Android 中被触摸过的 ImageView?

android - ImageView.setImageBitmap 为空白

android - AsyncTask 类中的错误无法发送数据