部分代码借鉴网上。
看图先:
主界面Activity :
package com.mj.candraw;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class CanDrawActivity extends Activity {
/** Called when the activity is first created. */
private MyDrawView mView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitView();
setContentView(mView);
}
private void InitView() {
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mView = new MyDrawView(CanDrawActivity.this);
mView.setLayoutParams(params);
mView.setFocusable(true);
}
}
自定义涂鸦控件,MyDrawView :
package com.mj.candraw;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyDrawView extends View {
public static final String TAG = MyDrawView.class.getSimpleName();
private Context mContext = null;
private Path mPath = null;
private Paint mPaint = null;
private Paint mBitmapPaint = null;// 画布的画笔
private Canvas mCanvas = null;
private Bitmap mBitmap = null;
private int mWidth = 0;
private int mHeight = 0;
private int mX, mY;
private final int SPACE = 4;
private boolean mInit = false;
public MyDrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
// InitView(context);
mContext = context;
}
public MyDrawView(Context context, AttributeSet a) {
super(context, a);
// TODO Auto-generated constructor stub
// InitView(context);
mContext = context;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
Log.i(TAG, "onLayout ");
InitView(mContext);
super.onLayout(changed, left, top, right, bottom);
}
private void InitView(Context context) {
mWidth = this.getWidth();
mHeight = this.getHeight();
Log.i(TAG, "mWidth=" + mWidth + " mHeight=" + mHeight);
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置外边缘
mPaint.setStrokeCap(Paint.Cap.SQUARE);// 形状
mPaint.setStrokeWidth(5);// 画笔宽度
mInit = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
touch_down(x, y);
break;
}
case MotionEvent.ACTION_UP: {
touch_up(x, y);
break;
}
case MotionEvent.ACTION_MOVE: {
touch_move(x, y);
break;
}
}
return true;
}
private void touch_down(int x, int y) {
Log.i(TAG, "touch_down x=" + x + " y=" + y);
mPath = new Path();
mPath.moveTo(x, y);
mX = x;
mY = y;
this.postInvalidate();
}
private void touch_move(int x, int y) {
// Log.i(TAG, "touch_down x=" + x + " y=" + y);
int _x = Math.abs(x - mX);
int _y = Math.abs(y - mY);
if (_x > SPACE || _y > SPACE) {
mPath.lineTo(x, y);
mX = x;
mY = y;
this.postInvalidate();
}
}
private void touch_up(int x, int y) {
Log.i(TAG, "touch_up x=" + x + " y=" + y);
mPath.lineTo(x, y);
mCanvas.drawPath(mPath, mPaint);
this.postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if (!mInit)
return;
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
if (mPath != null) {
canvas.drawPath(mPath, mPaint);
}
super.onDraw(canvas);
}
}