package com.ysd.keepcar.view.shop.model; 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.ysd.keepcar.R; import java.math.BigDecimal; /** * Created by 郝悅 on 2017/12/28. */ public class MyRatingBar 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; /** * 設置半星的圖片資源文件 * * @param starHalfDrawable */ public void setStarHalfDrawable(Drawable starHalfDrawable) { this.starHalfDrawable = starHalfDrawable; } /** * 設置滿星的圖片資源文件 * * @param starFillDrawable */ public void setStarFillDrawable(Drawable starFillDrawable) { this.starFillDrawable = starFillDrawable; } /** * 設置空白和默認的圖片資源文件 * * @param starEmptyDrawable */ public void setStarEmptyDrawable(Drawable starEmptyDrawable) { this.starEmptyDrawable = starEmptyDrawable; } /** * 設置星星是否能夠點擊操做 * * @param clickable */ public void setClickable(boolean clickable) { this.mClickable = clickable; } /** * 設置星星點擊事件 * * @param onRatingChangeListener */ public void setOnRatingChangeListener(OnRatingChangeListener onRatingChangeListener) { this.onRatingChangeListener = onRatingChangeListener; } /** * 設置星星的大小 * * @param starImageSize */ public void setStarImageSize(float starImageSize) { this.starImageSize = starImageSize; } public void setStepSize(StepSize stepSize) { this.stepSize = stepSize; } /** * 構造函數 * 獲取xml中設置的資源文件 * * @param context * @param attrs */ public MyRatingBar(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); } /** * 設置每顆星星的參數 * * @return */ 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; } /** * 設置星星的個數 * * @param rating */ 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); } /** * 星星每次增長的方式整星仍是半星,枚舉類型 * 相似於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(); } } }