2013-05-09 38 views
0

我正试图在sdcard上保存一个画布。基本上我在onDraw(Canvas canvas)方法中绘制两个位图(一个在另一个之上)。但是当我保存文件时,只有最底层的Bitmap被存储。我张贴了代号为的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(); 
    } 

的问题是,只有基本的图像(firstBitmap的onDraw()方法)保存在SDCard上,而不是整个画布(包含两个图像)。我是新来的画布...所以任何帮助将不胜感激

回答

2

当getDrawingcache()被称为它使视图无效,以获得完整的缓存。所以调试你的代码,并检查它是否正在浏览你的onDraw()方法的每一行。

+0

感谢男人....它解决了我的问题...... – anz 2013-05-10 09:31:19