package com.angluswang.scratch.view;

import android.content.Context;
import android.graphics.Bitmap;
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;

/**
 * Created by Jeson on 2016/6/21.
 *  自定义刮刮卡控件
 */

public class Scratch extends View {

    private Paint mOutterPaint;
    private Path mPath;
    private Canvas mCanvas;
    private Bitmap mBitmap;

    private int mLastX;
    private int mLastY;

    public Scratch(Context context) {
        this(context, null);
    }

    public Scratch(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Scratch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        mOutterPaint = new Paint();
        mPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        //初始化Bitmap
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

        //设置画笔的一些属性
        setupOutpaint();

    }

    /**
     * 设置绘图画笔的一些属性
     */
    private void setupOutpaint() {

        mOutterPaint.setColor(Color.GREEN);
        mOutterPaint.setAntiAlias(true);
        mOutterPaint.setDither(true);
        mOutterPaint.setStrokeJoin(Paint.Join.ROUND);
        mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutterPaint.setStyle(Paint.Style.FILL);
        mOutterPaint.setStrokeWidth(20);
    }

    /**
     * 用户触摸事件
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN:

                System.out.print("down");
                mLastX = x;
                mLastY = y;
                mPath.moveTo(mLastX, mLastY);
                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;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawPath();
        mCanvas.drawBitmap(mBitmap, 0, 0, null);
    }

    private void drawPath() {
        mCanvas.drawPath(mPath, mOutterPaint);
    }
}

@@ -1756,12 +1756,14 @@
 yle.
-FILL
+STROKE
 );%0A 
@@ -2719,32 +2719,79 @@
 eak;%0A        %7D%0A%0A
+        mCanvas.drawPath(mPath, mOutterPaint);%0A
         invalida
@@ -2885,27 +2885,8 @@
 ) %7B%0A
-        drawPath();
 %0A   
@@ -2882,34 +2882,33 @@
 vas) %7B%0A%0A        
-mC
+c
 anvas.drawBitmap
@@ -2941,89 +2941,6 @@
  %7D%0A%0A
-    private void drawPath() %7B%0A        mCanvas.drawPath(mPath, mOutterPaint);%0A    %7D%0A
 %7D%0A

修复bug，能再新建的画板上作图