且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绘制的图像并保存图像到Android?

更新时间:2023-12-05 16:11:04

您的问题是绘图一遍又一遍整个画布上:

Your problem is your drawing over and over on your entire canvas:

 final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background
 c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent
 canvas.drawColor(Color.WHITE);  // this makes it all white on another canvas
 canvas.drawBitmap (mBitmap, 0,  0,null); // this draws your bitmap on another canvas

使用逻辑大致是这样的:

Use logic roughly like this:

@Override
public void run() {

Canvas c = new Canvas(mBitmap);


/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/
...

/* Now mBitmap will have both the original image & your painting */
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card
OutputStream fOut = null;
File file = new File(path, "MyImage.jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}

另外不要忘了加上必要的权限来保存文件:

Also don't forget to add necessary permission to save your file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

之外&lt;应用&GT;&LT; /用途&gt;在清单文件