![](/img/trans.png)
[英]How to save imageview as bitmap automatically once layout loads in Android?
[英]How to save multiple ImageViews as one Bitmap while maintaining position of each ImageView in Android?
我需要将三个ImageView保存为一个位图。 以下代码可将三个ImageView保存为一个位图,但会将每个图像推到左上角,从而破坏了用户的图像外观。 其中两个ImageView用颜色填充,另一个是实际图像。 如何在将ImageView保存为一个位图的同时仍保持其在布局中的位置?
到目前为止,这是我尝试过的方法:
public void saveDrawer(){
Bitmap mainBmp = backImage.getDrawingCache();
Bitmap centerBmp = centerImage.getDrawingCache();
Bitmap handleBmp = handleImage.getDrawingCache();
Canvas canvas = new Canvas(mainBmp);
canvas.drawBitmap(centerBmp, 1, 1, null);
canvas.drawBitmap(handleBmp, 1, 1, null);
OutputStream fOut = null;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "CustomDrawer" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "customdrawer.png");
fOut = new FileOutputStream(sdImageMainDirectory);
Log.d("Path", fOut.toString());
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
mainBmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
}
生成的图像应该看起来像这样:
canvas.drawBitmap(handleBmp, 1, 1, null);
1,1可以设定位置
关于此问题的上下文,我认为您应该做的是,将所有ImageView排列在单个父布局中,例如LinearLayout或FrameLayout或可能是RelativeLayout。 然后从这些父对象之一获取图形缓存,并将该位图保存到SD。
注意:相应地启用和禁用布局上的图形缓存。 这就是我所做的。
relativeLayoutBackground.setDrawingCacheEnabled(true);
relativeLayoutBackground.buildDrawingCache(true);
Bitmap bitmap = relativeLayoutBackground.getDrawingCache();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.