package com.jigdraw.draw.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.jigdraw.draw.R;

/**
 * Custom view to represent the drawing view the user use to draw.
 *
 * @author Jay Paulynice
 */
public class DrawingView extends View {
    private Path drawPath;
    private Paint drawPaint, canvasPaint;
    private int paintColor = 0xFF660000;
    private Canvas drawCanvas;
    private Bitmap canvasBitmap;
    private float brushSize, lastBrushSize;

    /**
     * Create new drawing view with context and attributes and setting up the
     * default parameters.
     *
     * @param context the context
     * @param attrs the attributes
     */
    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * Init parameters
     */
    private void init() {
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(brushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_UP:
                drawPath.lineTo(touchX, touchY);
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    /**
     * Set new color for drawing
     *
     * @param newColor the new color
     */
    public void setColor(int newColor) {
        invalidate();
        paintColor = newColor;
        drawPaint.setColor(newColor);
    }

    /**
     * Set new brush size for drawing
     *
     * @param newSize the new color
     */
    public void setBrushSize(float newSize) {
        lastBrushSize = brushSize;
        brushSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                newSize, getResources().getDisplayMetrics());
        drawPaint.setStrokeWidth(brushSize);
    }

    /**
     * @return last brush size
     */
    public float getLastBrushSize() {
        return lastBrushSize;
    }

    /**
     * Set erase to true when the erase button is clicked.
     *
     * @param isErase {@code true} if erase is clicked {@code false} otherwise
     */
    public void setErase(boolean isErase) {
        Xfermode xfermode = isErase ? new PorterDuffXfermode(
                PorterDuff.Mode.CLEAR) : null;
        drawPaint.setXfermode(xfermode);
    }

    /**
     * Create new canvas
     */
    public void startNew() {
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }

    /**
     * @return the paint color
     */
    public int getPaintColor() {
        return paintColor;
    }
}
@@ -3496,130 +3496,8 @@
  %7D%0A%0A
-    /**%0A     * @return last brush size%0A     */%0A    public float getLastBrushSize() %7B%0A        return lastBrushSize;%0A    %7D%0A%0A
     

clean up