你尝试使用这个例子。例如。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class FingerpaintView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "FingerpaintView";
private Paint foregroundPaint;
private Paint backgroundPaint;
private int width, height;
private int lastTouchX, lastTouchY;
private Bitmap pictureBitmap;
private Canvas pictureCanvas;
private final Context context;
public FingerpaintView(Context context) {
super(context);
this.context=context;
init();
}
public FingerpaintView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
init();
}
private void init() {
setFocusableInTouchMode(true);
getHolder().addCallback(this);
foregroundPaint = new Paint();
foregroundPaint.setColor(Color.WHITE);
foregroundPaint.setStrokeWidth(4);
backgroundPaint = new Paint();
backgroundPaint.setColor(Color.BLACK);
lastTouchX = -1;
lastTouchY = -1;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_MOVE)) {
Log.d(TAG, "Touched " + x + "," + y);
if ((lastTouchX != -1) && (lastTouchY != -1)) {
pictureCanvas.drawLine(lastTouchX, lastTouchY, x, y, foregroundPaint);
draw();
}
lastTouchX = x;
lastTouchY = y;
} else {
lastTouchX = -1;
lastTouchY = -1;
}
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU){
clear();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
this.width = width;
this.height = height;
if (pictureBitmap != null) {
pictureBitmap.recycle();
}
pictureBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
pictureCanvas = new Canvas(pictureBitmap);
clear();
draw();
}
public void draw() {
final Canvas c = getHolder().lockCanvas();
if (c != null) {
c.drawBitmap(pictureBitmap, 0, 0, null);
getHolder().unlockCanvasAndPost(c);
}
}
public void clear() {
pictureCanvas.drawRect(0, 0, width, height, backgroundPaint);
draw();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (pictureBitmap != null) {
saveFile(pictureBitmap,"MyImage");
pictureBitmap.recycle();
}
pictureBitmap = null;
}
private void saveFile(Bitmap bitmap, String name) {
String extStorageDirectory;
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
final File file = new File(extStorageDirectory, name);
try {
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(context, "Saved", Toast.LENGTH_LONG).show();
} catch (final FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
} catch (final IOException e) {
e.printStackTrace();
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
我期望你会很有帮助。