-
Notifications
You must be signed in to change notification settings - Fork 151
/
RotationRatingBar.java
88 lines (73 loc) · 2.78 KB
/
RotationRatingBar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.willy.ratingbar;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
/**
* Created by nappannda on 2017/05/16.
*/
public class RotationRatingBar extends AnimationRatingBar {
// Control animation speed
private static final long ANIMATION_DELAY = 15;
public RotationRatingBar(Context context) {
super(context);
}
public RotationRatingBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public RotationRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void emptyRatingBar() {
// Need to remove all previous runnable to prevent emptyRatingBar and fillRatingBar out of sync
if (mRunnable != null) {
mHandler.removeCallbacksAndMessages(mRunnableToken);
}
long delay = 0;
for (final PartialView partialView : mPartialViews) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
partialView.setEmpty();
}
}, delay += 5);
}
}
@Override
protected void fillRatingBar(final float rating) {
// Need to remove all previous runnable to prevent emptyRatingBar and fillRatingBar out of sync
if (mRunnable != null) {
mHandler.removeCallbacksAndMessages(mRunnableToken);
}
for (final PartialView partialView : mPartialViews) {
final int ratingViewId = (int) partialView.getTag();
final double maxIntOfRating = Math.ceil(rating);
if (ratingViewId > maxIntOfRating) {
partialView.setEmpty();
continue;
}
mRunnable = getAnimationRunnable(rating, partialView, ratingViewId, maxIntOfRating);
postRunnable(mRunnable, ANIMATION_DELAY);
}
}
@NonNull
private Runnable getAnimationRunnable(final float rating, final PartialView partialView, final int ratingViewId, final double maxIntOfRating) {
return new Runnable() {
@Override
public void run() {
if (ratingViewId == maxIntOfRating) {
partialView.setPartialFilled(rating);
} else {
partialView.setFilled();
}
if (ratingViewId == rating) {
Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotation);
partialView.startAnimation(rotation);
}
}
};
}
}