blob: f038c52fb0f90f5a200ecddf309e45be2a986ab6 [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.transition;
import android.graphics.Matrix;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewParent;
@RequiresApi(14)
class ViewUtilsApi14 implements ViewUtilsImpl {
private float[] mMatrixValues;
@Override
public ViewOverlayImpl getOverlay(@NonNull View view) {
return ViewOverlayApi14.createFrom(view);
}
@Override
public WindowIdImpl getWindowId(@NonNull View view) {
return new WindowIdApi14(view.getWindowToken());
}
@Override
public void setTransitionAlpha(@NonNull View view, float alpha) {
Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha);
if (savedAlpha != null) {
view.setAlpha(savedAlpha * alpha);
} else {
view.setAlpha(alpha);
}
}
@Override
public float getTransitionAlpha(@NonNull View view) {
Float savedAlpha = (Float) view.getTag(R.id.save_non_transition_alpha);
if (savedAlpha != null) {
return view.getAlpha() / savedAlpha;
} else {
return view.getAlpha();
}
}
@Override
public void saveNonTransitionAlpha(@NonNull View view) {
if (view.getTag(R.id.save_non_transition_alpha) == null) {
view.setTag(R.id.save_non_transition_alpha, view.getAlpha());
}
}
@Override
public void clearNonTransitionAlpha(@NonNull View view) {
// We don't clear the saved value when the view is hidden; that's the situation we are
// saving this value for.
if (view.getVisibility() == View.VISIBLE) {
view.setTag(R.id.save_non_transition_alpha, null);
}
}
@Override
public void transformMatrixToGlobal(@NonNull View view, @NonNull Matrix matrix) {
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
transformMatrixToGlobal(vp, matrix);
matrix.preTranslate(-vp.getScrollX(), -vp.getScrollY());
}
matrix.preTranslate(view.getLeft(), view.getTop());
final Matrix vm = view.getMatrix();
if (!vm.isIdentity()) {
matrix.preConcat(vm);
}
}
@Override
public void transformMatrixToLocal(@NonNull View view, @NonNull Matrix matrix) {
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
transformMatrixToLocal(vp, matrix);
matrix.postTranslate(vp.getScrollX(), vp.getScrollY());
}
matrix.postTranslate(view.getLeft(), view.getTop());
final Matrix vm = view.getMatrix();
if (!vm.isIdentity()) {
final Matrix inverted = new Matrix();
if (vm.invert(inverted)) {
matrix.postConcat(inverted);
}
}
}
@Override
public void setAnimationMatrix(@NonNull View view, Matrix matrix) {
if (matrix == null || matrix.isIdentity()) {
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight() / 2);
view.setTranslationX(0);
view.setTranslationY(0);
view.setScaleX(1);
view.setScaleY(1);
view.setRotation(0);
} else {
float[] values = mMatrixValues;
if (values == null) {
mMatrixValues = values = new float[9];
}
matrix.getValues(values);
final float sin = values[Matrix.MSKEW_Y];
final float cos = (float) Math.sqrt(1 - sin * sin)
* (values[Matrix.MSCALE_X] < 0 ? -1 : 1);
final float rotation = (float) Math.toDegrees(Math.atan2(sin, cos));
final float scaleX = values[Matrix.MSCALE_X] / cos;
final float scaleY = values[Matrix.MSCALE_Y] / cos;
final float dx = values[Matrix.MTRANS_X];
final float dy = values[Matrix.MTRANS_Y];
view.setPivotX(0);
view.setPivotY(0);
view.setTranslationX(dx);
view.setTranslationY(dy);
view.setRotation(rotation);
view.setScaleX(scaleX);
view.setScaleY(scaleY);
}
}
@Override
public void setLeftTopRightBottom(View v, int left, int top, int right, int bottom) {
v.setLeft(left);
v.setTop(top);
v.setRight(right);
v.setBottom(bottom);
}
}