• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AnimatorInflater;
22 import android.animation.AnimatorSet;
23 import android.animation.ObjectAnimator;
24 import android.graphics.Color;
25 import com.example.android.apis.R;
26 
27 import java.util.ArrayList;
28 
29 import android.animation.Animator;
30 import android.animation.ValueAnimator;
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.widget.Button;
42 import android.widget.LinearLayout;
43 
44 /**
45  * This application demonstrates loading Animator objects from XML resources.
46  */
47 public class AnimationLoading extends Activity {
48 
49     private static final int DURATION = 1500;
50 
51     /** Called when the activity is first created. */
52     @Override
onCreate(Bundle savedInstanceState)53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         setContentView(R.layout.animation_loading);
56         LinearLayout container = (LinearLayout) findViewById(R.id.container);
57         final MyAnimationView animView = new MyAnimationView(this);
58         container.addView(animView);
59 
60         Button starter = (Button) findViewById(R.id.startButton);
61         starter.setOnClickListener(new View.OnClickListener() {
62             public void onClick(View v) {
63                 animView.startAnimation();
64             }
65         });
66     }
67 
68     public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener {
69 
70         private static final float BALL_SIZE = 100f;
71 
72         public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
73         Animator animation = null;
74 
MyAnimationView(Context context)75         public MyAnimationView(Context context) {
76             super(context);
77             addBall(50, 50);
78             addBall(200, 50);
79             addBall(350, 50);
80             addBall(500, 50, Color.GREEN);
81             addBall(650, 50);
82             addBall(800, 50);
83             addBall(950, 50);
84             addBall(800, 50, Color.YELLOW);
85         }
86 
createAnimation()87         private void createAnimation() {
88             Context appContext = AnimationLoading.this;
89 
90             if (animation == null) {
91                 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.
92                         loadAnimator(appContext, R.anim.object_animator);
93                 anim.addUpdateListener(this);
94                 anim.setTarget(balls.get(0));
95 
96                 ValueAnimator fader = (ValueAnimator) AnimatorInflater.
97                         loadAnimator(appContext, R.anim.animator);
98                 fader.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
99                     public void onAnimationUpdate(ValueAnimator animation) {
100                         balls.get(1).setAlpha((Float) animation.getAnimatedValue());
101                     }
102                 });
103 
104                 AnimatorSet seq =
105                         (AnimatorSet) AnimatorInflater.loadAnimator(appContext,
106                         R.anim.animator_set);
107                 seq.setTarget(balls.get(2));
108 
109                 ObjectAnimator colorizer = (ObjectAnimator) AnimatorInflater.
110                         loadAnimator(appContext, R.anim.color_animator);
111                 colorizer.setTarget(balls.get(3));
112 
113                 ObjectAnimator animPvh = (ObjectAnimator) AnimatorInflater.
114                         loadAnimator(appContext, R.anim.object_animator_pvh);
115                 animPvh.setTarget(balls.get(4));
116 
117 
118                 ObjectAnimator animPvhKf = (ObjectAnimator) AnimatorInflater.
119                         loadAnimator(appContext, R.anim.object_animator_pvh_kf);
120                 animPvhKf.setTarget(balls.get(5));
121 
122                 ValueAnimator faderKf = (ValueAnimator) AnimatorInflater.
123                         loadAnimator(appContext, R.anim.value_animator_pvh_kf);
124                 faderKf.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
125                     public void onAnimationUpdate(ValueAnimator animation) {
126                         balls.get(6).setAlpha((Float) animation.getAnimatedValue());
127                     }
128                 });
129 
130                 // This animation has an accelerate interpolator applied on each
131                 // keyframe interval. In comparison, the animation defined in
132                 // R.anim.object_animator_pvh_kf uses the default linear interpolator
133                 // throughout the animation. As these two animations use the
134                 // exact same path, the effect of the per-keyframe interpolator
135                 // has been made obvious.
136                 ObjectAnimator animPvhKfInterpolated = (ObjectAnimator) AnimatorInflater.
137                         loadAnimator(appContext, R.anim.object_animator_pvh_kf_interpolated);
138                 animPvhKfInterpolated.setTarget(balls.get(7));
139 
140                 animation = new AnimatorSet();
141                 ((AnimatorSet) animation).playTogether(anim, fader, seq, colorizer, animPvh,
142                         animPvhKf, faderKf, animPvhKfInterpolated);
143 
144             }
145         }
146 
startAnimation()147         public void startAnimation() {
148             createAnimation();
149             animation.start();
150         }
151 
createBall(float x, float y)152         private ShapeHolder createBall(float x, float y) {
153             OvalShape circle = new OvalShape();
154             circle.resize(BALL_SIZE, BALL_SIZE);
155             ShapeDrawable drawable = new ShapeDrawable(circle);
156             ShapeHolder shapeHolder = new ShapeHolder(drawable);
157             shapeHolder.setX(x);
158             shapeHolder.setY(y);
159             return shapeHolder;
160         }
161 
addBall(float x, float y, int color)162         private void addBall(float x, float y, int color) {
163             ShapeHolder shapeHolder = createBall(x, y);
164             shapeHolder.setColor(color);
165             balls.add(shapeHolder);
166         }
167 
addBall(float x, float y)168         private void addBall(float x, float y) {
169             ShapeHolder shapeHolder = createBall(x, y);
170             int red = (int)(100 + Math.random() * 155);
171             int green = (int)(100 + Math.random() * 155);
172             int blue = (int)(100 + Math.random() * 155);
173             int color = 0xff000000 | red << 16 | green << 8 | blue;
174             Paint paint = shapeHolder.getShape().getPaint();
175             int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
176             RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
177                     50f, color, darkColor, Shader.TileMode.CLAMP);
178             paint.setShader(gradient);
179             balls.add(shapeHolder);
180         }
181 
182         @Override
onDraw(Canvas canvas)183         protected void onDraw(Canvas canvas) {
184             for (ShapeHolder ball : balls) {
185                 canvas.translate(ball.getX(), ball.getY());
186                 ball.getShape().draw(canvas);
187                 canvas.translate(-ball.getX(), -ball.getY());
188             }
189         }
190 
onAnimationUpdate(ValueAnimator animation)191         public void onAnimationUpdate(ValueAnimator animation) {
192 
193             invalidate();
194             ShapeHolder ball = balls.get(0);
195             ball.setY((Float)animation.getAnimatedValue());
196         }
197     }
198 }