Animator
类提供了创建动画的基本结构,但是一般使用的是它的子类:
ValueAnimator、ObjectAnimator、AnimatorSet
下面代码来自ApiDemo中的AnimationCloning类。与之关联的文件有ShapeHolder类、animation_cloning.xml
package cn.com.api.animation;
import android.widget.Button;
import android.animation.*;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class AnimationCloning extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_cloning); // 设置布局,布局xml中只包含了一个线性布局和一个Button
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);// 将自定义的View加入到线性布局中
Button starter = (Button) findViewById(R.id.startButton);
starter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animView.startAnimation();//点击button后开始动画
}
});
}
//自定义view类
public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener {
public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
AnimatorSet animation = null; // 总的动画集合
private float mDensity; // 屏幕密度
public MyAnimationView(Context context) {
super(context);
mDensity = getContext().getResources().getDisplayMetrics().density;// 得到密度值
ShapeHolder ball0 = addBall(50f, 25f);
ShapeHolder ball1 = addBall(150f, 25f);
ShapeHolder ball2 = addBall(250f, 25f);
ShapeHolder ball3 = addBall(350f, 25f);
}
private void createAnimation() {
if (animation == null) {
// ===============================================
// 第1个球的动画效果:用ObjectAnimator
// 用工厂方法构造对象:用ofFloat()因为属性值是float类型
// 第1个参数为目标对象:balls.get(0)
// 第2个参数为属性名:y 这里要求目标对象要有“set属性名()”的方法。
// 后面是可变参数,表明属性目标值,一个参数表明是终止值(对象要有get属性方法)
// 可变参数的个数为2时,表明第一个是起始值,第二个是终止值;更多个参数时,动画属性值会逐个经过这些值
ObjectAnimator anim1 = ObjectAnimator.ofFloat(balls.get(0), "y",
0f, getHeight() - balls.get(0).getHeight()).setDuration(500);
// 第二个球的动画效果:clone动画效果1,但是重新设置目标物体
ObjectAnimator anim2 = anim1.clone();
anim2.setTarget(balls.get(1));
// 因为前两个动画完全相同,所以设置刷新监听的时候就只设置了一个(它们刷新的是同一个View)
anim1.addUpdateListener(this);
// 第三个球的动画效果:先加速下落,再减速上升
ShapeHolder ball2 = balls.get(2);
// 动画效果:落下效果
ObjectAnimator animDown = ObjectAnimator.ofFloat(ball2, "y",
0f, getHeight() - ball2.getHeight()).setDuration(500);
// 落下效果改变了插值Interpolator,设置为加速
animDown.setInterpolator(new AccelerateInterpolator());
// 动画效果:上升效果
ObjectAnimator animUp = ObjectAnimator.ofFloat(ball2, "y",
getHeight() - ball2.getHeight(), 0f).setDuration(500);
animUp.setInterpolator(new DecelerateInterpolator()); // 上升效果设置为减速上升
// 用一个AnimatorSet对象将下落效果和上升效果顺序播放
AnimatorSet s1 = new AnimatorSet();
s1.playSequentially(animDown, animUp); // 顺序播放效果,参数个数可变
animDown.addUpdateListener(this); // 下落动画刷新View
animUp.addUpdateListener(this); // 上升动画刷新View
// 第四个球的动画效果
// 另一个AnimatorSet克隆了上一个set,更换了对象
AnimatorSet s2 = (AnimatorSet) s1.clone();
s2.setTarget(balls.get(3));
// 用一个总的AnimatorSet对象管理以上所有动画
animation = new AnimatorSet();
animation.playTogether(anim1, anim2, s1); // 并行
animation.playSequentially(s1, s2); // 串行
}
}
// 在指定位置加上球形
private ShapeHolder addBall(float x, float y) {
OvalShape circle = new OvalShape(); //创建一个椭圆
circle.resize(50f * mDensity, 50f * mDensity); //设置椭圆的宽、高
ShapeDrawable drawable = new ShapeDrawable(circle); //将圆包装成Drawable对象
ShapeHolder shapeHolder = new ShapeHolder(drawable); //创建一个ShapeHolder对象
shapeHolder.setX(x - 25f); //设置ShapeHolder对象的x、y坐标
shapeHolder.setY(y - 25f);
int red = (int)(100 + Math.random() * 155);
int green = (int)(100 + Math.random() * 155);
int blue = (int)(100 + Math.random() * 155);
//将red、green、bule三个随机数组合成ARGB颜色
int color = 0xff000000 | red << 16 | green << 8 | blue;
//获得drawable上关联的画笔
Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
//将red、green、bule三个随机数除以4得到商值组合成ARGB颜色
int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
50f, color, darkColor, Shader.TileMode.CLAMP); //创建圆形渐变
paint.setShader(gradient);
shapeHolder.setPaint(paint); //为shapeHolder设置paint画笔
balls.add(shapeHolder);
return shapeHolder;
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < balls.size(); ++i) { // 遍历并绘制每一个球形对象
ShapeHolder shapeHolder = balls.get(i);
canvas.save(); //保存canvas的当前坐标系统
//坐标变换:将画布坐标系统平移到shapeHolder的X、Y坐标处
canvas.translate(shapeHolder.getX(), shapeHolder.getY());
shapeHolder.getShape().draw(canvas); //将shapeHolder持有的圆形绘制在canvas上
canvas.restore(); //恢复canvas的坐标系统
}
}
public void startAnimation() {
createAnimation();
animation.start(); // 这里开始播放动画
}
public void onAnimationUpdate(ValueAnimator animation) {
// 在参数更新的时候invalidate,刷新整个View的绘制
// 否则onDraw不会被调用,即看不到View外观的改变
invalidate();
}
}
}
运行效果图: