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