From 3e1c8afbca9544c55e78eed0374babc05e0a3266 Mon Sep 17 00:00:00 2001 From: liujing Date: Fri, 16 Oct 2020 18:51:23 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E8=87=AA=E5=AE=9A=E4=B9=89ratingbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/common/view/CustomRatingBar.java | 298 ++++++++++++++++++ ...deules_commons_layout_custom_ratingbar.xml | 7 + .../src/main/res/values-xhdpi/dimens.xml | 4 +- .../src/main/res/values/attr.xml | 35 +- .../v2x/adapter/V2XShareEventAdapter.java | 5 +- .../module_v2x_event_share_description.xml | 6 +- 6 files changed, 343 insertions(+), 12 deletions(-) create mode 100644 modules/mogo-module-common/src/main/java/com/mogo/module/common/view/CustomRatingBar.java create mode 100644 modules/mogo-module-common/src/main/res/layout/modeules_commons_layout_custom_ratingbar.xml diff --git a/modules/mogo-module-common/src/main/java/com/mogo/module/common/view/CustomRatingBar.java b/modules/mogo-module-common/src/main/java/com/mogo/module/common/view/CustomRatingBar.java new file mode 100644 index 0000000000..f896e216c6 --- /dev/null +++ b/modules/mogo-module-common/src/main/java/com/mogo/module/common/view/CustomRatingBar.java @@ -0,0 +1,298 @@ +package com.mogo.module.common.view; + +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 androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; + +import com.mogo.module.common.R; + +import java.math.BigDecimal; + +public class CustomRatingBar extends LinearLayout { + private Context mContext; + /* + 是否可点击 + * */ + private boolean mClickable; + /* + * 单元总数(心形/星星) + * */ + private int elementCount; + /* + * 单元点击事件 + * */ + private OnRatingChangeListener mOnRatingBarChangeListener; + /* + * 每个单元的大小 + * */ + private float elementWidth; + + private float elementHeight; + + public float getElementWidth() { + return elementWidth; + } + + public void setElementWidth(float elementWidth) { + this.elementWidth = elementWidth; + } + + public float getElementHeight() { + return elementHeight; + } + + public void setElementHeight(float elementHeight) { + this.elementHeight = elementHeight; + } + + /* + * 每个单元间的距离 + * */ + private float elementPadding; + /* + *单元的显示数量,支持小数点 + * */ + private float elementStep; + /* + * 空白的默认单元图片 + * */ + private Drawable elementEmptyDrawable; + /* + * 选中后的单元图片 + * */ + private Drawable elementFillDrawable; + /* + * 半颗单元图片 + * */ + private Drawable elementHarfDrawable; + /* + * 每次点击单元所增加的量是半个还是整个 + * */ + private StepSize stepSize; + + @Override + public boolean isClickable() { + return mClickable; + } + + @Override + public void setClickable(boolean clickable) { + mClickable = clickable; + } + + public int getElementCount() { + return elementCount; + } + + public void setElementCount(int elementCount) { + this.elementCount = elementCount; + } + + public OnRatingChangeListener getOnRatingBarChangeListener() { + return mOnRatingBarChangeListener; + } + + public void setOnRatingChangeListener(OnRatingChangeListener onRatingBarChangeListener) { + mOnRatingBarChangeListener = onRatingBarChangeListener; + } + + public float getElementPadding() { + return elementPadding; + } + + public void setElementPadding(float elementPadding) { + this.elementPadding = elementPadding; + } + + public float getElementStep() { + return elementStep; + } + + public void setElementStep(float elementStep) { + this.elementStep = elementStep; + } + + public Drawable getElementEmptyDrawable() { + return elementEmptyDrawable; + } + + public void setElementEmptyDrawable(Drawable elementEmptyDrawable) { + this.elementEmptyDrawable = elementEmptyDrawable; + } + + public Drawable getElementFillDrawable() { + return elementFillDrawable; + } + + public void setElementFillDrawable(Drawable elementFillDrawable) { + this.elementFillDrawable = elementFillDrawable; + } + + public Drawable getElementHarfDrawable() { + return elementHarfDrawable; + } + + public void setElementHarfDrawable(Drawable elementHarfDrawable) { + this.elementHarfDrawable = elementHarfDrawable; + } + + + public void setStepSize(StepSize stepSize) { + this.stepSize = stepSize; + } + + public CustomRatingBar(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + mContext = context; + setOrientation(LinearLayout.HORIZONTAL); + TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomRatingBar); + elementWidth = mTypedArray.getDimension(R.styleable.CustomRatingBar_elenmentWidth, + context.getResources().getDimension(R.dimen.heart_ratingbar_width)); + elementHeight = mTypedArray.getDimension(R.styleable.CustomRatingBar_elenmentHeight, + context.getResources().getDimension(R.dimen.heart_ratingbar_height)); + elementPadding = mTypedArray.getDimension(R.styleable.CustomRatingBar_elenmentPadding, 10); + + elementStep = mTypedArray.getFloat(R.styleable.CustomRatingBar_elenmentStep, 1.0f); + stepSize = StepSize.fromStep(mTypedArray.getInt(R.styleable.CustomRatingBar_stepSize, 1)); + elementCount = mTypedArray.getInteger(R.styleable.CustomRatingBar_elenmentCount, 5); + stepSize = StepSize.Half; + + Drawable drawable_empty_default = ContextCompat.getDrawable(context, R.drawable.icon_heart_unchoose); + Drawable drawable_harf_default = ContextCompat.getDrawable(context, R.drawable.icon_heart_second); + Drawable drawable_fill_default = ContextCompat.getDrawable(context, R.drawable.icon_heart_choose); + + Drawable drawable_empty = mTypedArray.getDrawable(R.styleable.CustomRatingBar_elenmentEmpty); + Drawable drawable_harf = mTypedArray.getDrawable(R.styleable.CustomRatingBar_elenmentHarf); + Drawable drawable_fill = mTypedArray.getDrawable(R.styleable.CustomRatingBar_elenmentFill); + + elementEmptyDrawable = drawable_empty != null ? drawable_empty : drawable_empty_default; + elementFillDrawable = drawable_harf != null ? drawable_harf : drawable_fill_default; + elementHarfDrawable = drawable_fill != null ? drawable_fill : drawable_harf_default; + mClickable = mTypedArray.getBoolean(R.styleable.CustomRatingBar_clickable, false); + mTypedArray.recycle(); + for (int i = 0; i < elementCount; ++i) { + final ImageView imageView = getElenmentImageView(); + imageView.setImageDrawable(elementEmptyDrawable); + imageView.setOnClickListener( + new OnClickListener() { + @Override + public void onClick(View v) { + if (mClickable) { + //浮点数的整数部分 + int fint = (int) elementStep; + BigDecimal b1 = new BigDecimal(Float.toString(elementStep)); + BigDecimal b2 = new BigDecimal(Integer.toString(fint)); + //浮点数的小数部分 + float fPoint = b1.subtract(b2).floatValue(); + if (fPoint == 0) { + fint -= 1; + } + + if (indexOfChild(v) > fint) { + setRating(indexOfChild(v) + 1); + } else if (indexOfChild(v) == fint) { + if (stepSize == StepSize.Full) {//如果是满星 就不考虑半颗星了 + return; + } + //点击之后默认每次先增加一颗星,再次点击变为半颗星 + if (imageView.getDrawable().getCurrent().getConstantState(). + equals(elementHarfDrawable.getConstantState())) { + setRating(indexOfChild(v) + 1); + } else { + setRating(indexOfChild(v) + 0.5f); + } + } else { + setRating(indexOfChild(v) + 1f); + } + + } + } + } + ); + addView(imageView); + } + setRating(elementStep); + } + + private ImageView getElenmentImageView() { + ImageView imageView = new ImageView(getContext()); + LayoutParams layout = new LayoutParams( + Math.round(mContext.getResources().getDimension(R.dimen.heart_ratingbar_width)), + Math.round(mContext.getResources().getDimension(R.dimen.heart_ratingbar_width)));//设置每个单元格在线性布局的大小 + layout.setMargins(0, 0, Math.round(elementPadding), 0);//设置每颗星星在线性布局的间距 + imageView.setLayoutParams(layout); + imageView.setAdjustViewBounds(true); + imageView.setScaleType(ImageView.ScaleType.FIT_XY); + imageView.setImageDrawable(elementEmptyDrawable); + imageView.setMinimumWidth((int) elementWidth); + imageView.setMaxWidth((int) elementWidth); + imageView.setMinimumHeight((int) elementHeight); + imageView.setMaxHeight((int) elementHeight); + return imageView; + } + + public void setRating(float rating) { + if (rating > elementCount) + rating = elementCount; + + if (mOnRatingBarChangeListener != null) { + mOnRatingBarChangeListener.onRatingChange(rating); + } + this.elementStep = 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(elementFillDrawable); + } + //设置没有选中的单元 + for (int i = fint; i < elementCount; i++) { + ((ImageView) getChildAt(i)).setImageDrawable(elementEmptyDrawable); + } + //小数点默认增加半个 + if (fPoint > 0) { + ((ImageView) getChildAt(fint)).setImageDrawable(elementHarfDrawable); + } + + + } + + 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(); + } + } + +} diff --git a/modules/mogo-module-common/src/main/res/layout/modeules_commons_layout_custom_ratingbar.xml b/modules/mogo-module-common/src/main/res/layout/modeules_commons_layout_custom_ratingbar.xml new file mode 100644 index 0000000000..ac3fb54105 --- /dev/null +++ b/modules/mogo-module-common/src/main/res/layout/modeules_commons_layout_custom_ratingbar.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/modules/mogo-module-common/src/main/res/values-xhdpi/dimens.xml b/modules/mogo-module-common/src/main/res/values-xhdpi/dimens.xml index 8f60bbf8ef..9692e54e2a 100644 --- a/modules/mogo-module-common/src/main/res/values-xhdpi/dimens.xml +++ b/modules/mogo-module-common/src/main/res/values-xhdpi/dimens.xml @@ -1054,8 +1054,8 @@ 48px -10px 10px - 36px - 30px + 40px + 43px 300px 281px 90px diff --git a/modules/mogo-module-common/src/main/res/values/attr.xml b/modules/mogo-module-common/src/main/res/values/attr.xml index 2bb191d2d9..c18d5a5d09 100644 --- a/modules/mogo-module-common/src/main/res/values/attr.xml +++ b/modules/mogo-module-common/src/main/res/values/attr.xml @@ -8,9 +8,36 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/adapter/V2XShareEventAdapter.java b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/adapter/V2XShareEventAdapter.java index 9412e5dc79..aacc3f1457 100644 --- a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/adapter/V2XShareEventAdapter.java +++ b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/adapter/V2XShareEventAdapter.java @@ -12,6 +12,7 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.alibaba.android.arouter.launcher.ARouter; +import com.mogo.module.common.view.CustomRatingBar; import com.mogo.module.v2x.R; import com.mogo.module.v2x.entity.panel.V2XShareEventDescription; import com.mogo.module.v2x.entity.panel.V2XShareEventItem; @@ -90,7 +91,7 @@ public class V2XShareEventAdapter extends RecyclerView.Adapter - + android:layout_height="wrap_content" />