package com.ifpdos.factorytest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/* JADX INFO: loaded from: classes.dex */
public class ZoomImageView extends View {
    public static final int STATUS_INIT = 1;
    public static final int STATUS_MOVE = 4;
    public static final int STATUS_ZOOM_IN = 3;
    public static final int STATUS_ZOOM_OUT = 2;
    private float centerPointX;
    private float centerPointY;
    private float currentBitmapHeight;
    private float currentBitmapWidth;
    private int currentStatus;
    private int height;
    private float initRatio;
    private double lastFingerDis;
    private float lastXMove;
    private float lastYMove;
    private Matrix matrix;
    private float movedDistanceX;
    private float movedDistanceY;
    private float scaledRatio;
    private Bitmap sourceBitmap;
    private float totalRatio;
    private float totalTranslateX;
    private float totalTranslateY;
    private int width;

    public ZoomImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.matrix = new Matrix();
        this.lastXMove = -1.0f;
        this.lastYMove = -1.0f;
        this.currentStatus = 1;
    }

    public void setImageBitmap(Bitmap bitmap) {
        this.sourceBitmap = bitmap;
        invalidate();
    }

    @Override // android.view.View
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
        if (z) {
            this.width = getWidth();
            this.height = getHeight();
        }
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int actionMasked = motionEvent.getActionMasked();
        if (actionMasked == 1) {
            this.lastXMove = -1.0f;
            this.lastYMove = -1.0f;
        } else if (actionMasked != 2) {
            if (actionMasked == 5) {
                if (motionEvent.getPointerCount() == 2) {
                    this.lastFingerDis = distanceBetweenFingers(motionEvent);
                }
            } else if (actionMasked == 6 && motionEvent.getPointerCount() == 2) {
                this.lastXMove = -1.0f;
                this.lastYMove = -1.0f;
            }
        } else if (motionEvent.getPointerCount() == 1) {
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            if (this.lastXMove == -1.0f && this.lastYMove == -1.0f) {
                this.lastXMove = x;
                this.lastYMove = y;
            }
            this.currentStatus = 4;
            float f = x - this.lastXMove;
            this.movedDistanceX = f;
            float f2 = y - this.lastYMove;
            this.movedDistanceY = f2;
            float f3 = this.totalTranslateX;
            if (f3 + f > 0.0f || this.width - (f3 + f) > this.currentBitmapWidth) {
                this.movedDistanceX = 0.0f;
            }
            float f4 = this.totalTranslateY;
            if (f4 + f2 > 0.0f || this.height - (f4 + f2) > this.currentBitmapHeight) {
                this.movedDistanceY = 0.0f;
            }
            invalidate();
            this.lastXMove = x;
            this.lastYMove = y;
        } else if (motionEvent.getPointerCount() == 2) {
            centerPointBetweenFingers(motionEvent);
            double dDistanceBetweenFingers = distanceBetweenFingers(motionEvent);
            double d = this.lastFingerDis;
            if (dDistanceBetweenFingers > d) {
                this.currentStatus = 2;
            } else {
                this.currentStatus = 3;
            }
            int i = this.currentStatus;
            if ((i == 2 && this.totalRatio < this.initRatio * 4.0f) || (i == 3 && this.totalRatio > this.initRatio)) {
                float f5 = (float) (dDistanceBetweenFingers / d);
                this.scaledRatio = f5;
                float f6 = this.totalRatio * f5;
                this.totalRatio = f6;
                float f7 = this.initRatio;
                if (f6 > f7 * 4.0f) {
                    this.totalRatio = f7 * 4.0f;
                } else if (f6 < f7) {
                    this.totalRatio = f7;
                }
                invalidate();
                this.lastFingerDis = dDistanceBetweenFingers;
            }
        }
        return true;
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int i = this.currentStatus;
        if (i == 1) {
            initBitmap(canvas);
        } else if (i == 2 || i == 3) {
            zoom(canvas);
            return;
        } else if (i == 4) {
            move(canvas);
            return;
        }
        canvas.drawBitmap(this.sourceBitmap, this.matrix, null);
    }

    private void zoom(Canvas canvas) {
        float f;
        this.matrix.reset();
        Matrix matrix = this.matrix;
        float f2 = this.totalRatio;
        matrix.postScale(f2, f2);
        float width = this.sourceBitmap.getWidth() * this.totalRatio;
        float height = this.sourceBitmap.getHeight() * this.totalRatio;
        float f3 = this.currentBitmapWidth;
        int i = this.width;
        float f4 = 0.0f;
        if (f3 < i) {
            f = (i - width) / 2.0f;
        } else {
            float f5 = this.totalTranslateX;
            float f6 = this.scaledRatio;
            f = (f5 * f6) + (this.centerPointX * (1.0f - f6));
            if (f > 0.0f) {
                f = 0.0f;
            } else if (i - f > width) {
                f = i - width;
            }
        }
        float f7 = this.currentBitmapHeight;
        int i2 = this.height;
        if (f7 < i2) {
            f4 = (i2 - height) / 2.0f;
        } else {
            float f8 = this.totalTranslateY;
            float f9 = this.scaledRatio;
            float f10 = (f8 * f9) + (this.centerPointY * (1.0f - f9));
            if (f10 <= 0.0f) {
                f4 = ((float) i2) - f10 > height ? i2 - height : f10;
            }
        }
        this.matrix.postTranslate(f, f4);
        this.totalTranslateX = f;
        this.totalTranslateY = f4;
        this.currentBitmapWidth = width;
        this.currentBitmapHeight = height;
        canvas.drawBitmap(this.sourceBitmap, this.matrix, null);
    }

    private void move(Canvas canvas) {
        this.matrix.reset();
        float f = this.totalTranslateX + this.movedDistanceX;
        float f2 = this.totalTranslateY + this.movedDistanceY;
        Matrix matrix = this.matrix;
        float f3 = this.totalRatio;
        matrix.postScale(f3, f3);
        this.matrix.postTranslate(f, f2);
        this.totalTranslateX = f;
        this.totalTranslateY = f2;
        canvas.drawBitmap(this.sourceBitmap, this.matrix, null);
    }

    private void initBitmap(Canvas canvas) {
        if (this.sourceBitmap != null) {
            this.matrix.reset();
            int width = this.sourceBitmap.getWidth();
            int height = this.sourceBitmap.getHeight();
            int i = this.width;
            int i2 = width - i;
            int i3 = this.height;
            if (i2 > height - i3) {
                float f = i / (width * 1.0f);
                this.matrix.postScale(f, f);
                float f2 = (this.height - (height * f)) / 2.0f;
                this.matrix.postTranslate(0.0f, f2);
                this.totalTranslateY = f2;
                this.initRatio = f;
                this.totalRatio = f;
            } else {
                float f3 = i3 / (height * 1.0f);
                this.matrix.postScale(f3, f3);
                float f4 = (this.width - (width * f3)) / 2.0f;
                this.matrix.postTranslate(f4, 0.0f);
                this.totalTranslateX = f4;
                this.initRatio = f3;
                this.totalRatio = f3;
            }
            float f5 = this.initRatio;
            this.currentBitmapWidth = width * f5;
            this.currentBitmapHeight = height * f5;
            canvas.drawBitmap(this.sourceBitmap, this.matrix, null);
        }
    }

    private double distanceBetweenFingers(MotionEvent motionEvent) {
        float fAbs = Math.abs(motionEvent.getX(0) - motionEvent.getX(1));
        float fAbs2 = Math.abs(motionEvent.getY(0) - motionEvent.getY(1));
        return Math.sqrt((fAbs * fAbs) + (fAbs2 * fAbs2));
    }

    private void centerPointBetweenFingers(MotionEvent motionEvent) {
        float x = motionEvent.getX(0);
        float y = motionEvent.getY(0);
        float x2 = motionEvent.getX(1);
        float y2 = motionEvent.getY(1);
        this.centerPointX = (x + x2) / 2.0f;
        this.centerPointY = (y + y2) / 2.0f;
    }
}
