这是一个普通的移动动画
1. private void startValueAnimation(){
2. if(mValueAnimator == null){
3. mValueAnimator = ValueAnimator.ofFloat(0, 500);
4. }
5. mValueAnimator.setInterpolator(new AnticipateInterpolator());
6. mValueAnimator.setTarget(mImageView);
7. mValueAnimator.setDuration(3000);
8. mValueAnimator.setRepeatCount(1);
9. mValueAnimator.start();
10. mValueAnimator.addUpdateListener(new AnimatorUpdateListener() {
11. @Override
12. public void onAnimationUpdate(ValueAnimator animation) {
13. //同时设置X,Y 两个属性
14. mImageView.setTranslationX((Float) animation.getAnimatedValue());
15. mImageView.setTranslationY((Float) animation.getAnimatedValue());
16. }
17. });
18. }
自定义一个TypeEvaluator
首先定义两个类
1. public class XYHolder{
2. private float mX;
3. private float mY;
4.
5. public XYHolder(float x, float y) {
6. mX = x;
7. mY = y;
8. }
9.
10. public float getX() {
11. return mX;
12. }
13.
14. public void setX(float x) {
15. mX = x;
16. }
17.
18. public float getY() {
19. return mY;
20. }
21.
22. public void setY(float y) {
23. mY = y;
24. }
25. }
执行过程:
1. private void startObjectAnimation() {
2. ViewXYHolder viewXYHolder = new ViewXYHolder(mImageView);
3. XYHolder startXY = new XYHolder(0f, 0f);
4. XYHolder endXY = new XYHolder(500f, 500f);
5. ObjectAnimator objectAnimator = ObjectAnimator.ofObject(viewXYHolder, "xY", new XYmEvaluator(), startXY, endXY);
6. objectAnimator.setInterpolator(new LinearInterpolator());
7. objectAnimator.setDuration(3000);
8. objectAnimator.start();
9. }
10.
11. public class XYmEvaluator implements TypeEvaluator {
12. public Object evaluate(float fraction, Object startValue, Object endValue) {
13. XYHolder startXY = (XYHolder) startValue;
14. XYHolder endXY = (XYHolder) endValue;
15. return new XYHolder(startXY.getX() + fraction * (endXY.getX() - startXY.getX()),
16. startXY.getY() + fraction * (endXY.getY() - startXY.getY()));
17. }
18. }
共同学习,写下你的评论
评论加载中...
作者其他优质文章