ValueAnimator的使用
activity_main.xml文件中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="www.weshared.anim.MainActivity">
<TextView
android:id="@+id/tv"
android:background="@android:color/holo_blue_bright"
android:padding="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/btn"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:background="@android:color/holo_red_dark"
android:text="执行动画"
android:layout_height="wrap_content" />
</RelativeLayout>
在res/animator目录下的value_animator.xml文件中(ValueAnimator的xml动画)
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:interpolator="@android:interpolator/bounce"
android:valueFrom="0px"
android:valueTo="720px"
android:valueType="intType" />
在MainActicity中
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ValueAnimator.AnimatorUpdateListener {
private TextView mTextView;
private Button mButton;
private int widthPixels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.tv);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(this);
//获取屏幕的宽度
widthPixels = getResources().getDisplayMetrics().widthPixels;
}
@Override
public void onClick(View v) {
//initAnim();
initOfObject();
//initXmlAnim();
}
private void initOfObject() {
ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('Z'));
animator.setDuration(2000).start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
char animatedValue = (char) animation.getAnimatedValue();
mTextView.setText(String.valueOf(animatedValue));
mTextView.requestLayout();
}
});
}
private void initAnim() {
ValueAnimator animator = ValueAnimator.ofInt(0, widthPixels);
animator.setDuration(2000);
animator.setInterpolator(new BounceInterpolator());
animator.start();
animator.addUpdateListener(this);
}
private void initXmlAnim() {
ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.value_animator);
animator.start();
animator.addUpdateListener(this);
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
mTextView.setWidth(animatedValue);
mTextView.setHeight((int) (animatedValue * 0.6));
mButton.setHeight((int) (animatedValue * 0.6));
mTextView.requestLayout();
mButton.requestLayout();
}
public class CharEvaluator implements TypeEvaluator<Character> {
@Override
public Character evaluate(float fraction, Character startValue, Character endValue) {
int startInt = (int) startValue;
int endInt = (int) endValue;
int curInt = (int) (startInt + fraction * (endInt - startInt));
char result = (char) curInt;
return result;
}
}
}