轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/linglongxin24/article/details/52918701 【DylanAndroid的csdn博客】
在Android開(kāi)發(fā)中渠啊,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià)拣宏,運(yùn)用星星進(jìn)行打分零聚。然而在Android系統(tǒng)中自帶的打分控件谆吴,RatingBar特別不好用站削,間距和大小無(wú)法改變坊萝。所以,我就自定義了一個(gè)特別好用的打分控件。在項(xiàng)目中可以直接使用十偶,特別簡(jiǎn)單菩鲜。下面直接上圖:
效果圖
這里寫(xiě)圖片描述
實(shí)現(xiàn)原理
其實(shí)就是自定義View繼承LinearLayout ,然后里面動(dòng)態(tài)加了五個(gè)ImageView惦积。
實(shí)現(xiàn)代碼接校,有詳細(xì)的注釋
- 在attrs中聲明的可以在xml中設(shè)置的變量
<declare-styleable name="RatingBar">
<!--尺寸值-->
<attr name="starImageSize" format="dimension" />
<!--星星間距-->
<attr name="starPadding" format="dimension" />
<!--星星總數(shù)-->
<attr name="starCount" format="integer" />
<!--空白的星星資源文件值-->
<attr name="starEmpty" format="reference" />
<!--滿星資源文件值-->
<attr name="starFill" format="reference" />
<!--半星資源文件值-->
<attr name="starHalf" format="reference" />
<!--是否可點(diǎn)擊boolean值-->
<attr name="clickable" format="boolean" />
<!--當(dāng)前進(jìn)度f(wàn)loat值-->
<attr name="starStep" format="float" />
<!--每次進(jìn)度方式的值,整星還是半星-->
<attr name="stepSize">
<enum name="Half" value="0" />
<enum name="Full" value="1" />
</attr>
</declare-styleable>
- RatingBar源碼
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.kejiang.yuandl.R;
import java.math.BigDecimal;
/**
* Created by dylan on 2015/6/11.
* 自定義打分控件RatingBar
* 可以自定義星星大小和間距
* Correction clickEvent from Xml
*/
public class RatingBar extends LinearLayout {
/**
* 是否可點(diǎn)擊
*/
private boolean mClickable;
/**
* 星星總數(shù)
*/
private int starCount;
/**
* 星星的點(diǎn)擊事件
*/
private OnRatingChangeListener onRatingChangeListener;
/**
* 每個(gè)星星的大小
*/
private float starImageSize;
/**
* 每個(gè)星星的間距
*/
private float starPadding;
/**
* 星星的顯示數(shù)量狮崩,支持小數(shù)點(diǎn)
*/
private float starStep;
/**
* 空白的默認(rèn)星星圖片
*/
private Drawable starEmptyDrawable;
/**
* 選中后的星星填充圖片
*/
private Drawable starFillDrawable;
/**
* 半顆星的圖片
*/
private Drawable starHalfDrawable;
/**
* 每次點(diǎn)擊星星所增加的量是整個(gè)還是半個(gè)
*/
private StepSize stepSize;
/**
* 設(shè)置半星的圖片資源文件
*
* @param starHalfDrawable
*/
public void setStarHalfDrawable(Drawable starHalfDrawable) {
this.starHalfDrawable = starHalfDrawable;
}
/**
* 設(shè)置滿星的圖片資源文件
*
* @param starFillDrawable
*/
public void setStarFillDrawable(Drawable starFillDrawable) {
this.starFillDrawable = starFillDrawable;
}
/**
* 設(shè)置空白和默認(rèn)的圖片資源文件
*
* @param starEmptyDrawable
*/
public void setStarEmptyDrawable(Drawable starEmptyDrawable) {
this.starEmptyDrawable = starEmptyDrawable;
}
/**
* 設(shè)置星星是否可以點(diǎn)擊操作
*
* @param clickable
*/
public void setClickable(boolean clickable) {
this.mClickable = clickable;
}
/**
* 設(shè)置星星點(diǎn)擊事件
*
* @param onRatingChangeListener
*/
public void setOnRatingChangeListener(OnRatingChangeListener onRatingChangeListener) {
this.onRatingChangeListener = onRatingChangeListener;
}
/**
* 設(shè)置星星的大小
*
* @param starImageSize
*/
public void setStarImageSize(float starImageSize) {
this.starImageSize = starImageSize;
}
public void setStepSize(StepSize stepSize) {
this.stepSize = stepSize;
}
/**
* 構(gòu)造函數(shù)
* 獲取xml中設(shè)置的資源文件
*
* @param context
* @param attrs
*/
public RatingBar(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) {
//浮點(diǎn)數(shù)的整數(shù)部分
int fint = (int) starStep;
BigDecimal b1 = new BigDecimal(Float.toString(starStep));
BigDecimal b2 = new BigDecimal(Integer.toString(fint));
//浮點(diǎn)數(shù)的小數(shù)部分
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;
}
//點(diǎn)擊之后默認(rèn)每次先增加一顆星蛛勉,再次點(diǎn)擊變?yōu)榘腩w星
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);
}
/**
* 設(shè)置每顆星星的參數(shù)
*
* @return
*/
private ImageView getStarImageView() {
ImageView imageView = new ImageView(getContext());
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
Math.round(starImageSize), Math.round(starImageSize));//設(shè)置每顆星星在線性布局的大小
layout.setMargins(0, 0, Math.round(starPadding), 0);//設(shè)置每顆星星在線性布局的間距
imageView.setLayoutParams(layout);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageDrawable(starEmptyDrawable);
imageView.setMinimumWidth(10);
imageView.setMaxHeight(10);
return imageView;
}
/**
* 設(shè)置星星的個(gè)數(shù)
*
* @param rating
*/
public void setStar(float rating) {
if (onRatingChangeListener != null) {
onRatingChangeListener.onRatingChange(rating);
}
this.starStep = rating;
//浮點(diǎn)數(shù)的整數(shù)部分
int fint = (int) rating;
BigDecimal b1 = new BigDecimal(Float.toString(rating));
BigDecimal b2 = new BigDecimal(Integer.toString(fint));
//浮點(diǎn)數(shù)的小數(shù)部分
float fPoint = b1.subtract(b2).floatValue();
//設(shè)置選中的星星
for (int i = 0; i < fint; ++i) {
((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
}
//設(shè)置沒(méi)有選中的星星
for (int i = fint; i < starCount; i++) {
((ImageView) getChildAt(i)).setImageDrawable(starEmptyDrawable);
}
//小數(shù)點(diǎn)默認(rèn)增加半顆星
if (fPoint > 0) {
((ImageView) getChildAt(fint)).setImageDrawable(starHalfDrawable);
}
}
/**
* 操作星星的點(diǎn)擊事件
*/
public interface OnRatingChangeListener {
/**
* 選中的星星的個(gè)數(shù)
*
* @param RatingCount
*/
void onRatingChange(float ratingCount);
}
/**
* 星星每次增加的方式整星還是半星,枚舉類(lèi)型
* 類(lèi)似于View.GONE
*/
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();
}
}
}
- 在xml中的用法
<com.kejiang.yuandl.view.RatingBar
android:id="@+id/rb"
android:layout_width="360dp"
android:layout_height="50dp"
app:starCount="5"
app:starEmpty="@mipmap/star_grey"
app:starFill="@mipmap/star_yellow"
app:starHalf="@mipmap/star_half_yellow"
app:starImageSize="40dp"
app:starPadding="20dp"
app:starStep="1.5"
app:stepSize="Half"></com.kejiang.yuandl.view.RatingBar>
- 在Activity中的設(shè)置
RatingBar ratingBar= (RatingBar) findViewById(R.id.rb);
ratingBar.setClickable(true);//設(shè)置可否點(diǎn)擊
ratingBar.setStar(2.5f);//設(shè)置顯示的星星個(gè)數(shù)
ratingBar.setStepSize(RatingBar.StepSize.Half);//設(shè)置每次點(diǎn)擊增加一顆星還是半顆星
ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
@Override
public void onRatingChange(float ratingCount) {//點(diǎn)擊星星變化后選中的個(gè)數(shù)
Log.d("RatingBar","RatingBar-Count="+ratingCount);
}
});