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 17package android.support.transition; 18 19import android.graphics.Matrix; 20import android.support.annotation.NonNull; 21import android.support.annotation.RequiresApi; 22import android.view.View; 23import android.view.ViewParent; 24 25@RequiresApi(14) 26class ViewUtilsApi14 implements ViewUtilsImpl { 27 28 private float[] mMatrixValues; 29 30 @Override 31 public ViewOverlayImpl getOverlay(@NonNull View view) { 32 return ViewOverlayApi14.createFrom(view); 33 } 34 35 @Override 36 public WindowIdImpl getWindowId(@NonNull View view) { 37 return new WindowIdApi14(view.getWindowToken()); 38 } 39 40 @Override 41 public void setTransitionAlpha(@NonNull View view, float alpha) { 42 Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha); 43 if (savedAlpha != null) { 44 view.setAlpha(savedAlpha * alpha); 45 } else { 46 view.setAlpha(alpha); 47 } 48 } 49 50 @Override 51 public float getTransitionAlpha(@NonNull View view) { 52 Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha); 53 if (savedAlpha != null) { 54 return view.getAlpha() / savedAlpha; 55 } else { 56 return view.getAlpha(); 57 } 58 } 59 60 @Override 61 public void saveNonTransitionAlpha(@NonNull View view) { 62 if (view.getTag(R.id.save_non_transition_alpha) == null) { 63 view.setTag(R.id.save_non_transition_alpha, view.getAlpha()); 64 } 65 } 66 67 @Override 68 public void clearNonTransitionAlpha(@NonNull View view) { 69 // We don't clear the saved value when the view is hidden; that's the situation we are 70 // saving this value for. 71 if (view.getVisibility() == View.VISIBLE) { 72 view.setTag(R.id.save_non_transition_alpha, null); 73 } 74 } 75 76 @Override 77 public void transformMatrixToGlobal(@NonNull View view, @NonNull Matrix matrix) { 78 final ViewParent parent = view.getParent(); 79 if (parent instanceof View) { 80 final View vp = (View) parent; 81 transformMatrixToGlobal(vp, matrix); 82 matrix.preTranslate(-vp.getScrollX(), -vp.getScrollY()); 83 } 84 matrix.preTranslate(view.getLeft(), view.getTop()); 85 final Matrix vm = view.getMatrix(); 86 if (!vm.isIdentity()) { 87 matrix.preConcat(vm); 88 } 89 } 90 91 @Override 92 public void transformMatrixToLocal(@NonNull View view, @NonNull Matrix matrix) { 93 final ViewParent parent = view.getParent(); 94 if (parent instanceof View) { 95 final View vp = (View) parent; 96 transformMatrixToLocal(vp, matrix); 97 matrix.postTranslate(vp.getScrollX(), vp.getScrollY()); 98 } 99 matrix.postTranslate(view.getLeft(), view.getTop()); 100 final Matrix vm = view.getMatrix(); 101 if (!vm.isIdentity()) { 102 final Matrix inverted = new Matrix(); 103 if (vm.invert(inverted)) { 104 matrix.postConcat(inverted); 105 } 106 } 107 } 108 109 @Override 110 public void setAnimationMatrix(@NonNull View view, Matrix matrix) { 111 if (matrix == null || matrix.isIdentity()) { 112 view.setPivotX(view.getWidth() / 2); 113 view.setPivotY(view.getHeight() / 2); 114 view.setTranslationX(0); 115 view.setTranslationY(0); 116 view.setScaleX(1); 117 view.setScaleY(1); 118 view.setRotation(0); 119 } else { 120 float[] values = mMatrixValues; 121 if (values == null) { 122 mMatrixValues = values = new float[9]; 123 } 124 matrix.getValues(values); 125 final float sin = values[Matrix.MSKEW_Y]; 126 final float cos = (float) Math.sqrt(1 - sin * sin) 127 * (values[Matrix.MSCALE_X] < 0 ? -1 : 1); 128 final float rotation = (float) Math.toDegrees(Math.atan2(sin, cos)); 129 final float scaleX = values[Matrix.MSCALE_X] / cos; 130 final float scaleY = values[Matrix.MSCALE_Y] / cos; 131 final float dx = values[Matrix.MTRANS_X]; 132 final float dy = values[Matrix.MTRANS_Y]; 133 view.setPivotX(0); 134 view.setPivotY(0); 135 view.setTranslationX(dx); 136 view.setTranslationY(dy); 137 view.setRotation(rotation); 138 view.setScaleX(scaleX); 139 view.setScaleY(scaleY); 140 } 141 } 142 143 @Override 144 public void setLeftTopRightBottom(View v, int left, int top, int right, int bottom) { 145 v.setLeft(left); 146 v.setTop(top); 147 v.setRight(right); 148 v.setBottom(bottom); 149 } 150 151} 152