RatingBarView.java
4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.xgimi.gimicinema.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.xgimi.gimicinema.R;
public class RatingBarView extends LinearLayout {
public interface OnRatingListener {
void onRating(Object bindObject, int RatingScore);
}
private boolean mClickable = true;
private OnRatingListener onRatingListener;
private Object bindObject;
private float starImageSize;
private int starCount;
private Drawable starEmptyDrawable;
private Drawable starFillDrawable;
private int mStarCount;
public void setClickable(boolean clickable) {
this.mClickable = clickable;
}
public RatingBarView(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.HORIZONTAL);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RatingBarView);
starImageSize = ta.getDimension(R.styleable.RatingBarView_starImageSize, 20);
starCount = ta.getInteger(R.styleable.RatingBarView_starCount, 5);
starEmptyDrawable = ta.getDrawable(R.styleable.RatingBarView_starEmpty);
starFillDrawable = ta.getDrawable(R.styleable.RatingBarView_starFill);
ta.recycle();
for (int i = 0; i < starCount; ++i) {
ImageView imageView = getStarImageView(context, attrs);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mClickable) {
mStarCount = indexOfChild(v) + 1;
setStar(mStarCount, true);
if (onRatingListener != null) {
onRatingListener.onRating(bindObject, mStarCount);
}
}
}
});
addView(imageView);
}
}
private ImageView getStarImageView(Context context, AttributeSet attrs) {
ImageView imageView = new ImageView(context);
ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(Math.round(starImageSize), Math.round(starImageSize));
imageView.setLayoutParams(para);
// you can change gap between two stars use the padding
imageView.setPadding(0, 0, 40, 0);
imageView.setImageDrawable(starEmptyDrawable);
imageView.setMaxWidth(10);
imageView.setMaxHeight(10);
return imageView;
}
public void setStar(int starCount, boolean animation) {
starCount = starCount > this.starCount ? this.starCount : starCount;
starCount = starCount < 0 ? 0 : starCount;
for (int i = 0; i < starCount; ++i) {
((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
if (animation) {
ScaleAnimation sa = new ScaleAnimation(0, 0, 1, 1);
getChildAt(i).startAnimation(sa);
}
}
for (int i = this.starCount - 1; i >= starCount; --i) {
((ImageView) getChildAt(i)).setImageDrawable(starEmptyDrawable);
}
}
public int getStarCount() {
return mStarCount;
}
public void setStarFillDrawable(Drawable starFillDrawable) {
this.starFillDrawable = starFillDrawable;
}
public void setStarEmptyDrawable(Drawable starEmptyDrawable) {
this.starEmptyDrawable = starEmptyDrawable;
}
public void setStarCount(int startCount) {
this.starCount = starCount;
}
public void setStarImageSize(float starImageSize) {
this.starImageSize = starImageSize;
}
public void setBindObject(Object bindObject) {
this.bindObject = bindObject;
}
public void setOnRatingListener(OnRatingListener onRatingListener) {
this.onRatingListener = onRatingListener;
}
}