2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.apis.animation;
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import android.graphics.drawable.ColorDrawable;
22 import com.example.android.apis.R;
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.graphics.drawable.shapes.RectShape;
34 import android.os.Bundle;
35 import android.view.MotionEvent;
36 import android.view.View;
37 import android.view.animation.AccelerateInterpolator;
38 import android.view.animation.DecelerateInterpolator;
39 import android.widget.LinearLayout;
41 import java.util.ArrayList;
44 public class BouncingBalls extends Activity {
45 /** Called when the activity is first created. */
47 public void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.bouncing_balls);
50 LinearLayout container = (LinearLayout) findViewById(R.id.container);
51 container.addView(new MyAnimationView(this));
54 public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener,
55 Animator.AnimatorListener {
57 private static final int RED = 0xffFF8080;
58 private static final int BLUE = 0xff8080FF;
59 private static final int CYAN = 0xff80ffff;
60 private static final int GREEN = 0xff80ff80;
62 public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
63 AnimatorSet animation = null;
65 public MyAnimationView(Context context) {
68 // Create a colored background
69 ColorDrawable background = new ColorDrawable(RED);
70 setBackgroundDrawable(background);
72 // Animate background color
73 ValueAnimator colorAnim = ObjectAnimator.ofInt(background, "color", BLUE);
74 colorAnim.setDuration(3000);
75 colorAnim.setEvaluator(new RGBEvaluator());
76 colorAnim.setRepeatCount(ValueAnimator.INFINITE);
77 colorAnim.setRepeatMode(ValueAnimator.REVERSE);
78 colorAnim.addUpdateListener(this); // forces invalidation to get the redraw
83 public boolean onTouchEvent(MotionEvent event) {
84 if (event.getAction() != MotionEvent.ACTION_DOWN &&
85 event.getAction() != MotionEvent.ACTION_MOVE) {
88 ShapeHolder newBall = addBall(event.getX(), event.getY());
90 // Bouncing animation with squash and stretch
91 float startY = newBall.getY();
92 float endY = getHeight() - 50f;
93 float h = (float)getHeight();
94 float eventY = event.getY();
95 int duration = (int)(500 * ((h - eventY)/h));
96 ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY);
97 bounceAnim.setDuration(duration);
98 bounceAnim.setInterpolator(new AccelerateInterpolator());
99 ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(),
100 newBall.getX() - 25f);
101 squashAnim1.setDuration(duration/4);
102 squashAnim1.setRepeatCount(1);
103 squashAnim1.setRepeatMode(ValueAnimator.REVERSE);
104 squashAnim1.setInterpolator(new DecelerateInterpolator());
105 ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "width", newBall.getWidth(),
106 newBall.getWidth() + 50);
107 squashAnim2.setDuration(duration/4);
108 squashAnim2.setRepeatCount(1);
109 squashAnim2.setRepeatMode(ValueAnimator.REVERSE);
110 squashAnim2.setInterpolator(new DecelerateInterpolator());
111 ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY,
113 stretchAnim1.setDuration(duration/4);
114 stretchAnim1.setRepeatCount(1);
115 stretchAnim1.setInterpolator(new DecelerateInterpolator());
116 stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);
117 ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "height",
118 newBall.getHeight(), newBall.getHeight() - 25);
119 stretchAnim2.setDuration(duration/4);
120 stretchAnim2.setRepeatCount(1);
121 stretchAnim2.setInterpolator(new DecelerateInterpolator());
122 stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);
123 ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "y", endY,
125 bounceBackAnim.setDuration(duration);
126 bounceBackAnim.setInterpolator(new DecelerateInterpolator());
127 // Sequence the down/squash&stretch/up animations
128 AnimatorSet bouncer = new AnimatorSet();
129 bouncer.play(bounceAnim).before(squashAnim1);
130 bouncer.play(squashAnim1).with(squashAnim2);
131 bouncer.play(squashAnim1).with(stretchAnim1);
132 bouncer.play(squashAnim1).with(stretchAnim2);
133 bouncer.play(bounceBackAnim).after(stretchAnim2);
135 // Fading animation - remove the ball when the animation is done
136 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
137 fadeAnim.setDuration(250);
138 fadeAnim.addListener(this);
140 // Sequence the two animations to play one after the other
141 AnimatorSet animatorSet = new AnimatorSet();
142 animatorSet.play(bouncer).before(fadeAnim);
144 // Start the animation
150 private ShapeHolder addBall(float x, float y) {
151 OvalShape circle = new OvalShape();
152 circle.resize(50f, 50f);
153 ShapeDrawable drawable = new ShapeDrawable(circle);
154 ShapeHolder shapeHolder = new ShapeHolder(drawable);
155 shapeHolder.setX(x - 25f);
156 shapeHolder.setY(y - 25f);
157 int red = (int)(Math.random() * 255);
158 int green = (int)(Math.random() * 255);
159 int blue = (int)(Math.random() * 255);
160 int color = 0xff000000 | red << 16 | green << 8 | blue;
161 Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
162 int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
163 RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
164 50f, color, darkColor, Shader.TileMode.CLAMP);
165 paint.setShader(gradient);
166 shapeHolder.setPaint(paint);
167 balls.add(shapeHolder);
172 protected void onDraw(Canvas canvas) {
173 for (int i = 0; i < balls.size(); ++i) {
174 ShapeHolder shapeHolder = balls.get(i);
176 canvas.translate(shapeHolder.getX(), shapeHolder.getY());
177 shapeHolder.getShape().draw(canvas);
182 public void onAnimationUpdate(ValueAnimator animation) {
187 public void onAnimationCancel(Animator animation) {
191 public void onAnimationEnd(Animator animation) {
192 balls.remove(((ObjectAnimator)animation).getTarget());
197 public void onAnimationRepeat(Animator animation) {
201 public void onAnimationStart(Animator animation) {