Home | History | Annotate | Download | only in cts
      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 com.android.cts.animation.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
     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 
     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 
     93     public ValueAnimator createAnimatorWithRepeatMode(int repeatMode) {
     94         return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE, repeatMode,
     95                 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY);
     96     }
     97 
     98     public ValueAnimator createAnimatorWithRepeatCount(int repeatCount) {
     99         return createAnimator(view.newBall, "y", 1000, repeatCount, ValueAnimator.REVERSE,
    100                 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY);
    101     }
    102 
    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 
    108     public ValueAnimator createAnimatorWithInterpolator(TimeInterpolator interpolator){
    109         return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE,
    110                 ValueAnimator.REVERSE, interpolator, mStartY, mStartY + mDeltaY);
    111     }
    112 
    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 
    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 
    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 
    148     public void startAnimation(ValueAnimator valueAnimator){
    149         view.bounceYAnimator = valueAnimator;
    150         view.startColorAnimator();
    151         view.animateBall();
    152     }
    153 
    154     public void startAnimation(Animator animator){
    155         view.bounceYAnimator = (ValueAnimator)animator;
    156         view.startColorAnimator();
    157         view.animateBall();
    158     }
    159 
    160     public void startAnimation(ObjectAnimator bounceAnimator) {
    161         view.bounceYAnimator = bounceAnimator;
    162         view.startColorAnimator();
    163         view.animateBall();
    164     }
    165 
    166     public void startAnimation(ObjectAnimator bounceAnimator, ObjectAnimator colorAnimator) {
    167         view.bounceYAnimator = bounceAnimator;
    168         view.startColorAnimator(colorAnimator);
    169         view.animateBall();
    170     }
    171 
    172     public void startAnimatorSet(AnimatorSet set){
    173         view.startColorAnimator();
    174         view.animateBall(set);
    175     }
    176 
    177     public void startColorAnimation(ValueAnimator colorAnimator){
    178         view.startColorAnimator(colorAnimator);
    179     }
    180 
    181     public class AnimationView extends View {
    182         public static final int RED = 0xffFF8080;
    183         public static final int BLUE = 0xff8080FF;
    184         public ShapeHolder newBall = null;
    185         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
    186         AnimatorSet animation = null;
    187         public ValueAnimator bounceYAnimator;
    188         public ValueAnimator bounceXAnimator;
    189         public ValueAnimator colorAnimator;
    190 
    191         public AnimationView(Context context) {
    192             super(context);
    193             newBall = addBall(mBallHeight, mBallWidth);
    194         }
    195 
    196         public void startColorAnimator() {
    197             colorAnimator = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);
    198             colorAnimator.setDuration(1000);
    199             colorAnimator.setEvaluator(new ArgbEvaluator());
    200             colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
    201             colorAnimator.setRepeatMode(ValueAnimator.REVERSE);
    202             colorAnimator.start();
    203         }
    204 
    205         public void startColorAnimator(ValueAnimator animator) {
    206             this.colorAnimator = animator;
    207             colorAnimator.start();
    208         }
    209 
    210         @Override
    211         public boolean onTouchEvent(MotionEvent event) {
    212             return true;
    213         }
    214 
    215         public void animateBall() {
    216             AnimatorSet bouncer = new AnimatorSet();
    217             bouncer.play(bounceYAnimator);
    218             // Fading animation - remove the ball when the animation is done
    219             ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
    220             fadeAnim.setDuration(250);
    221             fadeAnim.addListener(new AnimatorListenerAdapter() {
    222                 @Override
    223                 public void onAnimationEnd(Animator animation) {
    224                     balls.remove(((ObjectAnimator)animation).getTarget());
    225                 }
    226             });
    227 
    228             // Sequence the two animations to play one after the other
    229             AnimatorSet animatorSet = new AnimatorSet();
    230             animatorSet.play(bouncer).before(fadeAnim);
    231             animatorSet.start();
    232         }
    233 
    234         public void animateBall(AnimatorSet set) {
    235             set.start();
    236         }
    237 
    238         private ShapeHolder addBall(float x, float y) {
    239             OvalShape circle = new OvalShape();
    240             circle.resize(x, y);
    241             ShapeDrawable drawable = new ShapeDrawable(circle);
    242             ShapeHolder shapeHolder = new ShapeHolder(drawable);
    243             shapeHolder.setX(x - 25f);
    244             shapeHolder.setY(y - 25f);
    245             Paint paint = drawable.getPaint();
    246             shapeHolder.setPaint(paint);
    247             int red = (int)(Math.random() * 255);
    248             int green = (int)(Math.random() * 255);
    249             int blue = (int)(Math.random() * 255);
    250             int color = 0xff000000 | red << 16 | green << 8 | blue;
    251             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
    252             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
    253                     50f, color, darkColor, Shader.TileMode.CLAMP);
    254             paint.setShader(gradient);
    255             balls.add(shapeHolder);
    256             return shapeHolder;
    257         }
    258 
    259         @Override
    260         protected void onDraw(Canvas canvas) {
    261             for (int i = 0; i < balls.size(); ++i) {
    262                 ShapeHolder shapeHolder = balls.get(i);
    263                 canvas.save();
    264                 canvas.translate(shapeHolder.getX(), shapeHolder.getY());
    265                 shapeHolder.getShape().draw(canvas);
    266                 canvas.restore();
    267             }
    268         }
    269     }
    270 }
    271 
    272