Android Value Animator很慢

时间:2017-12-04 09:45:00

标签: android television animator

我正在为Android TV制作铁路物品动画。我使用Value Animator组合了几个动画,但是动画非常慢,并且跳过很多帧。事实证明,即使我使用Value Animator只播放一个动画,它也会导致这种行为。我需要一起使用五个动画。这是我的代码:

public void setFocus(boolean hasFocus) {

    //If card has focus
    if (hasFocus) {

        animWidth = ValueAnimator.ofInt(rlMain.getMeasuredWidth(), (int) 
        Utils.convertDpToPixel(215));
        animWidth.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = 
        rlMain.getLayoutParams();
                layoutParams.width = val;
                rlMain.setLayoutParams(layoutParams);
            }
        });

        animHeight = ValueAnimator.ofInt(rlMain.getMeasuredHeight(), (int) 
        Utils.convertDpToPixel(328));
        animHeight.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = 
        rlMain.getLayoutParams();
                layoutParams.height = val;
                rlMain.setLayoutParams(layoutParams);
            }
        });

        animTopMargin = ValueAnimator.ofInt((int) Utils.convertDpToPixel(42), 
        (int) Utils.convertDpToPixel(12));
        animTopMargin.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) 
        rlMain.getLayoutParams();
                lp.setMargins(0, (Integer) animation.getAnimatedValue(), 
        (int) Utils.convertDpToPixel(12), 0);
                rlMain.setLayoutParams(lp);
            }
        });

        animTranslationX = ValueAnimator.ofFloat(rlContent.getX(), 
        rlContent.getX() + 15);
        animTranslationX.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float xAnimatedValue = (float) animation.getAnimatedValue();
                rlContent.setX(xAnimatedValue);
            }
        });

        animTranslationY = ValueAnimator.ofFloat(rlContent.getY(), 
        rlContent.getY() - 17);
        animTranslationY.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float yAnimatedValue = (float) animation.getAnimatedValue();
                rlContent.setY(yAnimatedValue);
            }
        });

        animatorSet = new AnimatorSet();
        animatorSet.playTogether(animWidth, animHeight, animTopMargin, 
        animTranslationX, animTranslationY);
        animatorSet.setDuration(1000);
        animatorSet.start();

        //Focus shadow visible
        rlFocus.setVisibility(View.VISIBLE);

        animWidth = null;
        animHeight = null;
        animTopMargin = null;
        animTranslationX = null;
        animTranslationY = null;
        animatorSet = null;

        } else {

        animWidth = ValueAnimator.ofInt(rlMain.getMeasuredWidth(), (int) 
        Utils.convertDpToPixel(185));
        animWidth.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = 
        rlMain.getLayoutParams();
                layoutParams.width = val;
                rlMain.setLayoutParams(layoutParams);
            }
        });

        animHeight = ValueAnimator.ofInt(rlMain.getMeasuredHeight(), (int) 
        Utils.convertDpToPixel(278));
        animHeight.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = 
        rlMain.getLayoutParams();
                layoutParams.height = val;
                rlMain.setLayoutParams(layoutParams);
            }
        });

        animTopMargin = ValueAnimator.ofInt((int) Utils.convertDpToPixel(12), 
        (int) Utils.convertDpToPixel(42));
        animTopMargin.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) 
        rlMain.getLayoutParams();
                lp.setMargins(0, (Integer) animation.getAnimatedValue(), 
        (int) Utils.convertDpToPixel(12), 0);
                rlMain.setLayoutParams(lp);
            }
        });

        animTranslationX = ValueAnimator.ofFloat(rlContent.getX(), 
        rlContent.getX() - 15);
        animTranslationX.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float xAnimatedValue = (float) animation.getAnimatedValue();
                rlContent.setX(xAnimatedValue);
            }
        });

        animTranslationY = ValueAnimator.ofFloat(rlContent.getY(), 
        rlContent.getY() + 17);
        animTranslationY.addUpdateListener(new 
        ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float yAnimatedValue = (float) animation.getAnimatedValue();
                rlContent.setY(yAnimatedValue);
            }
        });


        animatorSet = new AnimatorSet();
        animatorSet.playTogether(animWidth, animHeight, animTopMargin, 
        animTranslationX, animTranslationY);
        animatorSet.setDuration(1000);
        animatorSet.start();

        //Set focus shadow invisible with delay
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rlFocus.setVisibility(View.INVISIBLE);
            }
        }, 1010);

        animWidth = null;
        animHeight = null;
        animTopMargin = null;
        animTranslationX = null;
        animTranslationY = null;
        animatorSet = null;
    }
}

有没有办法让这些动画流畅?任何帮助将受到高度赞赏:)

0 个答案:

没有答案