package me.xiaopan.sketchsample.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import me.xiaopan.sketch.SketchImageView;
import me.xiaopan.sketchsample.util.DeviceUtils;

public class MappingView extends SketchImageView {
    private RectF srcRect;
    private Paint paint;

    public MappingView(Context context) {
        super(context);
        init(context);
    }

    public MappingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MappingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        srcRect = new RectF();
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(DeviceUtils.dp2px(context, 1));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (srcRect.isEmpty()) {
            return;
        }

//        srcRect.set(0, 0, 100, 100);
        canvas.drawRect(srcRect, paint);
    }

    public void update(int sourceWidth, RectF sourceSrcRect) {
        if (sourceWidth == 0) {
            return;
        }
        int selfWidth = getWidth();
        float scale = (float) selfWidth / sourceWidth;
        this.srcRect.set(sourceSrcRect.left * scale, sourceSrcRect.top * scale, sourceSrcRect.right * scale, sourceSrcRect.bottom * scale);
//        Log.d("test", "sourceSrcRect: " + sourceSrcRect.toString() + ", scale: " + scale + ", mapping: " + srcRect.toString());
        invalidate();
    }
}

@@ -210,25 +210,99 @@
 oid.
-util.AttributeSet
+graphics.drawable.Drawable;%0Aimport android.util.AttributeSet;%0Aimport android.view.ViewGroup
 ;%0A%0Ai
@@ -496,16 +496,89 @@
 paint;%0A%0A
+    private int cacheSourceWidth;%0A    private RectF cacheSourceSrcRect;%0A%0A
     publ
@@ -1175,16 +1175,57 @@
 xt, 1));
+%0A%0A        setScaleType(ScaleType.FIT_XY);
 %0A    %7D%0A%0A
@@ -1380,47 +1380,8 @@
  %7D%0A%0A
-//        srcRect.set(0, 0, 100, 100);%0A
     
@@ -1432,98 +1432,1900 @@
     
-public void update(int sourceWidth, RectF sourceSrcRect) %7B%0A        if (sourceWidth == 0) %7B
+@Override%0A    protected void onLayout(boolean changed, int left, int top, int right, int bottom) %7B%0A        super.onLayout(changed, left, top, right, bottom);%0A%0A        if (cacheSourceWidth != 0 && cacheSourceSrcRect != null && !cacheSourceSrcRect.isEmpty()) %7B%0A            update(cacheSourceWidth, cacheSourceSrcRect);%0A            cacheSourceWidth = 0;%0A            cacheSourceSrcRect.setEmpty();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void setImageDrawable(Drawable drawable) %7B%0A        int maxWidth = getResources().getDisplayMetrics().widthPixels / 2;%0A        int maxHeight = getResources().getDisplayMetrics().heightPixels / 2;%0A        int drawableWidth = drawable.getIntrinsicWidth();%0A        int drawableHeight = drawable.getIntrinsicHeight();%0A        if (drawableWidth %3E maxWidth %7C%7C drawableHeight %3E maxHeight) %7B%0A            float finalScale = Math.min((float) maxWidth / drawableWidth, (float) maxHeight / drawableHeight);%0A            ViewGroup.LayoutParams layoutParams = getLayoutParams();%0A            layoutParams.width = (int) (drawableWidth * finalScale);%0A            layoutParams.height = (int) (drawableHeight * finalScale);%0A            setLayoutParams(layoutParams);%0A        %7D%0A%0A        super.setImageDrawable(drawable);%0A    %7D%0A%0A    public void update(int sourceWidth, RectF sourceSrcRect) %7B%0A        if (sourceWidth == 0 %7C%7C sourceSrcRect.isEmpty()) %7B%0A            if (!srcRect.isEmpty()) %7B%0A                srcRect.setEmpty();%0A                invalidate();%0A            %7D%0A            return;%0A        %7D%0A%0A        if (getWidth() == 0 %7C%7C getDrawable() == null) %7B%0A            if (!srcRect.isEmpty()) %7B%0A                srcRect.setEmpty();%0A                invalidate();%0A            %7D%0A%0A            cacheSourceWidth = sourceWidth;%0A            if (cacheSourceSrcRect == null) %7B%0A                cacheSourceSrcRect = new RectF();%0A            %7D%0A            cacheSourceSrcRect.set(sourceSrcRect);
 %0A   
@@ -3343,32 +3343,33 @@
 turn;%0A        %7D%0A
+%0A
         int self
@@ -3587,138 +3587,8 @@
 e);%0A
-//        Log.d(%22test%22, %22sourceSrcRect: %22 + sourceSrcRect.toString() + %22, scale: %22 + scale + %22, mapping: %22 + srcRect.toString());%0A
     

optimize mapping view