1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.transition; 18 19 import android.graphics.Matrix; 20 import android.view.View; 21 import android.view.ViewParent; 22 23 import androidx.annotation.NonNull; 24 25 class ViewUtilsBase { 26 27 private float[] mMatrixValues; 28 29 public void setTransitionAlpha(@NonNull View view, float alpha) { 30 Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha); 31 if (savedAlpha != null) { 32 view.setAlpha(savedAlpha * alpha); 33 } else { 34 view.setAlpha(alpha); 35 } 36 } 37 38 public float getTransitionAlpha(@NonNull View view) { 39 Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha); 40 if (savedAlpha != null) { 41 return view.getAlpha() / savedAlpha; 42 } else { 43 return view.getAlpha(); 44 } 45 } 46 47 public void saveNonTransitionAlpha(@NonNull View view) { 48 if (view.getTag(R.id.save_non_transition_alpha) == null) { 49 view.setTag(R.id.save_non_transition_alpha, view.getAlpha()); 50 } 51 } 52 53 public void clearNonTransitionAlpha(@NonNull View view) { 54 // We don't clear the saved value when the view is hidden; that's the situation we are 55 // saving this value for. 56 if (view.getVisibility() == View.VISIBLE) { 57 view.setTag(R.id.save_non_transition_alpha, null); 58 } 59 } 60 61 public void transformMatrixToGlobal(@NonNull View view, @NonNull Matrix matrix) { 62 final ViewParent parent = view.getParent(); 63 if (parent instanceof View) { 64 final View vp = (View) parent; 65 transformMatrixToGlobal(vp, matrix); 66 matrix.preTranslate(-vp.getScrollX(), -vp.getScrollY()); 67 } 68 matrix.preTranslate(view.getLeft(), view.getTop()); 69 final Matrix vm = view.getMatrix(); 70 if (!vm.isIdentity()) { 71 matrix.preConcat(vm); 72 } 73 } 74 75 public void transformMatrixToLocal(@NonNull View view, @NonNull Matrix matrix) { 76 final ViewParent parent = view.getParent(); 77 if (parent instanceof View) { 78 final View vp = (View) parent; 79 transformMatrixToLocal(vp, matrix); 80 matrix.postTranslate(vp.getScrollX(), vp.getScrollY()); 81 } 82 matrix.postTranslate(view.getLeft(), view.getTop()); 83 final Matrix vm = view.getMatrix(); 84 if (!vm.isIdentity()) { 85 final Matrix inverted = new Matrix(); 86 if (vm.invert(inverted)) { 87 matrix.postConcat(inverted); 88 } 89 } 90 } 91 92 public void setAnimationMatrix(@NonNull View view, Matrix matrix) { 93 if (matrix == null || matrix.isIdentity()) { 94 view.setPivotX(view.getWidth() / 2); 95 view.setPivotY(view.getHeight() / 2); 96 view.setTranslationX(0); 97 view.setTranslationY(0); 98 view.setScaleX(1); 99 view.setScaleY(1); 100 view.setRotation(0); 101 } else { 102 float[] values = mMatrixValues; 103 if (values == null) { 104 mMatrixValues = values = new float[9]; 105 } 106 matrix.getValues(values); 107 final float sin = values[Matrix.MSKEW_Y]; 108 final float cos = (float) Math.sqrt(1 - sin * sin) 109 * (values[Matrix.MSCALE_X] < 0 ? -1 : 1); 110 final float rotation = (float) Math.toDegrees(Math.atan2(sin, cos)); 111 final float scaleX = values[Matrix.MSCALE_X] / cos; 112 final float scaleY = values[Matrix.MSCALE_Y] / cos; 113 final float dx = values[Matrix.MTRANS_X]; 114 final float dy = values[Matrix.MTRANS_Y]; 115 view.setPivotX(0); 116 view.setPivotY(0); 117 view.setTranslationX(dx); 118 view.setTranslationY(dy); 119 view.setRotation(rotation); 120 view.setScaleX(scaleX); 121 view.setScaleY(scaleY); 122 } 123 } 124 125 public void setLeftTopRightBottom(View v, int left, int top, int right, int bottom) { 126 v.setLeft(left); 127 v.setTop(top); 128 v.setRight(right); 129 v.setBottom(bottom); 130 } 131 132 } 133