Android绘图基础--简易写字板

/**
* 自定义一个写字板
* @description:自定义一个控件,实现一个可以手写的效果,后期是要实现刮刮卡抽奖效果,待续….
*/
@SuppressLint(“DrawAllocation”)
public class WriteView extends View {
private Paint mPaint;// 画笔
private Path mPath;// 绘制路径
private Canvas mCanvas;// 画面Canvas
private int mLastX;// 手指移动后X位置
private int mLastY;// 手指移动后Y位置
private Bitmap mBitmap;// 图片
public WriteView(Context context) {
this(context, null, 0);
}

public WriteView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initViewsParams();
}
public WriteView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

/**
 * 初始化相关数据及参数
 * @description:
 */
private void initViewsParams() {
    mPath=new Path();
    mPaint = new Paint();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();// 获取到宽度
    int height = getMeasuredHeight();// 获取到高度
    mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 根据参数创建新位图
    mCanvas = new Canvas(mBitmap);
    // 设置画笔参数
    mPaint.setColor(Color.GREEN);// 设置颜色
    mPaint.setAntiAlias(true);// 设置抗紧锯齿
    mPaint.setDither(true);// 防抖动
    mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置图形结合处的样子
    mPaint.setStrokeCap(Paint.Cap.ROUND);// 设置笔刷的图形样式,如圆形样式
    mPaint.setStyle(Style.STROKE);// 设置画笔样式为空心
    mPaint.setStrokeWidth(10);// 设置画笔粗细
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action=event.getAction();
    int x=(int) event.getX();//当前x
    int y=(int) event.getY();//当前y
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastX=x;
            mLastY=y;
            mPath.moveTo(mLastX,mLastY);//移动Path
            break;
        case MotionEvent.ACTION_MOVE:
            int dx=Math.abs(x-mLastX);
            int dy=Math.abs(y-mLastY);
            if(dx>3||dy>3){
                mPath.lineTo(x, y);
            }
            mLastX=x;
            mLastY=y;
            break;
        case MotionEvent.ACTION_UP:
            break;
        default:
            break;
    }
    invalidate();
    return true;//这里要返回true才可以哦·····
}
@Override
protected void onDraw(Canvas canvas) {
    mCanvas.drawPath(mPath, mPaint);
    canvas.drawBitmap(mBitmap, 0, 0, null);
}

}

你可能感兴趣的:(android,Path)