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 } 82 createAnimation()83 private void createAnimation() { 84 Context appContext = AnimationLoading.this; 85 86 if (animation == null) { 87 ObjectAnimator anim = (ObjectAnimator) AnimatorInflater. 88 loadAnimator(appContext, R.anim.object_animator); 89 anim.addUpdateListener(this); 90 anim.setTarget(balls.get(0)); 91 92 ValueAnimator fader = (ValueAnimator) AnimatorInflater. 93 loadAnimator(appContext, R.anim.animator); 94 fader.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 95 public void onAnimationUpdate(ValueAnimator animation) { 96 balls.get(1).setAlpha((Float) animation.getAnimatedValue()); 97 } 98 }); 99 100 AnimatorSet seq = 101 (AnimatorSet) AnimatorInflater.loadAnimator(appContext, 102 R.anim.animator_set); 103 seq.setTarget(balls.get(2)); 104 105 ObjectAnimator colorizer = (ObjectAnimator) AnimatorInflater. 106 loadAnimator(appContext, R.anim.color_animator); 107 colorizer.setTarget(balls.get(3)); 108 109 animation = new AnimatorSet(); 110 ((AnimatorSet) animation).playTogether(anim, fader, seq, colorizer); 111 } 112 } 113 startAnimation()114 public void startAnimation() { 115 createAnimation(); 116 animation.start(); 117 } 118 createBall(float x, float y)119 private ShapeHolder createBall(float x, float y) { 120 OvalShape circle = new OvalShape(); 121 circle.resize(BALL_SIZE, BALL_SIZE); 122 ShapeDrawable drawable = new ShapeDrawable(circle); 123 ShapeHolder shapeHolder = new ShapeHolder(drawable); 124 shapeHolder.setX(x); 125 shapeHolder.setY(y); 126 return shapeHolder; 127 } 128 addBall(float x, float y, int color)129 private void addBall(float x, float y, int color) { 130 ShapeHolder shapeHolder = createBall(x, y); 131 shapeHolder.setColor(color); 132 balls.add(shapeHolder); 133 } 134 addBall(float x, float y)135 private void addBall(float x, float y) { 136 ShapeHolder shapeHolder = createBall(x, y); 137 int red = (int)(100 + Math.random() * 155); 138 int green = (int)(100 + Math.random() * 155); 139 int blue = (int)(100 + Math.random() * 155); 140 int color = 0xff000000 | red << 16 | green << 8 | blue; 141 Paint paint = shapeHolder.getShape().getPaint(); 142 int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4; 143 RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 144 50f, color, darkColor, Shader.TileMode.CLAMP); 145 paint.setShader(gradient); 146 balls.add(shapeHolder); 147 } 148 149 @Override onDraw(Canvas canvas)150 protected void onDraw(Canvas canvas) { 151 for (ShapeHolder ball : balls) { 152 canvas.translate(ball.getX(), ball.getY()); 153 ball.getShape().draw(canvas); 154 canvas.translate(-ball.getX(), -ball.getY()); 155 } 156 } 157 onAnimationUpdate(ValueAnimator animation)158 public void onAnimationUpdate(ValueAnimator animation) { 159 160 invalidate(); 161 ShapeHolder ball = balls.get(0); 162 ball.setY((Float)animation.getAnimatedValue()); 163 } 164 } 165 }