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.widget.Button;
     22 import com.example.android.apis.R;
     23 
     24 import android.animation.*;
     25 import android.app.Activity;
     26 import android.content.Context;
     27 import android.graphics.Canvas;
     28 import android.graphics.Paint;
     29 import android.graphics.RadialGradient;
     30 import android.graphics.Shader;
     31 import android.graphics.drawable.ShapeDrawable;
     32 import android.graphics.drawable.shapes.OvalShape;
     33 import android.os.Bundle;
     34 import android.view.View;
     35 import android.view.animation.AccelerateInterpolator;
     36 import android.view.animation.DecelerateInterpolator;
     37 import android.widget.LinearLayout;
     38 
     39 import java.util.ArrayList;
     40 
     41 
     42 public class AnimationCloning extends Activity {
     43     /** Called when the activity is first created. */
     44     @Override
     45     public void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         setContentView(R.layout.animation_cloning);
     48         LinearLayout container = (LinearLayout) findViewById(R.id.container);
     49         final MyAnimationView animView = new MyAnimationView(this);
     50         container.addView(animView);
     51 
     52         Button starter = (Button) findViewById(R.id.startButton);
     53         starter.setOnClickListener(new View.OnClickListener() {
     54 
     55             public void onClick(View v) {
     56                 animView.startAnimation();
     57             }
     58         });
     59     }
     60 
     61     public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener {
     62 
     63         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
     64         AnimatorSet animation = null;
     65         private float mDensity;
     66 
     67         public MyAnimationView(Context context) {
     68             super(context);
     69 
     70             mDensity = getContext().getResources().getDisplayMetrics().density;
     71 
     72             ShapeHolder ball0 = addBall(50f, 25f);
     73             ShapeHolder ball1 = addBall(150f, 25f);
     74             ShapeHolder ball2 = addBall(250f, 25f);
     75             ShapeHolder ball3 = addBall(350f, 25f);
     76         }
     77 
     78         private void createAnimation() {
     79             if (animation == null) {
     80                 ObjectAnimator anim1 = ObjectAnimator.ofFloat(balls.get(0), "y",
     81                         0f, getHeight() - balls.get(0).getHeight()).setDuration(500);
     82                 ObjectAnimator anim2 = anim1.clone();
     83                 anim2.setTarget(balls.get(1));
     84                 anim1.addUpdateListener(this);
     85 
     86                 ShapeHolder ball2 = balls.get(2);
     87                 ObjectAnimator animDown = ObjectAnimator.ofFloat(ball2, "y",
     88                         0f, getHeight() - ball2.getHeight()).setDuration(500);
     89                 animDown.setInterpolator(new AccelerateInterpolator());
     90                 ObjectAnimator animUp = ObjectAnimator.ofFloat(ball2, "y",
     91                         getHeight() - ball2.getHeight(), 0f).setDuration(500);
     92                 animUp.setInterpolator(new DecelerateInterpolator());
     93                 AnimatorSet s1 = new AnimatorSet();
     94                 s1.playSequentially(animDown, animUp);
     95                 animDown.addUpdateListener(this);
     96                 animUp.addUpdateListener(this);
     97                 AnimatorSet s2 = (AnimatorSet) s1.clone();
     98                 s2.setTarget(balls.get(3));
     99 
    100                 animation = new AnimatorSet();
    101                 animation.playTogether(anim1, anim2, s1);
    102                 animation.playSequentially(s1, s2);
    103             }
    104         }
    105 
    106         private ShapeHolder addBall(float x, float y) {
    107             OvalShape circle = new OvalShape();
    108             circle.resize(50f * mDensity, 50f * mDensity);
    109             ShapeDrawable drawable = new ShapeDrawable(circle);
    110             ShapeHolder shapeHolder = new ShapeHolder(drawable);
    111             shapeHolder.setX(x - 25f);
    112             shapeHolder.setY(y - 25f);
    113             int red = (int)(100 + Math.random() * 155);
    114             int green = (int)(100 + Math.random() * 155);
    115             int blue = (int)(100 + Math.random() * 155);
    116             int color = 0xff000000 | red << 16 | green << 8 | blue;
    117             Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
    118             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
    119             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
    120                     50f, color, darkColor, Shader.TileMode.CLAMP);
    121             paint.setShader(gradient);
    122             shapeHolder.setPaint(paint);
    123             balls.add(shapeHolder);
    124             return shapeHolder;
    125         }
    126 
    127         @Override
    128         protected void onDraw(Canvas canvas) {
    129             for (int i = 0; i < balls.size(); ++i) {
    130                 ShapeHolder shapeHolder = balls.get(i);
    131                 canvas.save();
    132                 canvas.translate(shapeHolder.getX(), shapeHolder.getY());
    133                 shapeHolder.getShape().draw(canvas);
    134                 canvas.restore();
    135             }
    136         }
    137 
    138         public void startAnimation() {
    139             createAnimation();
    140             animation.start();
    141         }
    142 
    143         public void onAnimationUpdate(ValueAnimator animation) {
    144             invalidate();
    145         }
    146 
    147     }
    148 }