满意答案
wpdhc
2016.07.05
采纳率:49% 等级:7
已帮助:359人
public class DrawView extends View {
Context mycontext;
int toasttime = 1000*60;
boolean enabletoast = true;
//之前的坐标
float preX;
float preY;
//路径
private Path path;
//画笔
public Paint paint = null;
//默认画布大小
public static int VIEW_WIDTH = 500;
public static int VIEW_HEIGHT = 600;
//
Bitmap cacheBitmap = null;
//
Canvas cacheCanvas = null;
public DrawView(Context context, AttributeSet set) {
super(context, set);
mycontext = context;
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas = new Canvas();
path = new Path();
cacheCanvas.setBitmap(cacheBitmap);
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setAntiAlias(true);
paint.setDither(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_MOVE:
path.quadTo(preX, preY, x, y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_UP:
cacheCanvas.drawPath(path, paint);
path.reset();
if(enabletoast == false){
}
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint bmpPaint = new Paint();
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
canvas.drawPath(path, paint);
}
}
00分享举报