1 /* 2 * Copyright (C) 2012 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 android.animation.cts; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorListenerAdapter; 21 import android.animation.AnimatorSet; 22 import android.animation.ArgbEvaluator; 23 import android.animation.ObjectAnimator; 24 import android.animation.TimeInterpolator; 25 import android.animation.ValueAnimator; 26 import android.app.Activity; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.graphics.Canvas; 30 import android.graphics.Paint; 31 import android.graphics.RadialGradient; 32 import android.graphics.Shader; 33 import android.graphics.drawable.ShapeDrawable; 34 import android.graphics.drawable.shapes.OvalShape; 35 import android.os.Bundle; 36 import android.view.MotionEvent; 37 import android.view.View; 38 import android.view.ViewGroup; 39 import android.view.animation.AccelerateInterpolator; 40 41 import java.util.ArrayList; 42 43 public class AnimationActivity extends Activity { 44 private static final String BALL_HEIGHT = "ballwidth"; 45 private static final String BALL_WIDTH = "ballheight"; 46 private static final String STARTX = "startx"; 47 private static final String STARTY = "starty"; 48 private static final String DELTAX = "deltax"; 49 private static final String DELTAY = "deltay"; 50 private static final String DURATION = "duration"; 51 52 float mBallHeight = 50f; 53 float mBallWidth = 50f; 54 float mStartX = 200f; 55 float mStartY = 200f; 56 float mDeltaX = 200f; 57 float mDeltaY = 200f; 58 long mDuration = 1000; 59 public AnimationView view = null; 60 61 @Override onCreate(Bundle bundle)62 public void onCreate(Bundle bundle){ 63 super.onCreate(bundle); 64 Intent intent = this.getIntent(); 65 Bundle extras = intent.getExtras(); 66 if(extras != null) { 67 mBallHeight = extras.getFloat(BALL_HEIGHT); 68 mBallWidth = extras.getFloat(BALL_WIDTH); 69 mStartX = extras.getFloat(STARTX); 70 mStartY = extras.getFloat(STARTY); 71 mDeltaX = extras.getFloat(DELTAX); 72 mDeltaY = extras.getFloat(DELTAY); 73 mDuration = extras.getInt(DURATION); 74 } 75 setContentView(R.layout.animation_main); 76 view = new AnimationView(this); 77 ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container); 78 viewGroup.addView(view); 79 } 80 createAnimator(Object object,String propertyName, long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator, float start, float end)81 public ValueAnimator createAnimator(Object object,String propertyName, long duration, 82 int repeatCount, int repeatMode, 83 TimeInterpolator timeInterpolator, float start, float end){ 84 ValueAnimator valueAnimator = ObjectAnimator.ofFloat(object, propertyName, start,end); 85 valueAnimator.setDuration(duration); 86 valueAnimator.setRepeatCount(repeatCount); 87 valueAnimator.setInterpolator(timeInterpolator); 88 valueAnimator.setRepeatMode(repeatMode); 89 return valueAnimator; 90 } 91 createAnimatorWithRepeatMode(int repeatMode)92 public ValueAnimator createAnimatorWithRepeatMode(int repeatMode) { 93 return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE, repeatMode, 94 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY); 95 } 96 createAnimatorWithRepeatCount(int repeatCount)97 public ValueAnimator createAnimatorWithRepeatCount(int repeatCount) { 98 return createAnimator(view.newBall, "y", 1000, repeatCount, ValueAnimator.REVERSE, 99 new AccelerateInterpolator(), mStartY, mStartY + mDeltaY); 100 } 101 createAnimatorWithDuration(long duration)102 public ValueAnimator createAnimatorWithDuration(long duration) { 103 return createAnimator(view.newBall, "y", duration ,ValueAnimator.INFINITE, 104 ValueAnimator.REVERSE,new AccelerateInterpolator(), mStartY, mStartY + mDeltaY); 105 } 106 createAnimatorWithInterpolator(TimeInterpolator interpolator)107 public ValueAnimator createAnimatorWithInterpolator(TimeInterpolator interpolator){ 108 return createAnimator(view.newBall, "y", 1000, ValueAnimator.INFINITE, 109 ValueAnimator.REVERSE, interpolator, mStartY, mStartY + mDeltaY); 110 } 111 createObjectAnimatorForInt(Object object,String propertyName, long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator, int start, int end)112 public ValueAnimator createObjectAnimatorForInt(Object object,String propertyName, 113 long duration, int repeatCount, int repeatMode, TimeInterpolator timeInterpolator, 114 int start, int end) { 115 ObjectAnimator objAnimator = ObjectAnimator.ofInt(object, propertyName, start,end); 116 objAnimator.setDuration(duration); 117 118 objAnimator.setRepeatCount(repeatCount); 119 objAnimator.setInterpolator(timeInterpolator); 120 objAnimator.setRepeatMode(repeatMode); 121 return objAnimator; 122 } 123 createObjectAnimatorForInt()124 public ValueAnimator createObjectAnimatorForInt() { 125 ObjectAnimator objAnimator = ObjectAnimator.ofInt(view.newBall, "y", (int)mStartY, 126 (int)(mStartY + mDeltaY)); 127 objAnimator.setDuration(mDuration); 128 129 objAnimator.setRepeatCount(ValueAnimator.INFINITE); 130 objAnimator.setInterpolator(new AccelerateInterpolator()); 131 objAnimator.setRepeatMode(ValueAnimator.REVERSE); 132 return objAnimator; 133 } 134 startAnimation(long duration)135 public void startAnimation(long duration){ 136 ValueAnimator bounceAnimator = ObjectAnimator.ofFloat(view.newBall, "y", mStartY, 137 mStartY + mDeltaY); 138 bounceAnimator.setDuration(duration); 139 bounceAnimator.setRepeatCount(ValueAnimator.INFINITE); 140 bounceAnimator.setInterpolator(new AccelerateInterpolator()); 141 bounceAnimator.setRepeatMode(ValueAnimator.REVERSE); 142 view.bounceYAnimator = bounceAnimator; 143 view.startColorAnimator(); 144 view.animateBall(); 145 } 146 startSingleAnimation(Animator animator)147 public void startSingleAnimation(Animator animator) { 148 animator.setTarget(view.newBall); 149 animator.start(); 150 } 151 startAnimation(ValueAnimator valueAnimator)152 public void startAnimation(ValueAnimator valueAnimator){ 153 view.bounceYAnimator = valueAnimator; 154 view.startColorAnimator(); 155 view.animateBall(); 156 } 157 startAnimation(Animator animator)158 public void startAnimation(Animator animator){ 159 view.bounceYAnimator = (ValueAnimator)animator; 160 view.startColorAnimator(); 161 view.animateBall(); 162 } 163 startAnimation(ObjectAnimator bounceAnimator)164 public void startAnimation(ObjectAnimator bounceAnimator) { 165 view.bounceYAnimator = bounceAnimator; 166 view.startColorAnimator(); 167 view.animateBall(); 168 } 169 startAnimation(ObjectAnimator bounceAnimator, ObjectAnimator colorAnimator)170 public void startAnimation(ObjectAnimator bounceAnimator, ObjectAnimator colorAnimator) { 171 view.bounceYAnimator = bounceAnimator; 172 view.startColorAnimator(colorAnimator); 173 view.animateBall(); 174 } 175 startAnimatorSet(AnimatorSet set)176 public void startAnimatorSet(AnimatorSet set){ 177 view.startColorAnimator(); 178 view.animateBall(set); 179 } 180 startColorAnimation(ValueAnimator colorAnimator)181 public void startColorAnimation(ValueAnimator colorAnimator){ 182 view.startColorAnimator(colorAnimator); 183 } 184 185 public class AnimationView extends View { 186 public static final int RED = 0xffFF8080; 187 public static final int BLUE = 0xff8080FF; 188 public ShapeHolder newBall = null; 189 public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); 190 AnimatorSet animation = null; 191 public ValueAnimator bounceYAnimator; 192 public ValueAnimator bounceXAnimator; 193 public ValueAnimator colorAnimator; 194 AnimationView(Context context)195 public AnimationView(Context context) { 196 super(context); 197 newBall = addBall(mBallHeight, mBallWidth); 198 } 199 startColorAnimator()200 public void startColorAnimator() { 201 colorAnimator = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE); 202 colorAnimator.setDuration(1000); 203 colorAnimator.setEvaluator(new ArgbEvaluator()); 204 colorAnimator.setRepeatCount(ValueAnimator.INFINITE); 205 colorAnimator.setRepeatMode(ValueAnimator.REVERSE); 206 colorAnimator.start(); 207 } 208 startColorAnimator(ValueAnimator animator)209 public void startColorAnimator(ValueAnimator animator) { 210 this.colorAnimator = animator; 211 colorAnimator.start(); 212 } 213 214 @Override onTouchEvent(MotionEvent event)215 public boolean onTouchEvent(MotionEvent event) { 216 return true; 217 } 218 animateBall()219 public void animateBall() { 220 AnimatorSet bouncer = new AnimatorSet(); 221 bouncer.play(bounceYAnimator); 222 // Fading animation - remove the ball when the animation is done 223 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f); 224 fadeAnim.setDuration(250); 225 fadeAnim.addListener(new AnimatorListenerAdapter() { 226 @Override 227 public void onAnimationEnd(Animator animation) { 228 balls.remove(((ObjectAnimator)animation).getTarget()); 229 } 230 }); 231 232 // Sequence the two animations to play one after the other 233 AnimatorSet animatorSet = new AnimatorSet(); 234 animatorSet.play(bouncer).before(fadeAnim); 235 animatorSet.start(); 236 } 237 animateBall(AnimatorSet set)238 public void animateBall(AnimatorSet set) { 239 set.start(); 240 } 241 addBall(float x, float y)242 private ShapeHolder addBall(float x, float y) { 243 OvalShape circle = new OvalShape(); 244 circle.resize(x, y); 245 ShapeDrawable drawable = new ShapeDrawable(circle); 246 ShapeHolder shapeHolder = new ShapeHolder(drawable); 247 shapeHolder.setX(x - 25f); 248 shapeHolder.setY(y - 25f); 249 Paint paint = drawable.getPaint(); 250 shapeHolder.setPaint(paint); 251 int red = (int)(Math.random() * 255); 252 int green = (int)(Math.random() * 255); 253 int blue = (int)(Math.random() * 255); 254 int color = 0xff000000 | red << 16 | green << 8 | blue; 255 int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4; 256 RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 257 50f, color, darkColor, Shader.TileMode.CLAMP); 258 paint.setShader(gradient); 259 balls.add(shapeHolder); 260 return shapeHolder; 261 } 262 263 @Override onDraw(Canvas canvas)264 protected void onDraw(Canvas canvas) { 265 for (int i = 0; i < balls.size(); ++i) { 266 ShapeHolder shapeHolder = balls.get(i); 267 canvas.save(); 268 canvas.translate(shapeHolder.getX(), shapeHolder.getY()); 269 shapeHolder.getShape().draw(canvas); 270 canvas.restore(); 271 } 272 } 273 } 274 } 275 276