161 lines
5.8 KiB
Java
161 lines
5.8 KiB
Java
package androidx.transition;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.graphics.Matrix;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.view.ViewParent;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
public class ViewUtilsBase {
|
|
private static final String TAG = "ViewUtilsBase";
|
|
private static final int VISIBILITY_MASK = 12;
|
|
private static boolean sSetFrameFetched;
|
|
private static Method sSetFrameMethod;
|
|
private static Field sViewFlagsField;
|
|
private static boolean sViewFlagsFieldFetched;
|
|
private float[] mMatrixValues;
|
|
|
|
@SuppressLint({"PrivateApi"})
|
|
private void fetchSetFrame() {
|
|
if (!sSetFrameFetched) {
|
|
try {
|
|
Class cls = Integer.TYPE;
|
|
Method declaredMethod = View.class.getDeclaredMethod("setFrame", cls, cls, cls, cls);
|
|
sSetFrameMethod = declaredMethod;
|
|
declaredMethod.setAccessible(true);
|
|
} catch (NoSuchMethodException e) {
|
|
Log.i("ViewUtilsBase", "Failed to retrieve setFrame method", e);
|
|
}
|
|
sSetFrameFetched = true;
|
|
}
|
|
}
|
|
|
|
public void clearNonTransitionAlpha(@NonNull View view) {
|
|
if (view.getVisibility() == 0) {
|
|
view.setTag(R.id.save_non_transition_alpha, null);
|
|
}
|
|
}
|
|
|
|
public float getTransitionAlpha(@NonNull View view) {
|
|
Float f = (Float) view.getTag(R.id.save_non_transition_alpha);
|
|
return f != null ? view.getAlpha() / f.floatValue() : view.getAlpha();
|
|
}
|
|
|
|
public void saveNonTransitionAlpha(@NonNull View view) {
|
|
int i = R.id.save_non_transition_alpha;
|
|
if (view.getTag(i) == null) {
|
|
view.setTag(i, Float.valueOf(view.getAlpha()));
|
|
}
|
|
}
|
|
|
|
public void setAnimationMatrix(@NonNull View view, @Nullable Matrix matrix) {
|
|
if (matrix == null || matrix.isIdentity()) {
|
|
view.setPivotX((float) (view.getWidth() / 2));
|
|
view.setPivotY((float) (view.getHeight() / 2));
|
|
view.setTranslationX(0.0f);
|
|
view.setTranslationY(0.0f);
|
|
view.setScaleX(1.0f);
|
|
view.setScaleY(1.0f);
|
|
view.setRotation(0.0f);
|
|
return;
|
|
}
|
|
float[] fArr = this.mMatrixValues;
|
|
if (fArr == null) {
|
|
fArr = new float[9];
|
|
this.mMatrixValues = fArr;
|
|
}
|
|
matrix.getValues(fArr);
|
|
float f = fArr[3];
|
|
float sqrt = ((float) Math.sqrt((double) (1.0f - (f * f)))) * ((float) (fArr[0] < 0.0f ? -1 : 1));
|
|
float degrees = (float) Math.toDegrees(Math.atan2((double) f, (double) sqrt));
|
|
float f2 = fArr[0] / sqrt;
|
|
float f3 = fArr[4] / sqrt;
|
|
float f4 = fArr[2];
|
|
float f5 = fArr[5];
|
|
view.setPivotX(0.0f);
|
|
view.setPivotY(0.0f);
|
|
view.setTranslationX(f4);
|
|
view.setTranslationY(f5);
|
|
view.setRotation(degrees);
|
|
view.setScaleX(f2);
|
|
view.setScaleY(f3);
|
|
}
|
|
|
|
public void setLeftTopRightBottom(@NonNull View view, int i, int i2, int i3, int i4) {
|
|
fetchSetFrame();
|
|
Method method = sSetFrameMethod;
|
|
if (method != null) {
|
|
try {
|
|
method.invoke(view, Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), Integer.valueOf(i4));
|
|
} catch (IllegalAccessException unused) {
|
|
} catch (InvocationTargetException e) {
|
|
throw new RuntimeException(e.getCause());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setTransitionAlpha(@NonNull View view, float f) {
|
|
Float f2 = (Float) view.getTag(R.id.save_non_transition_alpha);
|
|
if (f2 != null) {
|
|
view.setAlpha(f2.floatValue() * f);
|
|
} else {
|
|
view.setAlpha(f);
|
|
}
|
|
}
|
|
|
|
public void setTransitionVisibility(@NonNull View view, int i) {
|
|
if (!sViewFlagsFieldFetched) {
|
|
try {
|
|
Field declaredField = View.class.getDeclaredField("mViewFlags");
|
|
sViewFlagsField = declaredField;
|
|
declaredField.setAccessible(true);
|
|
} catch (NoSuchFieldException unused) {
|
|
Log.i("ViewUtilsBase", "fetchViewFlagsField: ");
|
|
}
|
|
sViewFlagsFieldFetched = true;
|
|
}
|
|
Field field = sViewFlagsField;
|
|
if (field != null) {
|
|
try {
|
|
sViewFlagsField.setInt(view, i | (field.getInt(view) & -13));
|
|
} catch (IllegalAccessException unused2) {
|
|
}
|
|
}
|
|
}
|
|
|
|
public void transformMatrixToGlobal(@NonNull View view, @NonNull Matrix matrix) {
|
|
ViewParent parent = view.getParent();
|
|
if (parent instanceof View) {
|
|
View view2 = (View) parent;
|
|
transformMatrixToGlobal(view2, matrix);
|
|
matrix.preTranslate((float) (-view2.getScrollX()), (float) (-view2.getScrollY()));
|
|
}
|
|
matrix.preTranslate((float) view.getLeft(), (float) view.getTop());
|
|
Matrix matrix2 = view.getMatrix();
|
|
if (!matrix2.isIdentity()) {
|
|
matrix.preConcat(matrix2);
|
|
}
|
|
}
|
|
|
|
public void transformMatrixToLocal(@NonNull View view, @NonNull Matrix matrix) {
|
|
ViewParent parent = view.getParent();
|
|
if (parent instanceof View) {
|
|
View view2 = (View) parent;
|
|
transformMatrixToLocal(view2, matrix);
|
|
matrix.postTranslate((float) view2.getScrollX(), (float) view2.getScrollY());
|
|
}
|
|
matrix.postTranslate((float) (-view.getLeft()), (float) (-view.getTop()));
|
|
Matrix matrix2 = view.getMatrix();
|
|
if (!matrix2.isIdentity()) {
|
|
Matrix matrix3 = new Matrix();
|
|
if (matrix2.invert(matrix3)) {
|
|
matrix.postConcat(matrix3);
|
|
}
|
|
}
|
|
}
|
|
}
|