android - 捕获布局屏幕截图后,ImageView 是透明的

标签 android android-layout

我在 Android 屏幕捕获方面遇到问题。捕获的图像是透明的,而原始图像是不透明的固体 JPEG 文件! 我在许多具有不同操作系统版本的设备上进行了测试,并且所有手机上都存在问题。 这是我的屏幕捕获代码以及最终输出

代码:

final Bitmap rawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(rawBitmap);
rendererView.layout(0, 0, width, height);
rendererView.draw(canvas);
rawBitmap.compress(CompressFormat.PNG, 100, new FileOutputStream("/sdcard/test" + System.currentTimeMillis() + ".png"));

结果:

enter image description here

最佳答案

问题已经解决了。将图像源加载到 ImageView 时,我对 BitmapFactory 选项使用了错误的设置。

代码(之前和之后):

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    //options.inPreferredConfig = Config.ARGB_4444; //WRONG
    options.inPreferredConfig = Config.ARGB_8888; //CORRECT
    bitmap = BitmapFactory.decodeFile(result.filePath(), options);

谢谢大家。

关于android - 捕获布局屏幕截图后,ImageView 是透明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758571/

相关文章:

java - 动态替换 Android fragment

android - 在 Android 应用程序中将图像上传到服务器

android - 通过 AlarmManager 创建的警报在 25 天后未运行(= 2^31 - 1 = 2147483647 毫秒)

java - JSONException : Value <! java.lang 类型的 DOCTYPE 无法转换为 JSONObject

android - setContentView 花费太多时间

android - 我想为所有 Activity 保留一个操作栏

android - 资源 $NotFoundException : Resource is not a ColorStateList (color or path): } in android

android - 如何在同一 Activity 中实现具有不同项目列表和不同点击操作的多个微调器

android - com.google.ads.AdView 无法实例化布局问题

安卓动态分屏