Android屬性動畫基礎(chǔ):簡單實踐之兩個小球同時落地

這是一篇純粹的實踐文章,沒有解析,唯一需要提醒的是,要保證兩個小球在垂直方向保持一致的運動狀態(tài)。直接上示例圖片(屏幕錄制效果不好)及代碼,代碼中展示了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();
        // 自由落體,動畫系統(tǒng)利用反射直接修改view平移屬性
        freeFallAnimator = ObjectAnimator.ofFloat(freeFallView, "translationY", 0, verticalDistance);
        // 自由落體動畫設(shè)置方式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;
            }
        });
        // 自由落體動畫設(shè)置方式2
//        freeFallAnimator.setInterpolator(new AccelerateInterpolator());
        // 自由落體動畫設(shè)置方式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,動畫系統(tǒng)通過反射直接修改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;
            }
        }
    }

    // 描述小球運動軌跡坐標(biāo)
    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,186評論 3 119
  • 【Android 動畫】 動畫分類補間動畫(Tween動畫)幀動畫(Frame 動畫)屬性動畫(Property ...
    Rtia閱讀 6,380評論 1 38
  • 守著孤獨,不知道你有沒有嘗試過?就是在人群里,一個星期沒有說一句話。一個人的思緒紛飛如煙云,在往事與書字中撲捉活著...
    九兒雜文閱讀 607評論 1 4
  • 慢慢的發(fā)現(xiàn)過去的很多動態(tài)都只是一紙空話 或是單槍直入的意有所指 或是有待推敲的欲蓋彌彰 偶爾矯情到自己都覺得可怕 ...
    婠執(zhí)閱讀 749評論 0 3
  • 夏季的天空 月光灑落一抹余暉 微微拂過臉頰的微風(fēng) 仰望著群星璀璨的星空 讓我不禁思念起遠(yuǎn)方的你 慵懶的流星劃過斑駁...
    小安的自娛自樂閱讀 271評論 0 0

友情鏈接更多精彩內(nèi)容