AnimationSeeking.java revision b9461fc8ef49e050b97d5305f32bc50099c78a16
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 17package 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. 21import com.example.android.apis.R; 22 23import java.util.ArrayList; 24 25import android.animation.Animatable; 26import android.animation.Animator; 27import android.animation.PropertyAnimator; 28import android.animation.Sequencer; 29import android.animation.Animatable.AnimatableListener; 30import android.app.Activity; 31import android.content.Context; 32import android.graphics.Canvas; 33import android.graphics.Paint; 34import android.graphics.RadialGradient; 35import android.graphics.Shader; 36import android.graphics.drawable.ShapeDrawable; 37import android.graphics.drawable.shapes.OvalShape; 38import android.os.Bundle; 39import android.view.View; 40import android.view.animation.BounceInterpolator; 41import android.widget.Button; 42import android.widget.LinearLayout; 43import android.widget.SeekBar; 44 45/** 46 * This application demonstrates the seeking capability of Animator. The SeekBar in the 47 * UI allows you to set the position of the animation. Pressing the Run button will play from 48 * the current position of the animation. 49 */ 50public class AnimationSeeking extends Activity { 51 52 private static final int DURATION = 1500; 53 private SeekBar mSeekBar; 54 55 /** Called when the activity is first created. */ 56 @Override 57 public void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 setContentView(R.layout.animation_seeking); 60 LinearLayout container = (LinearLayout) findViewById(R.id.container); 61 final MyAnimationView animView = new MyAnimationView(this); 62 container.addView(animView); 63 64 Button starter = (Button) findViewById(R.id.startButton); 65 starter.setOnClickListener(new View.OnClickListener() { 66 67 @Override 68 public void onClick(View v) { 69 animView.startAnimation(); 70 } 71 }); 72 73 mSeekBar = (SeekBar) findViewById(R.id.seekBar); 74 mSeekBar.setMax(DURATION); 75 mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 76 77 @Override 78 public void onStopTrackingTouch(SeekBar seekBar) { 79 } 80 81 @Override 82 public void onStartTrackingTouch(SeekBar seekBar) { 83 } 84 85 @Override 86 public void onProgressChanged(SeekBar seekBar, int progress, 87 boolean fromUser) { 88 // prevent seeking on app creation 89 if (animView.getHeight() != 0) { 90 animView.seek(progress); 91 } 92 } 93 }); 94 } 95 96 public class MyAnimationView extends View implements Animator.AnimatorUpdateListener, AnimatableListener { 97 98 private static final int RED = 0xffFF8080; 99 private static final int BLUE = 0xff8080FF; 100 private static final int CYAN = 0xff80ffff; 101 private static final int GREEN = 0xff80ff80; 102 private static final float BALL_SIZE = 100f; 103 104 public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); 105 Sequencer animation = null; 106 Animator bounceAnim = null; 107 ShapeHolder ball = null; 108 109 public MyAnimationView(Context context) { 110 super(context); 111 ball = addBall(200, 0); 112 } 113 114 private void createAnimation() { 115 if (bounceAnim == null) { 116 bounceAnim = new PropertyAnimator(1500, ball, "y", 117 ball.getY(), getHeight() - BALL_SIZE); 118 bounceAnim.setInterpolator(new BounceInterpolator()); 119 bounceAnim.addUpdateListener(this); 120 } 121 } 122 123 public void startAnimation() { 124 createAnimation(); 125 bounceAnim.start(); 126 } 127 128 public void seek(long seekTime) { 129 createAnimation(); 130 bounceAnim.setCurrentPlayTime(seekTime); 131 } 132 133 private ShapeHolder addBall(float x, float y) { 134 OvalShape circle = new OvalShape(); 135 circle.resize(BALL_SIZE, BALL_SIZE); 136 ShapeDrawable drawable = new ShapeDrawable(circle); 137 ShapeHolder shapeHolder = new ShapeHolder(drawable); 138 shapeHolder.setX(x); 139 shapeHolder.setY(y); 140 int red = (int)(100 + Math.random() * 155); 141 int green = (int)(100 + Math.random() * 155); 142 int blue = (int)(100 + Math.random() * 155); 143 int color = 0xff000000 | red << 16 | green << 8 | blue; 144 Paint paint = drawable.getPaint(); 145 int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4; 146 RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 147 50f, color, darkColor, Shader.TileMode.CLAMP); 148 paint.setShader(gradient); 149 shapeHolder.setPaint(paint); 150 balls.add(shapeHolder); 151 return shapeHolder; 152 } 153 154 @Override 155 protected void onDraw(Canvas canvas) { 156 canvas.translate(ball.getX(), ball.getY()); 157 ball.getShape().draw(canvas); 158 } 159 160 public void onAnimationUpdate(Animator animation) { 161 invalidate(); 162 long playtime = bounceAnim.getCurrentPlayTime(); 163 //mSeekBar.setProgress((int)playtime); 164 } 165 166 @Override 167 public void onAnimationCancel(Animatable animation) { 168 } 169 170 @Override 171 public void onAnimationEnd(Animatable animation) { 172 balls.remove(((PropertyAnimator)animation).getTarget()); 173 174 } 175 176 @Override 177 public void onAnimationRepeat(Animatable animation) { 178 } 179 180 @Override 181 public void onAnimationStart(Animatable animation) { 182 } 183 } 184}