我在 Canvas 上画了一些东西,我想将此图作为 jpeg 图像保存在我的存储中。我使用了以下方法,它在我的目录中保存了图像sign.jpg,但它保存了空白黑色图像,没有在其上绘制任何内容,其大小为14.49kb。
这是方法
public Bitmap savebit(){
Bitmap saveBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(saveBitmap);
canvas.setBitmap(saveBitmap);
this.draw(canvas);
File file = new File(Environment.getExternalStorageDirectory() + "/sign.jpg");
try {
saveBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
return saveBitmap;
}
最佳答案
只需将您的膨胀 View 传递给以下函数
Bitmap bmp = viewToBitmap(abc);
Bitmap bmp = viewToBitmap(frameLayout);
Bitmap bmp = viewToBitmap(LinearLayout);
Bitmap bmp = viewToBitmap(AnyView);
<小时/>
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
关于java - 如何在android Canvas 中将整个 Canvas 保存为jpeg图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39616603/