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 alpha level of an object.
25  * Useful for fading things in and out. This animation ends up
26  * changing the alpha property of a {@link Transformation}
27  *
28  */
29 public class AlphaAnimation extends Animation {
30     private float mFromAlpha;
31     private float mToAlpha;
32 
33     /**
34      * Constructor used when an AlphaAnimation is loaded from a resource.
35      *
36      * @param context Application context to use
37      * @param attrs Attribute set from which to read values
38      */
AlphaAnimation(Context context, AttributeSet attrs)39     public AlphaAnimation(Context context, AttributeSet attrs) {
40         super(context, attrs);
41 
42         TypedArray a =
43             context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AlphaAnimation);
44 
45         mFromAlpha = a.getFloat(com.android.internal.R.styleable.AlphaAnimation_fromAlpha, 1.0f);
46         mToAlpha = a.getFloat(com.android.internal.R.styleable.AlphaAnimation_toAlpha, 1.0f);
47 
48         a.recycle();
49     }
50 
51     /**
52      * Constructor to use when building an AlphaAnimation from code
53      *
54      * @param fromAlpha Starting alpha value for the animation, where 1.0 means
55      *        fully opaque and 0.0 means fully transparent.
56      * @param toAlpha Ending alpha value for the animation.
57      */
AlphaAnimation(float fromAlpha, float toAlpha)58     public AlphaAnimation(float fromAlpha, float toAlpha) {
59         mFromAlpha = fromAlpha;
60         mToAlpha = toAlpha;
61     }
62 
63     /**
64      * Changes the alpha property of the supplied {@link Transformation}
65      */
66     @Override
applyTransformation(float interpolatedTime, Transformation t)67     protected void applyTransformation(float interpolatedTime, Transformation t) {
68         final float alpha = mFromAlpha;
69         t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));
70     }
71 
72     @Override
willChangeTransformationMatrix()73     public boolean willChangeTransformationMatrix() {
74         return false;
75     }
76 
77     @Override
willChangeBounds()78     public boolean willChangeBounds() {
79         return false;
80     }
81 
82     /**
83      * @hide
84      */
85     @Override
hasAlpha()86     public boolean hasAlpha() {
87         return true;
88     }
89 }
90