package com.jump.canvasdraw;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class HandWrite extends ImageView {
    private Bitmap originalBitmap = null;
    private Bitmap new1Bitmap = null;
    private float clickX = 0, clickY = 0;
    private float startX = 0, startY = 0;
    private int color = Color.WHITE;
    private float strokeWidth = 3.0f;
    private boolean status = false;
    private Paint mPaint;
    private Canvas mCanvas;
    private Path path;

    public HandWrite(Context context) {
        super(context);
        Init();
    }

    public HandWrite(Context context, AttributeSet attrs) {
        super(context, attrs);
        Init();
    }

    public HandWrite(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Init();
    }

    public void Init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿功能
        mPaint.setDither(true);//设定是否使用图像抖动处理
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(color);
        mPaint.setStrokeWidth(strokeWidth);
        path = new Path();
        originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a).copy(Bitmap.Config.ARGB_8888, true);
    }

    public void setBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            setImageBitmap(originalBitmap);
        } else {
            setImageBitmap(bitmap);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                originalBitmap.getWidth(), originalBitmap.getHeight());
        setLayoutParams(layoutParams);
    }

    public void clear() {
//        isClear = true;
        new1Bitmap = Bitmap.createBitmap(originalBitmap);
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(new1Bitmap, 0,
                0, null);
    }

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

    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        path.reset();
        path.moveTo(x, y);
        startX = x;
        startY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - startX);
        float dy = Math.abs(y - startY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            path.quadTo(startX, startY, (x + startX) / 2, (y + startY) / 2);
            mCanvas.drawPath(path, mPaint);
            startX = x;
            startY = y;
        }
    }

    private void touch_up() {
        path.lineTo(startX, startY);
        // commit the path to our offscreen
        mCanvas.drawPath(path, mPaint);
        // kill this so we don't double draw
        path.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        clickX = event.getX();
        clickY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(clickX, clickY);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(clickX, clickY);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
//                HandWriting();
                touch_up();
                invalidate();
                break;
        }
        return true;
    }

    public void setAlpha(int alpha) {
        setPaint();
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        setPaint();
    }

    public void setColor(int color) {
        this.color = color;
        setPaint();
    }

    public void setStatus(boolean status) {
        this.status = status;
        setPaint();
    }

    public void setPaint() {
        mPaint.setColor(color);
        mPaint.setStrokeWidth(strokeWidth);
//        mPaint.setAlpha(alpha);
        if (status) {
            mPaint.setXfermode(new PorterDuffXfermode(
                    PorterDuff.Mode.CLEAR));
        } else {
            mPaint.setXfermode(null);
        }
    }

    public void setmCanvas() {
        new1Bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas.setBitmap(new1Bitmap);
        invalidate();
    }

    public int getColor() {
        return mPaint.getColor();
    }

    public Bitmap MergeBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(new1Bitmap.getWidth(), new1Bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        //开启mDragItemView绘图缓存
        setDrawingCacheEnabled(true);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(getDrawingCache(), 0, 0, null);
        destroyDrawingCache();
        return bitmap;
    }
}
