亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距

 更新時(shí)間:2016年10月26日 09:31:00   作者:DylanAndroid  
Android開發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分。這篇文章介紹了Android自定義View之自定義評(píng)價(jià)打分控件RatingBar可以自定義星星大小和間距的相關(guān)資料,感興趣的朋友一起看看吧

在Android開發(fā)中,我們經(jīng)常會(huì)用到對(duì)商家或者商品的評(píng)價(jià),運(yùn)用星星進(jìn)行打分。然而在Android系統(tǒng)中自帶的打分控件,RatingBar特別不好用,間距和大小無法改變。所以,我就自定義了一個(gè)特別好用的打分控件。在項(xiàng)目中可以直接使用,特別簡(jiǎn)單。下面直接上圖:

效果圖

這里寫圖片描述

實(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)度float值-->
<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è)置沒有選中的星星
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);
}
/**
* 星星每次增加的方式整星還是半星,枚舉類型
* 類似于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);
}
});

相關(guān)文章

最新評(píng)論