0

我正在尝试将画布保存在 SD 卡上。基本上我在 onDraw(Canvas 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 上编写的 SDcard 上保存画布的代码是:

   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 上,而不是整个画布(两个图像)。我是画布新手......所以任何帮助将不胜感激

4

1 回答 1

2

当调用 getDrawingcache() 时,它会使视图无效以获取完整缓存。所以调试你的代码并检查它是否在视图中遍历你的 onDraw() 方法的每一行。

于 2013-05-10T09:30:36.883 回答