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