使用 getDrawingCache 时提高图像质量 - android

And*_*dyN 3 android image android-layout image-quality

我正在使用getDrawingCache()方法捕获我的布局视图并从中创建图像。代码运行良好并生成图像,但这里的问题是,生成的图像质量非常低。我希望生成的图像的分辨率很高,这样无论何时我设置ImageView它都不会被拉伸。

这是我正在使用的代码:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout); layout.setDrawingCacheEnabled(true); Bitmap bmp = layout.getDrawingCache(); File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis()+".jpg"); try { FileOutputStream out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); }

PS -我也试过在打电话之前使用layout.buildDrawingCache(true);layout.setDrawingCacheQuality(RelativeLayout.DRAWING_CACHE_QUALITY_HIGH);layout.getDrawingCache();但没有发现质量有任何变化。

任何人都可以帮我解决这个问题,我怎样才能提高生成的图像的质量?提前致谢。

And*_*dyN 5

在搜索了很长时间并尝试了不同的解决方案之后,最终我获得了比以前更好的捕获图像质量。

我将代码更改为:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(true);
final Bitmap bmp = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
Canvas c= new Canvas(bmp);


File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + System.currentTimeMillis()+".jpg");
try {

    FileOutputStream out = new FileOutputStream(f);
    c.drawBitmap(bmp, 0, 0, null);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);

    out.flush();
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这就是帮助我的原因。希望它也能帮助其他人。