Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2010 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 com.example.android.apis.animation;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import android.animation.Animator;
     22 import android.animation.ObjectAnimator;
     23 import android.widget.CheckBox;
     24 import android.widget.TextView;
     25 import com.example.android.apis.R;
     26 
     27 import java.util.ArrayList;
     28 
     29 import android.animation.ValueAnimator;
     30 import android.animation.AnimatorSet;
     31 import android.app.Activity;
     32 import android.content.Context;
     33 import android.graphics.Canvas;
     34 import android.graphics.Paint;
     35 import android.graphics.RadialGradient;
     36 import android.graphics.Shader;
     37 import android.graphics.drawable.ShapeDrawable;
     38 import android.graphics.drawable.shapes.OvalShape;
     39 import android.os.Bundle;
     40 import android.view.View;
     41 import android.view.animation.AccelerateInterpolator;
     42 import android.widget.Button;
     43 import android.widget.LinearLayout;
     44 
     45 /**
     46  * This demo shows how the AnimatorListener events work.
     47  */
     48 public class AnimatorEvents extends Activity {
     49 
     50     TextView startText, repeatText, cancelText, endText;
     51     TextView startTextAnimator, repeatTextAnimator, cancelTextAnimator, endTextAnimator;
     52 
     53     @Override
     54     public void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56         setContentView(R.layout.animator_events);
     57         LinearLayout container = (LinearLayout) findViewById(R.id.container);
     58         final MyAnimationView animView = new MyAnimationView(this);
     59         container.addView(animView);
     60         startText = (TextView) findViewById(R.id.startText);
     61         startText.setAlpha(.5f);
     62         repeatText = (TextView) findViewById(R.id.repeatText);
     63         repeatText.setAlpha(.5f);
     64         cancelText = (TextView) findViewById(R.id.cancelText);
     65         cancelText.setAlpha(.5f);
     66         endText = (TextView) findViewById(R.id.endText);
     67         endText.setAlpha(.5f);
     68         startTextAnimator = (TextView) findViewById(R.id.startTextAnimator);
     69         startTextAnimator.setAlpha(.5f);
     70         repeatTextAnimator = (TextView) findViewById(R.id.repeatTextAnimator);
     71         repeatTextAnimator.setAlpha(.5f);
     72         cancelTextAnimator = (TextView) findViewById(R.id.cancelTextAnimator);
     73         cancelTextAnimator.setAlpha(.5f);
     74         endTextAnimator = (TextView) findViewById(R.id.endTextAnimator);
     75         endTextAnimator.setAlpha(.5f);
     76         final CheckBox endCB = (CheckBox) findViewById(R.id.endCB);
     77 
     78 
     79         Button starter = (Button) findViewById(R.id.startButton);
     80         starter.setOnClickListener(new View.OnClickListener() {
     81 
     82             public void onClick(View v) {
     83                 animView.startAnimation(endCB.isChecked());
     84             }
     85         });
     86 
     87         Button canceler = (Button) findViewById(R.id.cancelButton);
     88         canceler.setOnClickListener(new View.OnClickListener() {
     89 
     90             public void onClick(View v) {
     91                 animView.cancelAnimation();
     92             }
     93         });
     94 
     95         Button ender = (Button) findViewById(R.id.endButton);
     96         ender.setOnClickListener(new View.OnClickListener() {
     97 
     98             public void onClick(View v) {
     99                 animView.endAnimation();
    100             }
    101         });
    102 
    103     }
    104 
    105     public class MyAnimationView extends View implements Animator.AnimatorListener,
    106     ValueAnimator.AnimatorUpdateListener {
    107 
    108         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
    109         Animator animation;
    110         ShapeHolder ball = null;
    111         boolean endImmediately = false;
    112 
    113         public MyAnimationView(Context context) {
    114             super(context);
    115             ball = createBall(25, 25);
    116         }
    117 
    118         private void createAnimation() {
    119             if (animation == null) {
    120                 ObjectAnimator yAnim = ObjectAnimator.ofFloat(ball, "y",
    121                         ball.getY(), getHeight() - 50f).setDuration(1500);
    122                 yAnim.setRepeatCount(0);
    123                 yAnim.setRepeatMode(ValueAnimator.REVERSE);
    124                 yAnim.setInterpolator(new AccelerateInterpolator(2f));
    125                 yAnim.addUpdateListener(this);
    126                 yAnim.addListener(this);
    127 
    128                 ObjectAnimator xAnim = ObjectAnimator.ofFloat(ball, "x",
    129                         ball.getX(), ball.getX() + 300).setDuration(1000);
    130                 xAnim.setStartDelay(0);
    131                 xAnim.setRepeatCount(0);
    132                 xAnim.setRepeatMode(ValueAnimator.REVERSE);
    133                 xAnim.setInterpolator(new AccelerateInterpolator(2f));
    134 
    135                 ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(ball, "alpha", 1f, .5f).
    136                         setDuration(1000);
    137                 AnimatorSet alphaSeq = new AnimatorSet();
    138                 alphaSeq.play(alphaAnim);
    139 
    140                 animation = new AnimatorSet();
    141                 ((AnimatorSet) animation).playTogether(yAnim, xAnim);
    142                 animation.addListener(this);
    143             }
    144         }
    145 
    146         public void startAnimation(boolean endImmediately) {
    147             this.endImmediately = endImmediately;
    148             startText.setAlpha(.5f);
    149             repeatText.setAlpha(.5f);
    150             cancelText.setAlpha(.5f);
    151             endText.setAlpha(.5f);
    152             startTextAnimator.setAlpha(.5f);
    153             repeatTextAnimator.setAlpha(.5f);
    154             cancelTextAnimator.setAlpha(.5f);
    155             endTextAnimator.setAlpha(.5f);
    156             createAnimation();
    157             animation.start();
    158         }
    159 
    160         public void cancelAnimation() {
    161             createAnimation();
    162             animation.cancel();
    163         }
    164 
    165         public void endAnimation() {
    166             createAnimation();
    167             animation.end();
    168         }
    169 
    170         private ShapeHolder createBall(float x, float y) {
    171             OvalShape circle = new OvalShape();
    172             circle.resize(50f, 50f);
    173             ShapeDrawable drawable = new ShapeDrawable(circle);
    174             ShapeHolder shapeHolder = new ShapeHolder(drawable);
    175             shapeHolder.setX(x - 25f);
    176             shapeHolder.setY(y - 25f);
    177             int red = (int)(Math.random() * 255);
    178             int green = (int)(Math.random() * 255);
    179             int blue = (int)(Math.random() * 255);
    180             int color = 0xff000000 | red << 16 | green << 8 | blue;
    181             Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
    182             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
    183             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
    184                     50f, color, darkColor, Shader.TileMode.CLAMP);
    185             paint.setShader(gradient);
    186             shapeHolder.setPaint(paint);
    187             return shapeHolder;
    188         }
    189 
    190         @Override
    191         protected void onDraw(Canvas canvas) {
    192             canvas.save();
    193             canvas.translate(ball.getX(), ball.getY());
    194             ball.getShape().draw(canvas);
    195             canvas.restore();
    196         }
    197 
    198         public void onAnimationUpdate(ValueAnimator animation) {
    199             invalidate();
    200         }
    201 
    202         public void onAnimationStart(Animator animation) {
    203             if (animation instanceof AnimatorSet) {
    204                 startText.setAlpha(1f);
    205             } else {
    206                 startTextAnimator.setAlpha(1f);
    207             }
    208             if (endImmediately) {
    209                 animation.end();
    210             }
    211         }
    212 
    213         public void onAnimationEnd(Animator animation) {
    214             if (animation instanceof AnimatorSet) {
    215                 endText.setAlpha(1f);
    216             } else {
    217                 endTextAnimator.setAlpha(1f);
    218             }
    219         }
    220 
    221         public void onAnimationCancel(Animator animation) {
    222             if (animation instanceof AnimatorSet) {
    223                 cancelText.setAlpha(1f);
    224             } else {
    225                 cancelTextAnimator.setAlpha(1f);
    226             }
    227         }
    228 
    229         public void onAnimationRepeat(Animator animation) {
    230             if (animation instanceof AnimatorSet) {
    231                 repeatText.setAlpha(1f);
    232             } else {
    233                 repeatTextAnimator.setAlpha(1f);
    234             }
    235         }
    236     }
    237 }