我正在尝试在SD卡上保存画布。基本上我在onDraw(Canvas画布)方法中绘制两个位图(一个在另一个上面)。但是当我保存文件时,只存储底层的位图。我在这里发布了onDraw方法的代码:
Paint paint = new Paint();
paint.setColor(Color.BLACK);
//rectangle for the first image
rct = new Rect(10, 10, canvas.getWidth(), canvas.getHeight());
// rectangle for the second image, the secong image is drawn where the user touches the screen
new_image = new RectF(touchX, touchY, touchX + secondBitmap.getWidth(),
touchY + secondBitmap.getHeight());
//this is the bitmap that is drawn first
canvas.drawBitmap(firstBitmap, null, rct, paint);
//this is the bitmap drawn on top of the first bitmap on user touch
canvas.drawBitmap(secondBitmap, null, new_image, paint);
canvas.save();
在MainActivity上写的SD卡上保存画布的代码是:
Bitmap bm = canvas.getDrawingCache() // canvas in an object of the class I extended from View
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
boolean exists = (new File(path)).exists();
OutputStream outStream = null;
File file = new File(path, "drawn_image" + ".jpg");
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
问题是只有基本图像(onDraw()方法的firstBitmap)保存在SDCard而不是整个画布上(同时包含两个图像)。我是画布的新手...所以任何帮助都会非常感激
答案 0 :(得分:2)
当调用getDrawingcache()时,它会使视图无效以获取完整缓存。所以调试你的代码并检查它是否在视图中遍历onDraw()方法的每一行。