动画之Evaluator
Evaluator就是通过监听器拿到当前动画对对应的具体数值,作用在于从插值器返回的数值进行转换成对应的数值.简单来说就是转换器
Evaluator返回值的类型更加动画中值决定的,所以在使用的时候注意数据类型
tv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ValueAnimator animator = ValueAnimator.ofInt(0,300); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { int curValue = (Integer)animation.getAnimatedValue(); tv.layout(tv.getLeft(),curValue,tv.getRight(),curValue+tv.getHeight()); } }); animator.setDuration(1000); animator.setEvaluator(new ReverseEvaluator()); animator.start(); } });
import android.animation.TypeEvaluator; public class ReverseEvaluator implements TypeEvaluator<Integer> { public Integer evaluate(float fraction, Integer startValue, Integer endValue) { int startInt = startValue; return (int)(endValue - fraction * (endValue - startInt)); } }
tv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff); animator.setEvaluator(new ArgbEvaluator());//颜色值过滤转换 animator.setDuration(3000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { int curValue = (Integer) animation.getAnimatedValue(); tv.setBackgroundColor(curValue); } }); animator.start(); }
tv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('Z')); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { char text = (Character) animation.getAnimatedValue(); tv.setText(String.valueOf(text)); } }); animator.setDuration(10000); animator.setInterpolator(new AccelerateInterpolator()); animator.start(); } });
抛物动画
ballImg = (ImageView) findViewById(R.id.ball_img); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); ValueAnimator animator = ValueAnimator.ofObject(new FallingBallEvaluator(),new Point(0,0),new Point(width*2/3,height*2/3)); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { mCurPoint = (Point) animation.getAnimatedValue(); ballImg.layout(mCurPoint.x, mCurPoint.y, mCurPoint.x + ballImg.getWidth(), mCurPoint.y + ballImg.getHeight()); } }); animator.setDuration(2000); animator.start(); } });
public class FallingBallEvaluator implements TypeEvaluator<Point> { private Point point = new Point(); public Point evaluate(float fraction, Point startValue, Point endValue) { point.x = (int)(startValue.x + fraction *(endValue.x - startValue.x)); if (fraction*2<=1){ point.y = (int)(startValue.y + fraction*2*(endValue.y - startValue.y)); }else { point.y = endValue.y; } return point; } }
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#ff0000"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" tools:context="com.loaderman.customviewdemo.MainActivity"> <ImageView android:id="@+id/ball_img" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/cicle"/> <Button android:id="@+id/start_anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始动画"/> </LinearLayout>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!