فقط کافیست که کد های زیر را در فایل های مخصوص قرار دهید.
ابتدا چند تا عکس ستاره که یکی نصفه و یکی تمام خالی و دیگری تمام پر را دانلود و در Resource خود قرار دهید. سپس کد های attr را در پوشه value قرار دهید در فایل xml با نام attr قرار دهید. برای استفاده کردن باید در پروژه ی خود از لایبرری android-support v4 استفاده کنید. و در نهایت کلاس CustomRatingBar را کپی کنید. ستاره پر را در فیلد starOnResource مقدار دهی کنید. ستاره تمام خالی را در فیلد starOffResource مقدار دهی کنید. و در نهایت در فیلد starHalfResource ستاره نصفه را قرار دهید.
اسنیپ
CustomRatingBarattr+
public class CustomRatingBar extends LinearLayout {
public IRatingBarCallbacks getOnScoreChanged() {
return onScoreChanged;
}
public void setOnScoreChanged(IRatingBarCallbacks onScoreChanged) {
this.onScoreChanged = onScoreChanged;
}
public interface IRatingBarCallbacks {
void scoreChanged(float score);
}
private int maxStars = 5;
private float ratingValue = 5.0f;
private int starOnResource = R.drawable.star_on;
private int starOffResource = R.drawable.star_off;
private int starHalfResource = R.drawable.star_half;
private ImageView[] starsViews;
private int starPadding = -3;
private IRatingBarCallbacks onScoreChanged;
private int lastStarId;
private boolean onlyForPreview;
private double lastX;
private boolean halfStars = true;
public void setMaxStars(int value) {
maxStars = value;
init();
}
public void setRating(float value) {
ratingValue = value;
updateStars();
}
public void onlyForPreview(boolean value) {
onlyForPreview = value;
}
public void halfStars(boolean value) {
halfStars = value;
}
public CustomRatingBar(Context context) {
super(context);
}
public float getScore() {
return ratingValue;
}
public void setScore(float score) {
score = Math.round(score * 2) / 2.0f;
if ( !halfStars)
score = Math.round(score);
ratingValue = score;
updateStars();
}
public void setScrollToSelect(boolean enabled) {
onlyForPreview = !enabled;
}
public CustomRatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
initializeAttributes(attrs, context);
}
private void initializeAttributes(AttributeSet attrs, Context context) {
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.CustomRatingBar);
final int index = array.getIndexCount();
for (int i = 0; i < index; i++) {
int attr = array.getIndex(i);
if (attr == R.styleable.CustomRatingBar_maxStars)
maxStars = array.getInt(attr, 5);
else if (attr == R.styleable.CustomRatingBar_stars)
ratingValue = array.getFloat(attr, 2.5f);
else if (attr == R.styleable.CustomRatingBar_starHalf)
starHalfResource = array.getResourceId(attr, android.R.drawable.star_on);
else if (attr == R.styleable.CustomRatingBar_starOn)
starOnResource = array.getResourceId(attr, android.R.drawable.star_on);
else if (attr == R.styleable.CustomRatingBar_starOff)
starOffResource = array.getResourceId(attr, android.R.drawable.star_off);
else if (attr == R.styleable.CustomRatingBar_starPadding)
starPadding = (int) array.getDimension(attr, 0);
else if (attr == R.styleable.CustomRatingBar_onlyForDisplay)
onlyForPreview = array.getBoolean(attr, false);
else if (attr == R.styleable.CustomRatingBar_halfStars)
halfStars = array.getBoolean(attr, true);
}
array.recycle();
}
@TargetApi(Build.VERSION_CODES.BASE)
public CustomRatingBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
initializeAttributes(attrs, context);
}
private void init() {
starsViews = new ImageView[maxStars];
for (int i = 0; i < maxStars; i++) {
ImageView v = createStar();
addView(v);
starsViews[i] = v;
}
updateStars();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
/**
* hardcore math over here
*
* @param x
* @return
*/
private float getScoreForPosition(float x) {
if (halfStars)
return (float) Math.round(((x / ((float) getWidth() / (maxStars * 3f))) / 3f) * 2f) / 2;
float value = (float) Math.round((x / ((float) getWidth() / (maxStars))));
return value < 0 ? 1 : value;
}
private int getImageForScore(float score) {
if (score > 0)
return Math.round(score) - 1;
else
return -1;
}
private void updateStars() {
boolean flagHalf = (ratingValue != 0 && (ratingValue % 0.5 == 0)) && halfStars;
for (int i = 1; i <= maxStars; i++) {
if (i <= ratingValue)
starsViews[i - 1].setImageResource(starOnResource);
else {
if (flagHalf && i - 0.5 <= ratingValue)
starsViews[i - 1].setImageResource(starHalfResource);
else
starsViews[i - 1].setImageResource(starOffResource);
}
}
}
private ImageView createStar() {
ImageView img = new ImageView(getContext());
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.weight = 1;
img.setPadding(starPadding, 0, starPadding, 0);
img.setAdjustViewBounds(true);
img.setScaleType(ImageView.ScaleType.FIT_CENTER);
img.setLayoutParams(params);
img.setImageResource(starOffResource);
return img;
}
private ImageView getImageView(int position) {
try {
return starsViews[position];
}
catch (Exception e) {
return null;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (onlyForPreview) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
lastStarId = -1;
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(event.getX() - lastX) > 50)
requestDisallowInterceptTouchEvent(true);
float lastscore = ratingValue;
ratingValue = getScoreForPosition(event.getX());
if (lastscore != ratingValue) {
lastStarId = getImageForScore(ratingValue);
updateStars();
if (onScoreChanged != null)
onScoreChanged.scoreChanged(ratingValue);
}
break;
case MotionEvent.ACTION_DOWN:
lastX = event.getX();
lastscore = ratingValue;
ratingValue = getScoreForPosition(event.getX());
lastStarId = getImageForScore(ratingValue);
if (lastscore != ratingValue) {
updateStars();
if (onScoreChanged != null)
onScoreChanged.scoreChanged(ratingValue);
}
}
return true;
}
public boolean isHalfStars() {
return halfStars;
}
}<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomRatingBar">
<attr name="maxStars" format="integer"/>
<attr name="stars" format="float"/>
<attr name="starOff" format="reference|integer"/>
<attr name="starOn" format="reference|integer"/>
<attr name="starHalf" format="reference|integer"/>
<attr name="starPadding" format="dimension"/>
<attr name="onlyForDisplay" format="boolean"/>
<attr name="halfStars" format="boolean"/>
</declare-styleable>
</resources>