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

时间:2012-10-06 09:36:22

标签: 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

2 个答案:

答案 0 :(得分:2)

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

守则(之前及之后):

    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);

谢谢大家。

答案 1 :(得分:0)

我会在createBitmap()

中尝试Bitmap.Config.RGB_565