AnimationSeeking 展示了 我们可以 自己设置动画播放的时间(也就是小球开始播放时候的状态 , 本案例是小球的y位置)
/**
* This application demonstrates the seeking capability of ValueAnimator. The SeekBar in the
* UI allows you to set the position of the animation. Pressing the Run button will play from
* the current position of the animation.
*/
public class AnimationSeeking extends Activity {
private static final int DURATION = 5000;
private SeekBar mSeekBar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_seeking);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
Button starter = (Button) findViewById(R.id.startButton);
starter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animView.startAnimation();
}
});
mSeekBar = (SeekBar) findViewById(R.id.seekBar);
mSeekBar.setMax(DURATION);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// prevent seeking on app creation
if (animView.getHeight() != 0) {
animView.seek(progress);
}
}
});
}
public class MyAnimationView extends View implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
private static final int RED = 0xffFF8080;
private static final int BLUE = 0xff8080FF;
private static final int CYAN = 0xff80ffff;
private static final int GREEN = 0xff80ff80;
private static final float BALL_SIZE = 100f;
public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
AnimatorSet animation = null;
ValueAnimator bounceAnim = null;
ShapeHolder ball = null;
public MyAnimationView(Context context) {
super(context);
ball = addBall(200, 0);
}
private void createAnimation() {
if (bounceAnim == null) {
bounceAnim = ObjectAnimator.ofFloat(ball, "y",
ball.getY(), getHeight() - BALL_SIZE).setDuration(DURATION);
bounceAnim.setInterpolator(new CycleInterpolator(2));//循环加速器,执行两次
bounceAnim.addUpdateListener(this);
}
}
public void startAnimation() {
createAnimation();
bounceAnim.start();
}
public void seek(long seekTime) {
createAnimation();
bounceAnim.setCurrentPlayTime(seekTime); //设置当前当前动画从什么时间开始播放
}
private ShapeHolder addBall(float x, float y) {
OvalShape circle = new OvalShape();
circle.resize(BALL_SIZE, BALL_SIZE);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x);
shapeHolder.setY(y);
int red = (int)(100 + Math.random() * 155);
int green = (int)(100 + Math.random() * 155);
int blue = (int)(100 + Math.random() * 155);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint();
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);
balls.add(shapeHolder);
return shapeHolder;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate(ball.getX(), ball.getY());
ball.getShape().draw(canvas);
}
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
long playtime = bounceAnim.getCurrentPlayTime();//可以获取当前的播放时间
mSeekBar.setProgress((int)playtime);
}
public void onAnimationCancel(Animator animation) {
}
public void onAnimationEnd(Animator animation) {
balls.remove(((ObjectAnimator)animation).getTarget());
}
public void onAnimationRepeat(Animator animation) {
}
public void onAnimationStart(Animator animation) {
}
}
}
可以监听到 animator的 开始 结束 取消 重复 等事件
/**
* This demo shows how the AnimatorListener events work.
*/
public class AnimatorEvents extends Activity {
private static final int OFF = 0x77FFFFFF;
private static final int ON = 0xFFFFFFFF;
TextView startText, repeatText, cancelText, endText;
TextView startTextAnimator, repeatTextAnimator, cancelTextAnimator, endTextAnimator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animator_events);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
startText = (TextView) findViewById(R.id.startText);
startText.setTextColor(OFF);
repeatText = (TextView) findViewById(R.id.repeatText);
repeatText.setTextColor(OFF);
cancelText = (TextView) findViewById(R.id.cancelText);
cancelText.setTextColor(OFF);
endText = (TextView) findViewById(R.id.endText);
endText.setTextColor(OFF);
startTextAnimator = (TextView) findViewById(R.id.startTextAnimator);
startTextAnimator.setTextColor(OFF);
repeatTextAnimator = (TextView) findViewById(R.id.repeatTextAnimator);
repeatTextAnimator.setTextColor(OFF);
cancelTextAnimator = (TextView) findViewById(R.id.cancelTextAnimator);
cancelTextAnimator.setTextColor(OFF);
endTextAnimator = (TextView) findViewById(R.id.endTextAnimator);
endTextAnimator.setTextColor(OFF);
final CheckBox endCB = (CheckBox) findViewById(R.id.endCB);
Button starter = (Button) findViewById(R.id.startButton);
starter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animView.startAnimation(endCB.isChecked());
}
});
Button canceler = (Button) findViewById(R.id.cancelButton);
canceler.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animView.cancelAnimation();
}
});
Button ender = (Button) findViewById(R.id.endButton);
ender.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animView.endAnimation();
}
});
}
public class MyAnimationView extends View implements Animator.AnimatorListener,
ValueAnimator.AnimatorUpdateListener {
public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
Animator animation;
ShapeHolder ball = null;
boolean endImmediately = false;
public MyAnimationView(Context context) {
super(context);
ball = createBall(25, 25);
}
private void createAnimation() {
if (animation == null) {
ObjectAnimator yAnim = ObjectAnimator.ofFloat(ball, "y",
ball.getY(), getHeight() - 50f).setDuration(1500);
yAnim.setRepeatCount(0);
yAnim.setRepeatMode(ValueAnimator.REVERSE);
yAnim.setInterpolator(new AccelerateInterpolator(2f));
yAnim.addUpdateListener(this);
yAnim.addListener(this);
ObjectAnimator xAnim = ObjectAnimator.ofFloat(ball, "x",
ball.getX(), ball.getX() + 300).setDuration(1000);
xAnim.setStartDelay(0);
xAnim.setRepeatCount(0);
xAnim.setRepeatMode(ValueAnimator.REVERSE);
xAnim.setInterpolator(new AccelerateInterpolator(2f));
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(ball, "alpha", 1f, .5f).
setDuration(1000);
AnimatorSet alphaSeq = new AnimatorSet();
alphaSeq.play(alphaAnim);
animation = new AnimatorSet();
((AnimatorSet) animation).playTogether(yAnim, xAnim , alphaSeq);
animation.addListener(this);
}
}
public void startAnimation(boolean endImmediately) {
this.endImmediately = endImmediately;
startText.setTextColor(OFF);
repeatText.setTextColor(OFF);
cancelText.setTextColor(OFF);
endText.setTextColor(OFF);
startTextAnimator.setTextColor(OFF);
repeatTextAnimator.setTextColor(OFF);
cancelTextAnimator.setTextColor(OFF);
endTextAnimator.setTextColor(OFF);
createAnimation();
animation.start();
}
public void cancelAnimation() {
createAnimation();
animation.cancel();
}
public void endAnimation() {
createAnimation();
animation.end();
}
private ShapeHolder createBall(float x, float y) {
OvalShape circle = new OvalShape();
circle.resize(50f, 50f);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x - 25f);
shapeHolder.setY(y - 25f);
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
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);
return shapeHolder;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(ball.getX(), ball.getY());
ball.getShape().draw(canvas);
canvas.restore();
}
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
/**
* 也可以监听到 animator的 开始 结束 取消 重复 等事件
*/
public void onAnimationStart(Animator animation) {
if (animation instanceof AnimatorSet) {
startText.setTextColor(ON);
} else {
startTextAnimator.setTextColor(ON);
}
if (endImmediately) {
animation.end();
}
}
public void onAnimationEnd(Animator animation) {
if (animation instanceof AnimatorSet) {
endText.setTextColor(ON);
} else {
endTextAnimator.setTextColor(ON);
}
}
public void onAnimationCancel(Animator animation) {
if (animation instanceof AnimatorSet) {
cancelText.setTextColor(ON);
} else {
cancelTextAnimator.setTextColor(ON);
}
}
public void onAnimationRepeat(Animator animation) {
if (animation instanceof AnimatorSet) {
repeatText.setTextColor(ON);
} else {
repeatTextAnimator.setTextColor(ON);
}
}
}
}