1 /*
2  * Copyright (C) 2012 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.animation.cts;
18 
19 import android.animation.Animator;
20 import android.animation.AnimatorListenerAdapter;
21 import android.animation.AnimatorSet;
22 import android.animation.ArgbEvaluator;
23 import android.animation.ObjectAnimator;
24 import android.animation.TimeInterpolator;
25 import android.animation.ValueAnimator;
26 import android.app.Activity;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.graphics.Canvas;
30 import android.graphics.Paint;
31 import android.graphics.RadialGradient;
32 import android.graphics.Shader;
33 import android.graphics.drawable.ShapeDrawable;
34 import android.graphics.drawable.shapes.OvalShape;
35 import android.os.Bundle;
36 import android.view.MotionEvent;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.animation.AccelerateInterpolator;
40 import java.util.ArrayList;
41 
42 import android.animation.cts.R;
43 
44 public class AnimationActivity extends Activity {
45     private static final String BALL_HEIGHT = "ballwidth";
46     private static final String BALL_WIDTH = "ballheight";
47     private static final String STARTX = "startx";
48     private static final String STARTY = "starty";
49     private static final String DELTAX = "deltax";
50     private static final String DELTAY = "deltay";
51     private static final String DURATION = "duration";
52 
53     float mBallHeight = 50f;
54     float mBallWidth = 50f;
55     float mStartX = 200f;
56     float mStartY = 200f;
57     float mDeltaX = 200f;
58     float mDeltaY = 200f;
59     long mDuration = 1000;
60     public AnimationView view = null;
61 
62     @Override
onCreate(Bundle bundle)63     public void onCreate(Bundle bundle){
64         super.onCreate(bundle);
65         Intent intent = this.getIntent();
66         Bundle extras = intent.getExtras();
67         if(extras != null) {
68             mBallHeight = extras.getFloat(BALL_HEIGHT);
69             mBallWidth = extras.getFloat(BALL_WIDTH);
70             mStartX = extras.getFloat(STARTX);
71             mStartY = extras.getFloat(STARTY);
72             mDeltaX = extras.getFloat(DELTAX);
73             mDeltaY = extras.getFloat(DELTAY);
74             mDuration = extras.getInt(DURATION);
75         }
76         setContentView(R.layout.animation_main);
77         view = new AnimationView(this);
78         ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
79         viewGroup.addView(view);
80     }
81 
createAnimator(Object object,String propertyName, long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator, float start, float end)82     public ValueAnimator createAnimator(Object object,String propertyName, long duration,
83             int repeatCount, int repeatMode,
84             TimeInterpolator timeInterpolator, float start, float end){
85         ValueAnimator valueAnimator = ObjectAnimator.ofFloat(object, propertyName, start,end);
86         valueAnimator.setDuration(duration);
87         valueAnimator.setRepeatCount(repeatCount);
88         valueAnimator.setInterpolator(timeInterpolator);
89         valueAnimator.setRepeatMode(repeatMode);
90         return valueAnimator;
91     }
92 
createAnimatorWithRepeatMode(int repeatMode)93     public ValueAnimator createAnimatorWithRepeatMode(int repeatMode) {
94         return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE, repeatMode,
95                 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY);
96     }
97 
createAnimatorWithRepeatCount(int repeatCount)98     public ValueAnimator createAnimatorWithRepeatCount(int repeatCount) {
99         return createAnimator(view.newBall, "y", 1000, repeatCount, ValueAnimator.REVERSE,
100                 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY);
101     }
102 
createAnimatorWithDuration(long duration)103     public ValueAnimator createAnimatorWithDuration(long duration) {
104         return createAnimator(view.newBall, "y", duration ,ValueAnimator.INFINITE,
105                 ValueAnimator.REVERSE,new AccelerateInterpolator(), mStartY, mStartY + mDeltaY);
106     }
107 
createAnimatorWithInterpolator(TimeInterpolator interpolator)108     public ValueAnimator createAnimatorWithInterpolator(TimeInterpolator interpolator){
109         return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE,
110                 ValueAnimator.REVERSE, interpolator, mStartY, mStartY + mDeltaY);
111     }
112 
createObjectAnimatorForInt(Object object,String propertyName, long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator, int start, int end)113     public ValueAnimator createObjectAnimatorForInt(Object object,String propertyName,
114             long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator,
115             int start, int end) {
116         ObjectAnimator objAnimator = ObjectAnimator.ofInt(object, propertyName, start,end);
117         objAnimator.setDuration(duration);
118 
119         objAnimator.setRepeatCount(repeatCount);
120         objAnimator.setInterpolator(timeInterpolator);
121         objAnimator.setRepeatMode(repeatMode);
122         return objAnimator;
123     }
124 
createObjectAnimatorForInt()125     public ValueAnimator createObjectAnimatorForInt() {
126         ObjectAnimator objAnimator = ObjectAnimator.ofInt(view.newBall, "y", (int)mStartY,
127             (int)(mStartY + mDeltaY));
128         objAnimator.setDuration(mDuration);
129 
130         objAnimator.setRepeatCount(ValueAnimator.INFINITE);
131         objAnimator.setInterpolator(new AccelerateInterpolator());
132         objAnimator.setRepeatMode(ValueAnimator.REVERSE);
133         return objAnimator;
134     }
135 
startAnimation(long duration)136     public void startAnimation(long duration){
137         ValueAnimator bounceAnimator = ObjectAnimator.ofFloat(view.newBall, "y", mStartY,
138             mStartY + mDeltaY);
139         bounceAnimator.setDuration(duration);
140         bounceAnimator.setRepeatCount(ValueAnimator.INFINITE);
141         bounceAnimator.setInterpolator(new AccelerateInterpolator());
142         bounceAnimator.setRepeatMode(ValueAnimator.REVERSE);
143         view.bounceYAnimator = bounceAnimator;
144         view.startColorAnimator();
145         view.animateBall();
146     }
147 
startSingleAnimation(Animator animator)148     public void startSingleAnimation(Animator animator) {
149         animator.setTarget(view.newBall);
150         animator.start();
151     }
152 
startAnimation(ValueAnimator valueAnimator)153     public void startAnimation(ValueAnimator valueAnimator){
154         view.bounceYAnimator = valueAnimator;
155         view.startColorAnimator();
156         view.animateBall();
157     }
158 
startAnimation(Animator animator)159     public void startAnimation(Animator animator){
160         view.bounceYAnimator = (ValueAnimator)animator;
161         view.startColorAnimator();
162         view.animateBall();
163     }
164 
startAnimation(ObjectAnimator bounceAnimator)165     public void startAnimation(ObjectAnimator bounceAnimator) {
166         view.bounceYAnimator = bounceAnimator;
167         view.startColorAnimator();
168         view.animateBall();
169     }
170 
startAnimation(ObjectAnimator bounceAnimator, ObjectAnimator colorAnimator)171     public void startAnimation(ObjectAnimator bounceAnimator, ObjectAnimator colorAnimator) {
172         view.bounceYAnimator = bounceAnimator;
173         view.startColorAnimator(colorAnimator);
174         view.animateBall();
175     }
176 
startAnimatorSet(AnimatorSet set)177     public void startAnimatorSet(AnimatorSet set){
178         view.startColorAnimator();
179         view.animateBall(set);
180     }
181 
startColorAnimation(ValueAnimator colorAnimator)182     public void startColorAnimation(ValueAnimator colorAnimator){
183         view.startColorAnimator(colorAnimator);
184     }
185 
186     public class AnimationView extends View {
187         public static final int RED = 0xffFF8080;
188         public static final int BLUE = 0xff8080FF;
189         public ShapeHolder newBall = null;
190         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
191         AnimatorSet animation = null;
192         public ValueAnimator bounceYAnimator;
193         public ValueAnimator bounceXAnimator;
194         public ValueAnimator colorAnimator;
195 
AnimationView(Context context)196         public AnimationView(Context context) {
197             super(context);
198             newBall = addBall(mBallHeight, mBallWidth);
199         }
200 
startColorAnimator()201         public void startColorAnimator() {
202             colorAnimator = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);
203             colorAnimator.setDuration(1000);
204             colorAnimator.setEvaluator(new ArgbEvaluator());
205             colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
206             colorAnimator.setRepeatMode(ValueAnimator.REVERSE);
207             colorAnimator.start();
208         }
209 
startColorAnimator(ValueAnimator animator)210         public void startColorAnimator(ValueAnimator animator) {
211             this.colorAnimator = animator;
212             colorAnimator.start();
213         }
214 
215         @Override
onTouchEvent(MotionEvent event)216         public boolean onTouchEvent(MotionEvent event) {
217             return true;
218         }
219 
animateBall()220         public void animateBall() {
221             AnimatorSet bouncer = new AnimatorSet();
222             bouncer.play(bounceYAnimator);
223             // Fading animation - remove the ball when the animation is done
224             ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
225             fadeAnim.setDuration(250);
226             fadeAnim.addListener(new AnimatorListenerAdapter() {
227                 @Override
228                 public void onAnimationEnd(Animator animation) {
229                     balls.remove(((ObjectAnimator)animation).getTarget());
230                 }
231             });
232 
233             // Sequence the two animations to play one after the other
234             AnimatorSet animatorSet = new AnimatorSet();
235             animatorSet.play(bouncer).before(fadeAnim);
236             animatorSet.start();
237         }
238 
animateBall(AnimatorSet set)239         public void animateBall(AnimatorSet set) {
240             set.start();
241         }
242 
addBall(float x, float y)243         private ShapeHolder addBall(float x, float y) {
244             OvalShape circle = new OvalShape();
245             circle.resize(x, y);
246             ShapeDrawable drawable = new ShapeDrawable(circle);
247             ShapeHolder shapeHolder = new ShapeHolder(drawable);
248             shapeHolder.setX(x - 25f);
249             shapeHolder.setY(y - 25f);
250             Paint paint = drawable.getPaint();
251             shapeHolder.setPaint(paint);
252             int red = (int)(Math.random() * 255);
253             int green = (int)(Math.random() * 255);
254             int blue = (int)(Math.random() * 255);
255             int color = 0xff000000 | red << 16 | green << 8 | blue;
256             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
257             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
258                     50f, color, darkColor, Shader.TileMode.CLAMP);
259             paint.setShader(gradient);
260             balls.add(shapeHolder);
261             return shapeHolder;
262         }
263 
264         @Override
onDraw(Canvas canvas)265         protected void onDraw(Canvas canvas) {
266             for (int i = 0; i < balls.size(); ++i) {
267                 ShapeHolder shapeHolder = balls.get(i);
268                 canvas.save();
269                 canvas.translate(shapeHolder.getX(), shapeHolder.getY());
270                 shapeHolder.getShape().draw(canvas);
271                 canvas.restore();
272             }
273         }
274     }
275 }
276 
277