画板/鼠标垫 自定义view代码:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/*
*Author:XingHai.Zhao
*Purpose:鼠标垫/画板
*/
public class MouseMat extends View {
public MouseMat(Context context) {
super(context);
}
public MouseMat(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MouseMat(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private Path path;
private Paint paint;
private void init() {
path = new Path();
paint = new Paint();
paint.setColor(Color.CYAN); //颜色
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10); //边框宽度
setClickable(true);//设置可点击
}
public MouseTouchListener mouseTouchListener;
public interface MouseTouchListener {
void MouseEvent(MotionEvent event);
}
public void setMouseTouchListener(MouseTouchListener mouseTouchListener) {
this.mouseTouchListener = mouseTouchListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MO