我有一个需要将布局转换为图像的应用程序,但问题是图像的分辨率会根据手机的密度而有所不同。
如果我在手机 A 上使用该应用程序,分辨率将为 480x350,但在手机 B 上,图像的分辨率将为 400x280。我的问题是:是否可以将此布局转换为图像,但无论其密度如何,每部手机的分辨率都相同?
我的代码:
private void salva() {
if(myDir.exists()==false) {
myDir.mkdirs();
}
RL = (RelativeLayout) findViewById(R.id.CardsLay);
RL.setDrawingCacheEnabled(true);
img = RL.getDrawingCache();
Canvas canvas = new Canvas(img);
try {
FileOutputStream out = new FileOutputStream(file);
canvas.drawBitmap(img, 0, 0, null);
img.compress(CompressFormat.JPEG, 80, out);
out.flush();
out.close();
RL.setDrawingCacheEnabled(false);
Toast.makeText(getApplicationContext(), "Image saved on phone", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Ops", Toast.LENGTH_SHORT).show();
}
}