1 官方文档
This class provides a simple timing engine(计时器) for running animations which calculate animated values and set them on target objects.
There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.
By default, ValueAnimator uses non-linear time interpolation, via the AccelerateDecelerateInterpolator class, which accelerates into and decelerates out of an animation. This behavior can be changed by calling setInterpolator(TimeInterpolator).
(这段话是说动画默认是非匀速的,可以加速播放动画也可以减速播放,需要使用setInterpolator方法)
2 使用方法
final ValueAnimator animator = ValueAnimator.ofInt(0, 50);
animator.setDuration(2000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//动画播放结束执行的操作
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
int top = value * 10;
int bottom = top + textView.getHeight();
//不停重新布局视图位置
setLayout(getRecyclerViewRect(top,bottom));
}
});
animator.start();
3 效果展示
4 完整代码
package com.demo1;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_click;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_click = (Button) findViewById(R.id.tv);
btn_click.setOnClickListener(this);
textView = (TextView) findViewById(R.id.tvext);
textView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv:
startAnimator();
break;
}
}
private void startAnimator() {
final ValueAnimator animator = ValueAnimator.ofInt(0, 50);
animator.setDuration(2000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
int top = value * 10;
int bottom = top + textView.getHeight();
//重新布局视图位置
setLayout(getRecyclerViewRect(top,bottom));
}
});
animator.start();
}
public void setLayout (Rect rect) {
textView.layout(rect.left, rect.top, rect.right, rect.bottom);
textView.invalidate();
}
public Rect getRecyclerViewRect (int top, int bottom) {
return new Rect(textView.getLeft(), top, textView.getRight(), bottom);
}
}