1 /* 2 * Copyright (C) 2006 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 android.view.animation; 18 19 import android.annotation.UnsupportedAppUsage; 20 import android.content.Context; 21 import android.content.res.TypedArray; 22 import android.util.AttributeSet; 23 24 /** 25 * An animation that controls the position of an object. See the 26 * {@link android.view.animation full package} description for details and 27 * sample code. 28 * 29 */ 30 public class TranslateAnimation extends Animation { 31 private int mFromXType = ABSOLUTE; 32 private int mToXType = ABSOLUTE; 33 34 private int mFromYType = ABSOLUTE; 35 private int mToYType = ABSOLUTE; 36 37 /** @hide */ 38 @UnsupportedAppUsage 39 protected float mFromXValue = 0.0f; 40 /** @hide */ 41 @UnsupportedAppUsage 42 protected float mToXValue = 0.0f; 43 44 /** @hide */ 45 @UnsupportedAppUsage 46 protected float mFromYValue = 0.0f; 47 /** @hide */ 48 @UnsupportedAppUsage 49 protected float mToYValue = 0.0f; 50 51 /** @hide */ 52 protected float mFromXDelta; 53 /** @hide */ 54 protected float mToXDelta; 55 /** @hide */ 56 protected float mFromYDelta; 57 /** @hide */ 58 protected float mToYDelta; 59 60 /** 61 * Constructor used when a TranslateAnimation is loaded from a resource. 62 * 63 * @param context Application context to use 64 * @param attrs Attribute set from which to read values 65 */ TranslateAnimation(Context context, AttributeSet attrs)66 public TranslateAnimation(Context context, AttributeSet attrs) { 67 super(context, attrs); 68 69 TypedArray a = context.obtainStyledAttributes(attrs, 70 com.android.internal.R.styleable.TranslateAnimation); 71 72 Description d = Description.parseValue(a.peekValue( 73 com.android.internal.R.styleable.TranslateAnimation_fromXDelta)); 74 mFromXType = d.type; 75 mFromXValue = d.value; 76 77 d = Description.parseValue(a.peekValue( 78 com.android.internal.R.styleable.TranslateAnimation_toXDelta)); 79 mToXType = d.type; 80 mToXValue = d.value; 81 82 d = Description.parseValue(a.peekValue( 83 com.android.internal.R.styleable.TranslateAnimation_fromYDelta)); 84 mFromYType = d.type; 85 mFromYValue = d.value; 86 87 d = Description.parseValue(a.peekValue( 88 com.android.internal.R.styleable.TranslateAnimation_toYDelta)); 89 mToYType = d.type; 90 mToYValue = d.value; 91 92 a.recycle(); 93 } 94 95 /** 96 * Constructor to use when building a TranslateAnimation from code 97 * 98 * @param fromXDelta Change in X coordinate to apply at the start of the 99 * animation 100 * @param toXDelta Change in X coordinate to apply at the end of the 101 * animation 102 * @param fromYDelta Change in Y coordinate to apply at the start of the 103 * animation 104 * @param toYDelta Change in Y coordinate to apply at the end of the 105 * animation 106 */ TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)107 public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) { 108 mFromXValue = fromXDelta; 109 mToXValue = toXDelta; 110 mFromYValue = fromYDelta; 111 mToYValue = toYDelta; 112 113 mFromXType = ABSOLUTE; 114 mToXType = ABSOLUTE; 115 mFromYType = ABSOLUTE; 116 mToYType = ABSOLUTE; 117 } 118 119 /** 120 * Constructor to use when building a TranslateAnimation from code 121 * 122 * @param fromXType Specifies how fromXValue should be interpreted. One of 123 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 124 * Animation.RELATIVE_TO_PARENT. 125 * @param fromXValue Change in X coordinate to apply at the start of the 126 * animation. This value can either be an absolute number if fromXType 127 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 128 * @param toXType Specifies how toXValue should be interpreted. One of 129 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 130 * Animation.RELATIVE_TO_PARENT. 131 * @param toXValue Change in X coordinate to apply at the end of the 132 * animation. This value can either be an absolute number if toXType 133 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 134 * @param fromYType Specifies how fromYValue should be interpreted. One of 135 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 136 * Animation.RELATIVE_TO_PARENT. 137 * @param fromYValue Change in Y coordinate to apply at the start of the 138 * animation. This value can either be an absolute number if fromYType 139 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 140 * @param toYType Specifies how toYValue should be interpreted. One of 141 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 142 * Animation.RELATIVE_TO_PARENT. 143 * @param toYValue Change in Y coordinate to apply at the end of the 144 * animation. This value can either be an absolute number if toYType 145 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. 146 */ TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)147 public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, 148 int fromYType, float fromYValue, int toYType, float toYValue) { 149 150 mFromXValue = fromXValue; 151 mToXValue = toXValue; 152 mFromYValue = fromYValue; 153 mToYValue = toYValue; 154 155 mFromXType = fromXType; 156 mToXType = toXType; 157 mFromYType = fromYType; 158 mToYType = toYType; 159 } 160 161 162 @Override applyTransformation(float interpolatedTime, Transformation t)163 protected void applyTransformation(float interpolatedTime, Transformation t) { 164 float dx = mFromXDelta; 165 float dy = mFromYDelta; 166 if (mFromXDelta != mToXDelta) { 167 dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime); 168 } 169 if (mFromYDelta != mToYDelta) { 170 dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime); 171 } 172 t.getMatrix().setTranslate(dx, dy); 173 } 174 175 @Override initialize(int width, int height, int parentWidth, int parentHeight)176 public void initialize(int width, int height, int parentWidth, int parentHeight) { 177 super.initialize(width, height, parentWidth, parentHeight); 178 mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth); 179 mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth); 180 mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight); 181 mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight); 182 } 183 } 184