我正在使用 getDrawingCache()
方法捕获我的布局 View 并从中创建图像。
代码工作正常并生成图像,但这里的问题是,生成的图像质量很低。我希望生成的图像的分辨率很高,这样无论何时将其设置为 ImageView
,它都不会被拉伸(stretch)。
这是我正在使用的代码:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setDrawingCacheEnabled(true);
位图 bmp = layout.getDrawingCache();
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis()+.jpg");
尝试 {
FileOutputStream out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
关闭();
} catch (IOException e){
e.printStackTrace();
}
附言- 在调用 layout.getDrawingCache() 之前,我还尝试使用
但没有发现质量变化。layout.buildDrawingCache(true);
和 layout.setDrawingCacheQuality(RelativeLayout.DRAWING_CACHE_QUALITY_HIGH);
;
谁能帮我解决这个问题,我怎样才能提高生成图像的质量? 提前致谢。
最佳答案
经过很长时间的搜索和尝试不同的解决方案后,我终于获得了比之前更好的捕获图像质量。
我将代码更改为:
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();
}
这对我有帮助。希望它也能帮助其他人。
关于android - 使用 getDrawingCache 提高图像质量 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838387/