1 /* 2 * Copyright (C) 2020 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.constraintlayout.core.motion.utils; 18 19 /** 20 * This is used to calculate the related velocity matrix for a post layout matrix 21 * 22 * 23 */ 24 public class VelocityMatrix { 25 float mDScaleX, mDScaleY, mDTranslateX, mDTranslateY, mDRotate; 26 float mRotate; 27 @SuppressWarnings("unused") private static String sTag = "VelocityMatrix"; 28 29 // @TODO: add description clear()30 public void clear() { 31 mDScaleX = mDScaleY = mDTranslateX = mDTranslateY = mDRotate = 0; 32 } 33 34 // @TODO: add description setRotationVelocity(SplineSet rot, float position)35 public void setRotationVelocity(SplineSet rot, float position) { 36 if (rot != null) { 37 mDRotate = rot.getSlope(position); 38 mRotate = rot.get(position); 39 } 40 } 41 42 // @TODO: add description setTranslationVelocity(SplineSet transX, SplineSet transY, float position)43 public void setTranslationVelocity(SplineSet transX, SplineSet transY, float position) { 44 45 if (transX != null) { 46 mDTranslateX = transX.getSlope(position); 47 } 48 if (transY != null) { 49 mDTranslateY = transY.getSlope(position); 50 } 51 } 52 53 // @TODO: add description setScaleVelocity(SplineSet scaleX, SplineSet scaleY, float position)54 public void setScaleVelocity(SplineSet scaleX, SplineSet scaleY, float position) { 55 56 if (scaleX != null) { 57 mDScaleX = scaleX.getSlope(position); 58 } 59 if (scaleY != null) { 60 mDScaleY = scaleY.getSlope(position); 61 } 62 } 63 64 // @TODO: add description setRotationVelocity(KeyCycleOscillator oscR, float position)65 public void setRotationVelocity(KeyCycleOscillator oscR, float position) { 66 if (oscR != null) { 67 mDRotate = oscR.getSlope(position); 68 } 69 } 70 71 // @TODO: add description setTranslationVelocity(KeyCycleOscillator oscX, KeyCycleOscillator oscY, float position)72 public void setTranslationVelocity(KeyCycleOscillator oscX, 73 KeyCycleOscillator oscY, 74 float position) { 75 76 if (oscX != null) { 77 mDTranslateX = oscX.getSlope(position); 78 } 79 80 if (oscY != null) { 81 mDTranslateY = oscY.getSlope(position); 82 } 83 } 84 85 // @TODO: add description setScaleVelocity(KeyCycleOscillator oscSx, KeyCycleOscillator oscSy, float position)86 public void setScaleVelocity(KeyCycleOscillator oscSx, 87 KeyCycleOscillator oscSy, 88 float position) { 89 if (oscSx != null) { 90 mDScaleX = oscSx.getSlope(position); 91 } 92 if (oscSy != null) { 93 mDScaleY = oscSy.getSlope(position); 94 } 95 } 96 97 /** 98 * Apply the transform a velocity vector 99 * 100 * 101 */ applyTransform(float locationX, float locationY, int width, int height, float[] mAnchorDpDt)102 public void applyTransform(float locationX, 103 float locationY, 104 int width, 105 int height, 106 float[] mAnchorDpDt) { 107 float dx = mAnchorDpDt[0]; 108 float dy = mAnchorDpDt[1]; 109 float offx = 2 * (locationX - 0.5f); 110 float offy = 2 * (locationY - 0.5f); 111 dx += mDTranslateX; 112 dy += mDTranslateY; 113 dx += offx * mDScaleX; 114 dy += offy * mDScaleY; 115 float r = (float) Math.toRadians(mRotate); 116 float dr = (float) Math.toRadians(mDRotate); 117 dx += dr * (float) (-width * offx * Math.sin(r) - height * offy * Math.cos(r)); 118 dy += dr * (float) (width * offx * Math.cos(r) - height * offy * Math.sin(r)); 119 mAnchorDpDt[0] = dx; 120 mAnchorDpDt[1] = dy; 121 } 122 } 123