<commit_before>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();
    }
}
<commit_msg>optimize mapping view<commit_after>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.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;

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

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

    private int cacheSourceWidth;
    private RectF cacheSourceSrcRect;

    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));

        setScaleType(ScaleType.FIT_XY);
    }

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

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

        canvas.drawRect(srcRect, paint);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (cacheSourceWidth != 0 && cacheSourceSrcRect != null && !cacheSourceSrcRect.isEmpty()) {
            update(cacheSourceWidth, cacheSourceSrcRect);
            cacheSourceWidth = 0;
            cacheSourceSrcRect.setEmpty();
        }
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        int maxWidth = getResources().getDisplayMetrics().widthPixels / 2;
        int maxHeight = getResources().getDisplayMetrics().heightPixels / 2;
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();
        if (drawableWidth > maxWidth || drawableHeight > maxHeight) {
            float finalScale = Math.min((float) maxWidth / drawableWidth, (float) maxHeight / drawableHeight);
            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            layoutParams.width = (int) (drawableWidth * finalScale);
            layoutParams.height = (int) (drawableHeight * finalScale);
            setLayoutParams(layoutParams);
        }

        super.setImageDrawable(drawable);
    }

    public void update(int sourceWidth, RectF sourceSrcRect) {
        if (sourceWidth == 0 || sourceSrcRect.isEmpty()) {
            if (!srcRect.isEmpty()) {
                srcRect.setEmpty();
                invalidate();
            }
            return;
        }

        if (getWidth() == 0 || getDrawable() == null) {
            if (!srcRect.isEmpty()) {
                srcRect.setEmpty();
                invalidate();
            }

            cacheSourceWidth = sourceWidth;
            if (cacheSourceSrcRect == null) {
                cacheSourceSrcRect = new RectF();
            }
            cacheSourceSrcRect.set(sourceSrcRect);
            return;
        }

        int selfWidth = getWidth();
        float scale = (float) selfWidth / sourceWidth;
        this.srcRect.set(sourceSrcRect.left * scale, sourceSrcRect.top * scale, sourceSrcRect.right * scale, sourceSrcRect.bottom * scale);
        invalidate();
    }
}

{'fraction_non_alphanumeric': 0.1052919708029197, 'fraction_numerical': 0.003467153284671533, 'mean_word_length': 2.5800130633572826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3140979', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1504, 'n_words': 332}