package net.zombie_sama.imagepainter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class PaintableImageView extends android.support.v7.widget.AppCompatImageView {
    private Paint paint;
    private ScaleDirection direction;
    private Canvas canvas;
    private boolean isPainting;
    private float startX = 0, startY = 0;
    // 缩放比
    private float scale;
    // 源图片
    private Bitmap bitmapSrc;
    // 缓存图片
    private Bitmap bitmapCache;
    // View长宽比
    private float ratioView;
    // Bitmap长宽比
    private float ratioImage;
    // 偏移量
    private float offset;

    public PaintableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3f);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (v == PaintableImageView.this) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            isPainting = false;
                            draw(event);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            draw(event);
                            break;
                        case MotionEvent.ACTION_UP:
                            draw(event);
                            isPainting = false;
                            break;
                    }
                    return true;
                } else {
                    isPainting = false;
                    return false;
                }
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            loadBitmap();
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        loadBitmap();
    }

    private void draw(MotionEvent event) {
        float x = event.getX() / scale;
        float y = event.getY() / scale;
        if (direction == ScaleDirection.height) {
            x -= offset;
        } else {
            y -= offset;
        }
        if (!isPainting) {
            isPainting = true;
            startX = x;
            startY = y;
        } else {
            Log.i("draw", "x:from " + startX + " to " + event.getX() + " y:from " + startY + " to " + event.getY());
            canvas.drawLine(startX, startY, x, y, paint);
            startX = x;
            startY = y;
            super.setImageBitmap(bitmapCache);
        }
    }

    private void loadBitmap() {
        if (bitmapSrc == null) bitmapSrc = ((BitmapDrawable) getDrawable()).getBitmap();
        bitmapCache = bitmapSrc.copy(bitmapSrc.getConfig(), true);
        if (canvas == null) canvas = new Canvas(bitmapCache);
        canvas.setBitmap(bitmapCache);
        super.setImageBitmap(bitmapCache);
        ratioView = (float) getWidth() / (float) getHeight();
        ratioImage = (float) bitmapCache.getWidth() / (float) bitmapCache.getHeight();
        if (ratioView > ratioImage) {
            scale = (float) getHeight() / (float) bitmapCache.getHeight();
            direction = ScaleDirection.height;
            offset = Math.abs(((float) getWidth() - ((float) getWidth() / scale)) / 2);
        } else {
            scale = (float) getWidth() / (float) bitmapCache.getWidth();
            direction = ScaleDirection.width;
            offset = Math.abs(((float) getHeight() - ((float) getHeight() / scale)) / 2);
        }
    }

    /**
     * 设置画笔颜色
     *
     * @param color 颜色
     */
    public void setPaintColor(int color) {
        paint.setColor(color);
    }

    /**
     * 获取画笔颜色
     */
    public int getPaintColor(){
        return paint.getColor();
    }

    /**
     * 设置画笔宽度
     *
     * @param width 宽度
     */
    public void setPaintStrokeWidth(float width) {
        paint.setStrokeWidth(width);
    }

    /**
     * 获取画笔宽度
     */
    public float getPaintStrokeWidth() {
        return paint.getStrokeWidth();
    }

    public Paint getPaint() {
        return paint;
    }

    /**
     * 设置画笔
     *
     * @param paint {@link Paint}
     */
    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    /**
     * 重置图片
     */
    public void rest() {
        setImageBitmap(bitmapSrc);
    }

    /**
     * 获取当前Bitmap
     */
    public Bitmap getResult() {
        return bitmapCache;
    }

    /**
     * 缩放比所基于的方向
     */
    private enum ScaleDirection {
        width, height
    }
}

@@ -610,16 +610,67 @@
 tY = 0;%0A
+    private OnDrawDoneListener onDrawDoneListener;%0A
     // %E7%BC%A9
@@ -672,16 +672,16 @@
  // %E7%BC%A9%E6%94%BE%E6%AF%94%0A
-
     priv
@@ -1010,16 +1010,25 @@
  attrs);
+%0A        
 paint = 
@@ -1790,32 +1790,200 @@
 inting = false;%0A
+                            if (onDrawDoneListener != null) %7B%0A                                onDrawDoneListener.onDrawDone(bitmapCache);%0A                            %7D%0A
                 
@@ -4362,16 +4362,17 @@
 tColor()
+ 
 %7B%0A      
@@ -5143,16 +5143,16 @@
 ction %7B%0A
-
         
@@ -5164,16 +5164,316 @@
 , height
+;%0A    %7D%0A%0A    /**%0A     * %E8%AE%BE%E7%BD%AE%E7%94%BB%E5%AE%8C%E4%B8%80%E7%AC%94%E6%97%B6%E7%9A%84%E5%9B%9E%E8%B0%83%0A     *%0A     * @param onDrawDoneListener %E5%9B%9E%E8%B0%83%0A     */%0A    public void setOnDrawDoneListener(OnDrawDoneListener onDrawDoneListener) %7B%0A        this.onDrawDoneListener = onDrawDoneListener;%0A    %7D%0A%0A    interface OnDrawDoneListener %7B%0A        void onDrawDone(Bitmap bitmap);
 %0A    %7D%0A%7D

Add callback when draw a line done