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.ObjectAnimator;
     22 import android.animation.ValueAnimator;
     23 import com.example.android.apis.R;
     24 
     25 import java.util.ArrayList;
     26 
     27 import android.app.Activity;
     28 import android.content.Context;
     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.View;
     37 import android.view.animation.AccelerateInterpolator;
     38 import android.widget.Button;
     39 import android.widget.LinearLayout;
     40 
     41 public class ReversingAnimation extends Activity {
     42 
     43     @Override
     44     public void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.animation_reversing);
     47         LinearLayout container = (LinearLayout) findViewById(R.id.container);
     48         final MyAnimationView animView = new MyAnimationView(this);
     49         container.addView(animView);
     50 
     51         Button starter = (Button) findViewById(R.id.startButton);
     52         starter.setOnClickListener(new View.OnClickListener() {
     53             public void onClick(View v) {
     54                 animView.startAnimation();
     55             }
     56         });
     57 
     58         Button reverser = (Button) findViewById(R.id.reverseButton);
     59         reverser.setOnClickListener(new View.OnClickListener() {
     60             public void onClick(View v) {
     61                 animView.reverseAnimation();
     62             }
     63         });
     64 
     65     }
     66 
     67     public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener {
     68 
     69         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
     70         ValueAnimator bounceAnim = null;
     71         ShapeHolder ball = null;
     72 
     73         public MyAnimationView(Context context) {
     74             super(context);
     75             ball = createBall(25, 25);
     76         }
     77 
     78         private void createAnimation() {
     79             if (bounceAnim == null) {
     80                 bounceAnim = ObjectAnimator.ofFloat(ball, "y", ball.getY(), getHeight() - 50f).
     81                         setDuration(1500);
     82                 bounceAnim.setInterpolator(new AccelerateInterpolator(2f));
     83                 bounceAnim.addUpdateListener(this);
     84             }
     85         }
     86 
     87         public void startAnimation() {
     88             createAnimation();
     89             bounceAnim.start();
     90         }
     91 
     92         public void reverseAnimation() {
     93             createAnimation();
     94             bounceAnim.reverse();
     95         }
     96 
     97         public void seek(long seekTime) {
     98             createAnimation();
     99             bounceAnim.setCurrentPlayTime(seekTime);
    100         }
    101 
    102         private ShapeHolder createBall(float x, float y) {
    103             OvalShape circle = new OvalShape();
    104             circle.resize(50f, 50f);
    105             ShapeDrawable drawable = new ShapeDrawable(circle);
    106             ShapeHolder shapeHolder = new ShapeHolder(drawable);
    107             shapeHolder.setX(x - 25f);
    108             shapeHolder.setY(y - 25f);
    109             int red = (int)(Math.random() * 255);
    110             int green = (int)(Math.random() * 255);
    111             int blue = (int)(Math.random() * 255);
    112             int color = 0xff000000 | red << 16 | green << 8 | blue;
    113             Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
    114             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
    115             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
    116                     50f, color, darkColor, Shader.TileMode.CLAMP);
    117             paint.setShader(gradient);
    118             shapeHolder.setPaint(paint);
    119             return shapeHolder;
    120         }
    121 
    122         @Override
    123         protected void onDraw(Canvas canvas) {
    124             canvas.save();
    125             canvas.translate(ball.getX(), ball.getY());
    126             ball.getShape().draw(canvas);
    127             canvas.restore();
    128         }
    129 
    130         public void onAnimationUpdate(ValueAnimator animation) {
    131             invalidate();
    132         }
    133 
    134     }
    135 }