CropImageView.java 源代码


package activity.CustomView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.kwad.sdk.core.response.model.AdVideoPreCacheConfig;
import com.ss.android.socialbase.downloader.constants.DownloadErrorCode;
import org.apache.poi.hssf.record.BOFRecord;
import org.mozilla.universalchardet.prober.HebrewProber;

public class CropImageView extends View {
    private final int EDGE_LB;
    private final int EDGE_LT;
    private final int EDGE_MOVE_IN;
    private final int EDGE_MOVE_OUT;
    private final int EDGE_NONE;
    private final int EDGE_RB;
    private final int EDGE_RT;
    private final int STATUS_MULTI_START;
    private final int STATUS_MULTI_TOUCHING;
    private final int STATUS_SINGLE;
    private int X;
    private int Y;
    private int cropHeight;
    private int cropWidth;
    public int currentEdge;
    protected boolean isFirst;
    private boolean isTouchInSquare;
    private topbarClickListener listener;
    protected Context mContext;
    protected Drawable mDrawable;
    protected Rect mDrawableDst;
    protected Rect mDrawableFloat;
    protected Rect mDrawableSrc;
    protected FloatDrawable mFloatDrawable;
    public int mMinFloatHeight;
    public int mMinFloatWidth;
    private int mStatus;
    private float mX_1;
    private float mY_1;
    public int mainSteamHeight;
    public int mainSteamWidth;
    protected float oriRationWH;

    public interface topbarClickListener {
        void TouchDownClick();

        void TouchUpClick();
    }

    public void setTopbarClickListener(topbarClickListener topbarclicklistener) {
        this.listener = topbarclicklistener;
    }

    public CropImageView(Context context) {
        super(context);
        this.mX_1 = 0.0f;
        this.mY_1 = 0.0f;
        this.STATUS_SINGLE = 1;
        this.STATUS_MULTI_START = 2;
        this.STATUS_MULTI_TOUCHING = 3;
        this.mStatus = 1;
        this.EDGE_LT = 1;
        this.EDGE_RT = 2;
        this.EDGE_LB = 3;
        this.EDGE_RB = 4;
        this.EDGE_MOVE_IN = 5;
        this.EDGE_MOVE_OUT = 6;
        this.EDGE_NONE = 7;
        this.currentEdge = 7;
        this.oriRationWH = 0.0f;
        this.mDrawableSrc = new Rect();
        this.mDrawableDst = new Rect();
        this.mDrawableFloat = new Rect();
        this.isFirst = true;
        this.isTouchInSquare = true;
        init(context);
    }

