【发布时间】:2012-01-01 00:27:14
【问题描述】:
【问题讨论】:
-
访问以下 [Link][1] 以获得答案。 [1]:stackoverflow.com/questions/2174875/android-canvas-to-jpg
标签: android android-layout android-emulator android-widget android-canvas
【问题讨论】:
标签: android android-layout android-emulator android-widget android-canvas
试试这个代码
View content = your_view;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path+"/image.png");
FileOutputStream ostream;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
Toast.makeText(getApplicationContext(), "image saved", 5000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", 5000).show();
}
【讨论】:
view的背景?现在只保存绘图而不保存背景。
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();
然后使用位图工厂将位图写入文件。
【讨论】:
一个选项是创建另一个画布(如下所示)并在这个新画布上重复所有绘图。 完成后,调用 drawBitmap。
Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);
// Do all your drawings here
canvas.drawBitmap(// The first picture //);
如果有一种方法可以复制现有的画布,那么最好的办法是您不需要重新绘制所有内容,但我找不到。
【讨论】:
我已经实施了以下方法并为我工作。 通过使用 xml 文件中的 id 而不是通过实例化 Customview 来获取您的 CustomView。
View v = findViewById(R.id.custom_view);
//don't get customview by this way, View v = new CustomView(this);
int canvasWidth = v.getWidth();
int canvasHeight = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
所有代码都应该在 saveButton 点击监听器中。
【讨论】: