package com. saruan. screenevaluation. Util;
import android. content. Context;
import android. content. res. TypedArray;
import android. graphics. drawable. Drawable;
import android. util. AttributeSet;
import android. view. View;
import android. widget. ImageView;
import android. widget. LinearLayout;
import com. saruan. screenevaluation. R;
import java. math. BigDecimal;
public class RatingBars extends LinearLayout {
private boolean mClickable;
private int starCount;
private OnRatingChangeListener onRatingChangeListener;
private float starImageSize;
private float starPadding;
private float starStep;
private Drawable starEmptyDrawable;
private Drawable starFillDrawable;
private Drawable starHalfDrawable;
private StepSize stepSize;
public void setStarHalfDrawable ( Drawable starHalfDrawable) {
this . starHalfDrawable = starHalfDrawable;
}
public void setStarFillDrawable ( Drawable starFillDrawable) {
this . starFillDrawable = starFillDrawable;
}
public void setStarEmptyDrawable ( Drawable starEmptyDrawable) {
this . starEmptyDrawable = starEmptyDrawable;
}
public void setClickable ( boolean clickable) {
this . mClickable = clickable;
}
public void setOnRatingChangeListener ( OnRatingChangeListener onRatingChangeListener) {
this . onRatingChangeListener = onRatingChangeListener;
}
public void setStarImageSize ( float starImageSize) {
this . starImageSize = starImageSize;
}
public void setStepSize ( StepSize stepSize) {
this . stepSize = stepSize;
}
public RatingBars ( Context context, AttributeSet attrs) {
super ( context, attrs) ;
setOrientation ( LinearLayout. HORIZONTAL) ;
TypedArray mTypedArray = context. obtainStyledAttributes ( attrs, R. styleable. RatingBar) ;
starImageSize = mTypedArray. getDimension ( R. styleable. RatingBar_starImageSize, 20 ) ;
starPadding = mTypedArray. getDimension ( R. styleable. RatingBar_starPadding, 10 ) ;
starStep = mTypedArray. getFloat ( R. styleable. RatingBar_starStep, 1.0f ) ;
stepSize = StepSize. fromStep ( mTypedArray. getInt ( R. styleable. RatingBar_stepSize, 1 ) ) ;
starCount = mTypedArray. getInteger ( R. styleable. RatingBar_starCount, 5 ) ;
starEmptyDrawable = mTypedArray. getDrawable ( R. styleable. RatingBar_starEmpty) ;
starFillDrawable = mTypedArray. getDrawable ( R. styleable. RatingBar_starFill) ;
starHalfDrawable = mTypedArray. getDrawable ( R. styleable. RatingBar_starHalf) ;
mClickable = mTypedArray. getBoolean ( R. styleable. RatingBar_clickable, true ) ;
mTypedArray. recycle ( ) ;
for ( int i = 0 ; i < starCount; ++ i) {
final ImageView imageView = getStarImageView ( ) ;
imageView. setImageDrawable ( starEmptyDrawable) ;
imageView. setOnClickListener (
new OnClickListener ( ) {
@Override
public void onClick ( View v) {
if ( mClickable) {
int fint = ( int ) starStep;
BigDecimal b1 = new BigDecimal ( Float. toString ( starStep) ) ;
BigDecimal b2 = new BigDecimal ( Integer. toString ( fint) ) ;
float fPoint = b1. subtract ( b2) . floatValue ( ) ;
if ( fPoint == 0 ) {
fint -= 1 ;
}
if ( indexOfChild ( v) > fint) {
setStar ( indexOfChild ( v) + 1 ) ;
} else if ( indexOfChild ( v) == fint) {
if ( stepSize == StepSize. Full) {
return ;
}
if ( imageView. getDrawable ( ) . getCurrent ( ) . getConstantState ( ) . equals ( starHalfDrawable. getConstantState ( ) ) ) {
setStar ( indexOfChild ( v) + 1 ) ;
} else {
setStar ( indexOfChild ( v) + 0.5f ) ;
}
} else {
setStar ( indexOfChild ( v) + 1f ) ;
}
}
}
}
) ;
addView ( imageView) ;
}
setStar ( starStep) ;
}
private ImageView getStarImageView ( ) {
ImageView imageView = new ImageView ( getContext ( ) ) ;
LinearLayout. LayoutParams layout = new LinearLayout. LayoutParams (
Math. round ( starImageSize) , Math. round ( starImageSize) ) ;
layout. setMargins ( 0 , 0 , Math. round ( starPadding) , 0 ) ;
imageView. setLayoutParams ( layout) ;
imageView. setAdjustViewBounds ( true ) ;
imageView. setScaleType ( ImageView. ScaleType. CENTER_CROP) ;
imageView. setImageDrawable ( starEmptyDrawable) ;
imageView. setMinimumWidth ( 10 ) ;
imageView. setMaxHeight ( 10 ) ;
return imageView;
}
public void setStar ( float rating) {
if ( onRatingChangeListener != null) {
onRatingChangeListener. onRatingChange ( rating) ;
}
this . starStep = rating;
int fint = ( int ) rating;
BigDecimal b1 = new BigDecimal ( Float. toString ( rating) ) ;
BigDecimal b2 = new BigDecimal ( Integer. toString ( fint) ) ;
float fPoint = b1. subtract ( b2) . floatValue ( ) ;
for ( int i = 0 ; i < fint; ++ i) {
( ( ImageView) getChildAt ( i) ) . setImageDrawable ( starFillDrawable) ;
}
for ( int i = fint; i < starCount; i++ ) {
( ( ImageView) getChildAt ( i) ) . setImageDrawable ( starEmptyDrawable) ;
}
if ( fPoint > 0 ) {
( ( ImageView) getChildAt ( fint) ) . setImageDrawable ( starHalfDrawable) ;
}
}
public interface OnRatingChangeListener {
void onRatingChange ( float ratingCount) ;
}
public enum StepSize {
Half ( 0 ) , Full ( 1 ) ;
int step;
StepSize ( int step) {
this . step = step;
}
public static StepSize fromStep ( int step) {
for ( StepSize f : values ( ) ) {
if ( f. step == step) {
return f;
}
}
throw new IllegalArgumentException ( ) ;
}
}
}
```java
< declare- styleable name= "RatingBar" >
< ! -- 尺寸值-- >
< attr name= "starImageSize" format= "dimension" / >
< ! -- 星星间距-- >
< attr name= "starPadding" format= "dimension" / >
< ! -- 星星总数-- >
< attr name= "starCount" format= "integer" / >
< ! -- 空白的星星资源文件值-- >
< attr name= "starEmpty" format= "reference" / >
< ! -- 满星资源文件值-- >
< attr name= "starFill" format= "reference" / >
< ! -- 半星资源文件值-- >
< attr name= "starHalf" format= "reference" / >
< ! -- 是否可点击boolean 值-- >
< attr name= "clickable" format= "boolean" / >
< ! -- 当前进度float 值-- >
< attr name= "starStep" format= "float" / >
< ! -- 每次进度方式的值,整星还是半星-- >
< attr name= "stepSize" >
< enum name= "Half" value= "0" / >
< enum name= "Full" value= "1" / >
< / attr>
< / declare- styleable>
ratingBars. setOnRatingChangeListener ( new RatingBars. OnRatingChangeListener ( ) {
@Override
public void onRatingChange ( float ratingCount) {
score ( ( int ) ratingCount) ;
}
} ) ;