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.ObjectAnimator; 22 import android.animation.TypeEvaluator; 23 import android.animation.ValueAnimator; 24 import com.example.android.apis.R; 25 26 import java.util.ArrayList; 27 28 import android.app.Activity; 29 import android.content.Context; 30 import android.graphics.Canvas; 31 import android.graphics.Paint; 32 import android.graphics.RadialGradient; 33 import android.graphics.Shader; 34 import android.graphics.drawable.ShapeDrawable; 35 import android.graphics.drawable.shapes.OvalShape; 36 import android.os.Bundle; 37 import android.view.View; 38 import android.widget.Button; 39 import android.widget.LinearLayout; 40 41 public class CustomEvaluator extends Activity { 42 43 @Override onCreate(Bundle savedInstanceState)44 public void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.animator_custom_evaluator); 47 LinearLayout container = (LinearLayout) findViewById(R.id.container); 48 final MyAnimationView animView = new MyAnimationView(this); 49 container.addView(animView); 50 51 Button starter = (Button) findViewById(R.id.startButton); 52 starter.setOnClickListener(new View.OnClickListener() { 53 public void onClick(View v) { 54 animView.startAnimation(); 55 } 56 }); 57 } 58 59 public class XYHolder { 60 private float mX; 61 private float mY; 62 XYHolder(float x, float y)63 public XYHolder(float x, float y) { 64 mX = x; 65 mY = y; 66 } 67 getX()68 public float getX() { 69 return mX; 70 } 71 setX(float x)72 public void setX(float x) { 73 mX = x; 74 } 75 getY()76 public float getY() { 77 return mY; 78 } 79 setY(float y)80 public void setY(float y) { 81 mY = y; 82 } 83 } 84 85 public class XYEvaluator implements TypeEvaluator { evaluate(float fraction, Object startValue, Object endValue)86 public Object evaluate(float fraction, Object startValue, Object endValue) { 87 XYHolder startXY = (XYHolder) startValue; 88 XYHolder endXY = (XYHolder) endValue; 89 return new XYHolder(startXY.getX() + fraction * (endXY.getX() - startXY.getX()), 90 startXY.getY() + fraction * (endXY.getY() - startXY.getY())); 91 } 92 } 93 94 public class BallXYHolder { 95 96 private ShapeHolder mBall; 97 BallXYHolder(ShapeHolder ball)98 public BallXYHolder(ShapeHolder ball) { 99 mBall = ball; 100 } 101 setXY(XYHolder xyHolder)102 public void setXY(XYHolder xyHolder) { 103 mBall.setX(xyHolder.getX()); 104 mBall.setY(xyHolder.getY()); 105 } 106 getXY()107 public XYHolder getXY() { 108 return new XYHolder(mBall.getX(), mBall.getY()); 109 } 110 } 111 112 public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener { 113 114 public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>(); 115 ValueAnimator bounceAnim = null; 116 ShapeHolder ball = null; 117 BallXYHolder ballHolder = null; 118 MyAnimationView(Context context)119 public MyAnimationView(Context context) { 120 super(context); 121 ball = createBall(25, 25); 122 ballHolder = new BallXYHolder(ball); 123 } 124 createAnimation()125 private void createAnimation() { 126 if (bounceAnim == null) { 127 XYHolder startXY = new XYHolder(0f, 0f); 128 XYHolder endXY = new XYHolder(300f, 500f); 129 bounceAnim = ObjectAnimator.ofObject(ballHolder, "xY", 130 new XYEvaluator(), endXY); 131 bounceAnim.setDuration(1500); 132 bounceAnim.addUpdateListener(this); 133 } 134 } 135 startAnimation()136 public void startAnimation() { 137 createAnimation(); 138 bounceAnim.start(); 139 } 140 createBall(float x, float y)141 private ShapeHolder createBall(float x, float y) { 142 OvalShape circle = new OvalShape(); 143 circle.resize(50f, 50f); 144 ShapeDrawable drawable = new ShapeDrawable(circle); 145 ShapeHolder shapeHolder = new ShapeHolder(drawable); 146 shapeHolder.setX(x - 25f); 147 shapeHolder.setY(y - 25f); 148 int red = (int)(Math.random() * 255); 149 int green = (int)(Math.random() * 255); 150 int blue = (int)(Math.random() * 255); 151 int color = 0xff000000 | red << 16 | green << 8 | blue; 152 Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG); 153 int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4; 154 RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 155 50f, color, darkColor, Shader.TileMode.CLAMP); 156 paint.setShader(gradient); 157 shapeHolder.setPaint(paint); 158 return shapeHolder; 159 } 160 161 @Override onDraw(Canvas canvas)162 protected void onDraw(Canvas canvas) { 163 canvas.save(); 164 canvas.translate(ball.getX(), ball.getY()); 165 ball.getShape().draw(canvas); 166 canvas.restore(); 167 } 168 onAnimationUpdate(ValueAnimator animation)169 public void onAnimationUpdate(ValueAnimator animation) { 170 invalidate(); 171 } 172 173 } 174 }