Android属性动画基础:简单实践之两个小球同时落地

这是一篇纯粹的实践文章,没有解析,唯一需要提醒的是,要保证两个小球在垂直方向保持一致的运动状态。直接上示例图片(屏幕录制效果不好)及代码,代码中展示了Interpolator、TypeEvaluator和PropertyValuesHolder的简单实用方式

/**
 * 通过两个小球同时落地,演示属性动画基本使用
 * Author: cuiyan
 * Date:   18/5/2 22:39
 * Desc:
 */
public class BallsFallDownSimultaneously extends BaseStateViewActivity implements View.OnClickListener {

    private View freeFallView;
    private View horizontalProjectileMotionView;
    // 模拟自由落体
    private ObjectAnimator freeFallAnimator;
    // 模拟平抛1
    private ValueAnimator horizontalProjectileAnimator1;
    // 模拟平抛2
    private ObjectAnimator horizontalProjectileAnimator2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public View createContentView(LayoutInflater inflater, ViewGroup contentRoot) {
        return inflater.inflate(R.layout.activity_balls_fall_down, contentRoot, false);
    }

    private void init() {
        setText(R.id.tv_title, "两个小球同时落地");
        setOnClickListener(R.id.iv_left, this);
        setOnClickListener(R.id.tv_reset, this);
        setOnClickListener(R.id.tv_start, this);
        freeFallView = findViewById(R.id.view_free_fall);
        horizontalProjectileMotionView = findViewById(R.id.view_horizontal_projectile_motion);
        setContentState(STATE_DATA_CONTENT);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            initAnimation();
        }
    }

    private void initAnimation() {
        final int verticalDistance = DisplayUtil.getScreenHeight() - freeFallView.getBottom() - DisplayUtil.getStatusBarHeight(this) - DisplayUtil.dp2px(44);
        int horizontalDistance = freeFallView.getLeft() - horizontalProjectileMotionView.getRight();
        // 自由落体,动画系统利用反射直接修改view平移属性
        freeFallAnimator = ObjectAnimator.ofFloat(freeFallView, "translationY", 0, verticalDistance);
        // 自由落体动画设置方式1
        freeFallAnimator.setInterpolator(new LinearInterpolator());
        freeFallAnimator.setEvaluator(new TypeEvaluator<Float>() {
            @Override
            public Float evaluate(float fraction, Float startValue, Float endValue) {
                return fraction * fraction * (endValue - startValue) + startValue;
            }
        });
        // 自由落体动画设置方式2
//        freeFallAnimator.setInterpolator(new AccelerateInterpolator());
        // 自由落体动画设置方式3
//        freeFallAnimator.setInterpolator(new TimeInterpolator() {
//            @Override
//            public float getInterpolation(float input) {
//                // 效果等同于freeFallAnimator.setInterpolator(new AccelerateInterpolator());
//                return input * input;
//            }
//        });

        // 平抛1,需手动修改平移属性
        horizontalProjectileAnimator1 = ValueAnimator.ofObject(new MyTypeEvaluator(), new Point(0, 0), new Point(horizontalDistance, verticalDistance));
        horizontalProjectileAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point point = (Point) animation.getAnimatedValue();
                horizontalProjectileMotionView.setTranslationX(point.getPointX());
                horizontalProjectileMotionView.setTranslationY(point.getPointY());
            }
        });
        horizontalProjectileAnimator1.setInterpolator(new LinearInterpolator());

        // 平抛2,动画系统通过反射直接修改view平移属性
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("translationX", 0, horizontalDistance);
        pvhX.setEvaluator(new TypeEvaluator<Float>() {
            @Override
            public Float evaluate(float fraction, Float startValue, Float endValue) {
                return fraction * (endValue - startValue) + startValue;
            }
        });
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("translationY", 0, verticalDistance);
        pvhY.setEvaluator(new TypeEvaluator<Float>() {
            @Override
            public Float evaluate(float fraction, Float startValue, Float endValue) {
                return fraction * fraction * (endValue - startValue) + startValue;
            }
        });
        horizontalProjectileAnimator2 = ObjectAnimator.ofPropertyValuesHolder(horizontalProjectileMotionView, pvhX, pvhY);
        horizontalProjectileAnimator2.setInterpolator(new LinearInterpolator());
    }

    private void runAnimation() {
        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.play(freeFallAnimator).with(horizontalProjectileAnimator);
//        animatorSet.playTogether(freeFallAnimator, horizontalProjectileAnimator1);
        animatorSet.playTogether(freeFallAnimator, horizontalProjectileAnimator2);

        animatorSet.setDuration(1500);
        animatorSet.start();
    }

    private void reset() {
        freeFallView.setTranslationY(0);
        horizontalProjectileMotionView.setTranslationX(0);
        horizontalProjectileMotionView.setTranslationY(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left: {
                finish();
                break;
            }
            case R.id.tv_reset: {
                reset();
                break;
            }
            case R.id.tv_start: {
                runAnimation();
                break;
            }
        }
    }

    // 描述小球运动轨迹坐标
    private class Point {
        private float pointX;
        private float pointY;

        private Point(float pointX, float pointY) {
            this.pointX = pointX;
            this.pointY = pointY;
        }

        private float getPointX() {
            return pointX;
        }

        private void setPointX(float pointX) {
            this.pointX = pointX;
        }

        private float getPointY() {
            return pointY;
        }

        private void setPointY(float pointY) {
            this.pointY = pointY;
        }
    }

    // 模拟平抛运动轨迹的估值器
    private class MyTypeEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            float pointX = fraction * (endValue.getPointX() - startValue.getPointY()) + startValue.getPointX();
            float pointY = fraction * fraction * (endValue.getPointY() - startValue.getPointY()) + startValue.getPointY();
            return new Point(pointX, pointY);
        }
    }
}

完整代码地址:https://github.com/670832188/TestApp/blob/master/app/src/main/java/com/dev/kit/testapp/animation/BallsFallDownSimultaneously.java

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,025评论 3 119
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,268评论 1 38
  • 守着孤独,不知道你有没有尝试过?就是在人群里,一个星期没有说一句话。一个人的思绪纷飞如烟云,在往事与书字中扑捉活着...
    九儿杂文阅读 578评论 1 4
  • 慢慢的发现过去的很多动态都只是一纸空话 或是单枪直入的意有所指 或是有待推敲的欲盖弥彰 偶尔矫情到自己都觉得可怕 ...
    婠执阅读 671评论 0 3
  • 夏季的天空 月光洒落一抹余晖 微微拂过脸颊的微风 仰望着群星璀璨的星空 让我不禁思念起远方的你 慵懒的流星划过斑驳...
    小安的自娱自乐阅读 256评论 0 0