动画评级栏明星

时间:2018-04-16 16:50:59

标签: java android

我在互联网上搜索答案但失败了。我得到的最接近的是:

int sc = total % 10;
ObjectAnimator anim = ObjectAnimator.ofFloat(stars, "rating", stars.getRating(), 5f);
anim.setDuration(2000);
                switch (sc){
                    case 5:
                        stars.setRating(1);break;
                    case 6:
                        stars.setRating(1);break;
                    case 7:
                        stars.setRating(2);break;
                    case 8:
                        stars.setRating(2);break;
                    case 9:
                        stars.setRating(3);break;
                    case 0:
                        stars.setRating(3);break;
                }

                anim.start();

但它不会停留在1星或2星。它总是将ratingbar完全填满3颗星。

1 个答案:

答案 0 :(得分:1)

代码

ObjectAnimator.ofFloat(stars, "Rating", stars.getRating(), 5f);

你告诉objectAnimator动画你的ratingBar评级从starts.getRating()到5星评级你应该改变你的代码如下

  ObjectAnimator.ofFloat(stars, "Rating",0, yourRate);

示例:

   myRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean fromUser) {

            if(fromUser)
            setRatingWithAnimation(v);

        }
    });


public void setRatingWithAnimation(float ratingWithAnimation) {

    ObjectAnimator
            .ofFloat(myRatingBar,"Rating",0,ratingWithAnimation)
            .setDuration(1000)
            .start();

}