-
Notifications
You must be signed in to change notification settings - Fork 151
/
RatingBarUtils.java
60 lines (48 loc) · 1.96 KB
/
RatingBarUtils.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
package com.willy.ratingbar;
import android.view.MotionEvent;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* Created by willy on 2018/3/8.
*/
class RatingBarUtils {
private static DecimalFormat mDecimalFormat;
private static final int MAX_CLICK_DISTANCE = 5;
private static final int MAX_CLICK_DURATION = 200;
static boolean isClickEvent(float startX, float startY, MotionEvent event) {
float duration = event.getEventTime() - event.getDownTime();
if (duration > MAX_CLICK_DURATION) {
return false;
}
float differenceX = Math.abs(startX - event.getX());
float differenceY = Math.abs(startY - event.getY());
return !(differenceX > MAX_CLICK_DISTANCE || differenceY > MAX_CLICK_DISTANCE);
}
static float calculateRating(PartialView partialView, float stepSize, float eventX) {
DecimalFormat decimalFormat = RatingBarUtils.getDecimalFormat();
float ratioOfView = Float.parseFloat(decimalFormat.format((eventX - partialView.getLeft()) / partialView.getWidth()));
float steps = Math.round(ratioOfView / stepSize) * stepSize;
return Float.parseFloat(decimalFormat.format((int) partialView.getTag() - (1 - steps)));
}
static float getValidMinimumStars(float minimumStars, int numStars, float stepSize) {
if (minimumStars < 0) {
minimumStars = 0;
}
if (minimumStars > numStars) {
minimumStars = numStars;
}
if (minimumStars % stepSize != 0) {
minimumStars = stepSize;
}
return minimumStars;
}
static DecimalFormat getDecimalFormat() {
if (mDecimalFormat == null) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
symbols.setDecimalSeparator('.');
mDecimalFormat = new DecimalFormat("#.##", symbols);
}
return mDecimalFormat;
}
}