1

我正在使用演示应用程序MyView在画布上绘制内容。FingerPaint API我想捕捉我在画布上写的任何东西。但是当我使用View v1 = myview.getRootView()它时,它只返回空白画布而不是内容。我想将我的绘图保存在 SDCard 中。以下是我的代码。让我知道我需要改变什么

v1 = myview.getRootView();
System.out.println("v1 value = "+v1);
v1.buildDrawingCache(true);
v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
           MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight()); 
v1.layout(0, 0, 100, 100); 

//Bitmap b = Bitmap.createBitmap(v1.getDrawingCache());
myview.mBitmap = Bitmap.createBitmap(v1.getDrawingCache());
System.out.println("BITMAP VALue = "+myview.mBitmap);

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
try {
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
} catch (Exception e) 
{
    e.printStackTrace();
}
v1.setDrawingCacheEnabled(false);

myviewMyView是扩展的类的对象View

4

1 回答 1

0

您可以直接使用 myview 对象来获取当前视图/画布

myview.setDrawingCacheEnabled(true);
Bitmap b = myview.getDrawingCache();

对于不可变的图像使用这个

Bitmap b = myview.getDrawingCache().copy(Config.ARGB_8888, true);

然后您可以将此位图保存为图像文件

于 2012-04-12T06:09:12.587 回答