    public CropImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mX_1 = 0.0f;
        this.mY_1 = 0.0f;
        this.STATUS_SINGLE = 1;
        this.STATUS_MULTI_START = 2;
        this.STATUS_MULTI_TOUCHING = 3;
        this.mStatus = 1;
        this.EDGE_LT = 1;
        this.EDGE_RT = 2;
        this.EDGE_LB = 3;
        this.EDGE_RB = 4;
        this.EDGE_MOVE_IN = 5;
        this.EDGE_MOVE_OUT = 6;
        this.EDGE_NONE = 7;
        this.currentEdge = 7;
        this.oriRationWH = 0.0f;
        this.mDrawableSrc = new Rect();
        this.mDrawableDst = new Rect();
        this.mDrawableFloat = new Rect();
        this.isFirst = true;
        this.isTouchInSquare = true;
        init(context);
    }

    public CropImageView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mX_1 = 0.0f;
        this.mY_1 = 0.0f;
        this.STATUS_SINGLE = 1;
        this.STATUS_MULTI_START = 2;
        this.STATUS_MULTI_TOUCHING = 3;
        this.mStatus = 1;
        this.EDGE_LT = 1;
        this.EDGE_RT = 2;
        this.EDGE_LB = 3;
        this.EDGE_RB = 4;
        this.EDGE_MOVE_IN = 5;
        this.EDGE_MOVE_OUT = 6;
        this.EDGE_NONE = 7;
        this.currentEdge = 7;
        this.oriRationWH = 0.0f;
        this.mDrawableSrc = new Rect();
        this.mDrawableDst = new Rect();
        this.mDrawableFloat = new Rect();
        this.isFirst = true;
        this.isTouchInSquare = true;
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        try {
            if (Build.VERSION.SDK_INT >= 11) {
                setLayerType(1, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.mFloatDrawable = new FloatDrawable(context);
    }

    public void setDrawable(Bitmap bitmap, int i, int i2, int i3, int i4) {
        this.mDrawable = new BitmapDrawable(bitmap);
        this.cropWidth = i;
        this.cropHeight = i2;
        this.isFirst = true;
        this.X = i3;
        this.Y = i4;
        invalidate();
    }

    @Override
    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        setMeasuredDimension(View.MeasureSpec.getSize(i), View.MeasureSpec.getSize(i2));
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        Log.e("setTopbarClickListener", "dispatchTouchEvent CropImageView");
        if (motionEvent.getPointerCount() > 1) {
            int i = this.mStatus;
            if (i == 1) {
                this.mStatus = 2;
            } else if (i == 2) {
                this.mStatus = 3;
            }
        } else {
            int i2 = this.mStatus;
            if (i2 == 2 || i2 == 3) {
                this.mX_1 = motionEvent.getX();
                this.mY_1 = motionEvent.getY();
            }
            this.mStatus = 1;
        }
        int action = motionEvent.getAction();
        if (action == 0) {
            topbarClickListener topbarclicklistener = this.listener;
            if (topbarclicklistener != null) {
                topbarclicklistener.TouchDownClick();
            }
            this.mX_1 = motionEvent.getX();
            float y = motionEvent.getY();
            this.mY_1 = y;
            this.currentEdge = getTouch((int) this.mX_1, (int) y);
        } else if (action == 1) {
            topbarClickListener topbarclicklistener2 = this.listener;
            if (topbarclicklistener2 != null) {
                topbarclicklistener2.TouchUpClick();
            }
        } else if (action == 2) {
            int i3 = this.mStatus;
            if (i3 != 3 && i3 == 1) {
                int x = (int) (motionEvent.getX() - this.mX_1);
                int y2 = (int) (motionEvent.getY() - this.mY_1);
                this.mX_1 = motionEvent.getX();
                this.mY_1 = motionEvent.getY();
                if (x != 0 || y2 != 0) {
                    int i4 = this.currentEdge;
                    if (i4 == 1) {
                        if (this.mDrawableFloat.right - (this.mDrawableFloat.left + x) <= this.mMinFloatWidth) {
                            x = 0;
                        }
                        if (this.mDrawableFloat.bottom - (this.mDrawableFloat.top + y2) <= this.mMinFloatHeight) {
                            y2 = 0;
                        }
                        Rect rect = this.mDrawableFloat;
                        rect.set(rect.left + x, this.mDrawableFloat.top + y2, this.mDrawableFloat.right, this.mDrawableFloat.bottom);
                    } else if (i4 == 2) {
                        if ((this.mDrawableFloat.right + x) - this.mDrawableFloat.left <= this.mMinFloatWidth) {
                            x = 0;
                        }
                        if (this.mDrawableFloat.bottom - (this.mDrawableFloat.top + y2) <= this.mMinFloatHeight) {
                            y2 = 0;
                        }
                        Rect rect2 = this.mDrawableFloat;
                        rect2.set(rect2.left, this.mDrawableFloat.top + y2, this.mDrawableFloat.right + x, this.mDrawableFloat.bottom);
                    } else if (i4 == 3) {
                        if (this.mDrawableFloat.right - (this.mDrawableFloat.left + x) <= this.mMinFloatWidth) {
                            x = 0;
                        }
                        if ((this.mDrawableFloat.bottom + y2) - this.mDrawableFloat.top <= this.mMinFloatHeight) {
                            y2 = 0;
                        }
                        Rect rect3 = this.mDrawableFloat;
                        rect3.set(rect3.left + x, this.mDrawableFloat.top, this.mDrawableFloat.right, this.mDrawableFloat.bottom + y2);
                    } else if (i4 == 4) {
                        if ((this.mDrawableFloat.right + x) - this.mDrawableFloat.left <= this.mMinFloatWidth) {
                            x = 0;
                        }
                        if ((this.mDrawableFloat.bottom + y2) - this.mDrawableFloat.top <= this.mMinFloatHeight) {
                            y2 = 0;
                        }
                        Rect rect4 = this.mDrawableFloat;
                        rect4.set(rect4.left, this.mDrawableFloat.top, this.mDrawableFloat.right + x, this.mDrawableFloat.bottom + y2);
                    } else if (i4 == 5) {
                        boolean contains = this.mDrawableFloat.contains((int) motionEvent.getX(), (int) motionEvent.getY());
                        this.isTouchInSquare = contains;
                        if (contains) {
                            if (this.mDrawableFloat.right + x >= getWidth()) {
                                x = 0;
                            }
                            if (this.mDrawableFloat.left + x <= 0) {
                                x = 0;
                            }
                            if (this.mDrawableFloat.top + y2 <= 0) {
                                y2 = 0;
                            }
                            this.mDrawableFloat.offset(x, this.mDrawableFloat.bottom + y2 < getHeight() ? y2 : 0);
                        }
                    }
                    this.mDrawableFloat.sort();
                    invalidate();
                }
            }
        } else if (action == 6) {
            topbarClickListener topbarclicklistener3 = this.listener;
            if (topbarclicklistener3 != null) {
                topbarclicklistener3.TouchUpClick();
            }
            this.currentEdge = 7;
        }
        return true;
    }

    public int getTouch(int i, int i2) {
        Rect bounds = this.mFloatDrawable.getBounds();
        int borderWidth = this.mFloatDrawable.getBorderWidth();
        int borderHeight = this.mFloatDrawable.getBorderHeight();
        if (bounds.left <= i && i < bounds.left + borderWidth && bounds.top <= i2 && i2 < bounds.top + borderHeight) {
            return 1;
        }
        if (bounds.right - borderWidth <= i && i < bounds.right && bounds.top <= i2 && i2 < bounds.top + borderHeight) {
            return 2;
        }
        if (bounds.left <= i && i < bounds.left + borderWidth && bounds.bottom - borderHeight <= i2 && i2 < bounds.bottom) {
            return 3;
        }
        if (bounds.right - borderWidth > i || i >= bounds.right || bounds.bottom - borderHeight > i2 || i2 >= bounds.bottom) {
            return bounds.contains(i, i2) ? 5 : 6;
        }
        return 4;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = this.mDrawable;
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || this.mDrawable.getIntrinsicHeight() == 0) {
            return;
        }
        configureBounds();
        this.mDrawable.draw(canvas);
        canvas.save();
        canvas.clipRect(this.mDrawableFloat, Region.Op.DIFFERENCE);
        canvas.drawColor(Color.parseColor("#44000000"));
        canvas.restore();
        this.mFloatDrawable.draw(canvas);
    }

    protected void configureBounds() {
        if (this.isFirst) {
            this.mDrawableSrc.set(0, 0, getWidth(), getHeight());
            this.mDrawableDst.set(this.mDrawableSrc);
            int width = (this.X * getWidth()) / this.mainSteamWidth;
            int height = (this.Y * getHeight()) / this.mainSteamHeight;
            this.mDrawableFloat.set(width, height, ((getWidth() * this.cropWidth) / this.mainSteamWidth) + width, ((getHeight() * this.cropHeight) / this.mainSteamHeight) + height);
            this.isFirst = false;
        } else if (getTouch((int) this.mX_1, (int) this.mY_1) == 5) {
            if (this.mDrawableFloat.left < 0) {
                Rect rect = this.mDrawableFloat;
                rect.right = rect.width();
                this.mDrawableFloat.left = 0;
            }
            if (this.mDrawableFloat.top < 0) {
                Rect rect2 = this.mDrawableFloat;
                rect2.bottom = rect2.height();
                this.mDrawableFloat.top = 0;
            }
            if (this.mDrawableFloat.right > getWidth()) {
                this.mDrawableFloat.left = getWidth() - this.mDrawableFloat.width();
                this.mDrawableFloat.right = getWidth();
            }
            if (this.mDrawableFloat.bottom > getHeight()) {
                this.mDrawableFloat.top = getHeight() - this.mDrawableFloat.height();
                this.mDrawableFloat.bottom = getHeight();
            }
            Rect rect3 = this.mDrawableFloat;
            rect3.set(rect3.left, this.mDrawableFloat.top, this.mDrawableFloat.right, this.mDrawableFloat.bottom);
        } else {
            if (this.mDrawableFloat.left < 0) {
                this.mDrawableFloat.left = 0;
            }
            if (this.mDrawableFloat.top < 0) {
                this.mDrawableFloat.top = 0;
            }
            if (this.mDrawableFloat.right > getWidth()) {
                this.mDrawableFloat.right = getWidth();
                this.mDrawableFloat.left = getWidth() - this.mDrawableFloat.width();
            }
            if (this.mDrawableFloat.bottom > getHeight()) {
                this.mDrawableFloat.bottom = getHeight();
                this.mDrawableFloat.top = getHeight() - this.mDrawableFloat.height();
            }
            Rect rect4 = this.mDrawableFloat;
            rect4.set(rect4.left, this.mDrawableFloat.top, this.mDrawableFloat.right, this.mDrawableFloat.bottom);
        }
        this.mDrawable.setBounds(this.mDrawableDst);
        this.mFloatDrawable.setBounds(this.mDrawableFloat);
    }

    public Bitmap getCropImage() {
        Bitmap createBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
        this.mDrawable.draw(new Canvas(createBitmap));
        Matrix matrix = new Matrix();
        float width = this.mDrawableSrc.width() / this.mDrawableDst.width();
        matrix.postScale(width, width);
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, this.mDrawableFloat.left, this.mDrawableFloat.top, this.mDrawableFloat.width(), this.mDrawableFloat.height(), matrix, true);
        createBitmap.recycle();
        return createBitmap2;
    }

    public int dipToPx(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public void SetMainStream(int i, int i2) {
        this.mainSteamWidth = i;
        this.mainSteamHeight = i2;
        if (i <= 0 || i2 <= 0) {
            if (this.mContext instanceof Activity) {
                ((Activity) this.mContext).getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
                int i3 = (int) (r3.widthPixels * 0.9d);
                this.mainSteamWidth = i3;
                this.mainSteamHeight = (int) (i3 / 1.5d);
            } else {
                this.mainSteamWidth = 1920;
                this.mainSteamHeight = DownloadErrorCode.ERROR_TTNET_NOT_MODIFIED;
            }
        }
        invalidate();
    }

    public void getMainSteam(int i) {
        if (i == 0) {
            this.mainSteamWidth = 640;
            this.mainSteamHeight = 480;
        } else if (i == 1) {
            this.mainSteamWidth = 320;
            this.mainSteamHeight = HebrewProber.NORMAL_NUN;
        } else if (i == 2) {
            this.mainSteamWidth = 160;
            this.mainSteamHeight = 112;
        } else if (i == 3) {
            this.mainSteamWidth = 704;
            this.mainSteamHeight = 576;
        } else if (i == 4) {
            this.mainSteamWidth = 352;
            this.mainSteamHeight = 288;
        } else if (i == 6) {
            this.mainSteamWidth = 1280;
            this.mainSteamHeight = 720;
        } else if (i == 7) {
            this.mainSteamWidth = 640;
            this.mainSteamHeight = 352;
        } else if (i == 8) {
            this.mainSteamWidth = 320;
            this.mainSteamHeight = 176;
        } else {
            switch (i) {
                case 13:
                    this.mainSteamWidth = 1920;
                    this.mainSteamHeight = DownloadErrorCode.ERROR_TTNET_NOT_MODIFIED;
                    break;
                case 14:
                    this.mainSteamWidth = 1280;
                    this.mainSteamHeight = 960;
                    break;
                case 15:
                    this.mainSteamWidth = BOFRecord.VERSION;
                    this.mainSteamHeight = BOFRecord.VERSION;
                    break;
                case 16:
                    this.mainSteamWidth = 2560;
                    this.mainSteamHeight = 1440;
                    break;
                case 17:
                    this.mainSteamWidth = AdVideoPreCacheConfig.DEFAULT_PRE_CACHE_SIZE;
                    this.mainSteamHeight = 448;
                    break;
                case 18:
                    this.mainSteamWidth = AdVideoPreCacheConfig.DEFAULT_PRE_CACHE_SIZE;
                    this.mainSteamHeight = 600;
                    break;
                case 19:
                    this.mainSteamWidth = 2304;
                    this.mainSteamHeight = 1296;
                    break;
                case 20:
                    this.mainSteamWidth = 2560;
                    this.mainSteamHeight = 1920;
                    break;
                case 21:
                    this.mainSteamWidth = 640;
                    this.mainSteamHeight = 360;
                    break;
                case 22:
                    this.mainSteamWidth = 3840;
                    this.mainSteamHeight = 2160;
                    break;
                case 23:
                    this.mainSteamWidth = 2880;
                    this.mainSteamHeight = 1620;
                    break;
                case 24:
                    this.mainSteamWidth = 3840;
                    this.mainSteamHeight = DownloadErrorCode.ERROR_TTNET_NOT_MODIFIED;
                    break;
                case 25:
                    this.mainSteamWidth = 1280;
                    this.mainSteamHeight = 360;
                    break;
                default:
                    if (this.mContext instanceof Activity) {
                        ((Activity) this.mContext).getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
                        int i2 = (int) (r9.widthPixels * 0.9d);
                        this.mainSteamWidth = i2;
                        this.mainSteamHeight = (int) (i2 / 1.5d);
                        break;
                    } else {
                        this.mainSteamWidth = 1920;
                        this.mainSteamHeight = DownloadErrorCode.ERROR_TTNET_NOT_MODIFIED;
                        break;
                    }
            }
        }
        invalidate();
    }

    public void setArea(int i) {
        if (i == 1) {
            this.mDrawableFloat.set(0, 0, getWidth() / 2, getHeight());
        } else if (i == 2) {
            this.mDrawableFloat.set(0, 0, getWidth(), getHeight());
        } else if (i == 3) {
            this.mDrawableFloat.set(getWidth() / 2, 0, getWidth(), getHeight());
        }
        invalidate();
    }

    public int getmDrawableFloatX() {
        return (this.mainSteamWidth * this.mDrawableFloat.left) / getWidth();
    }

    public int getmDrawableFloatY() {
        return (this.mainSteamHeight * this.mDrawableFloat.top) / getHeight();
    }

    public int getmDrawableFloatWidth() {
        return (this.mainSteamWidth * this.mDrawableFloat.width()) / getWidth();
    }

    public int getmDrawableFloatHeight() {
        return (this.mainSteamHeight * this.mDrawableFloat.height()) / getHeight();
    }
}