package com.willy.ratingbar;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* Created by willy on 2017/5/5.
*/
public class BaseRatingBar extends DirectionalLayout implements SimpleRatingBar, Component.TouchEventListener {
private static final String FORMAT = "image/jpg";
private int mNumStars;
private int mPadding = 20;
private int mStarWidth;
private int mStarHeight;
private float mMinimumStars = 0;
private float mRating = -1;
private float mStepSize = 1f;
private float mPreviousRating = 0;
private float mNmu = 10;
private boolean mIsIndicator = false;
private boolean mIsScrollable = true;
private boolean mIsClickable = true;
private boolean mClearRatingEnabled = true;
private float mStartX;
private float mStartY;
private PixelMap mEmptyDrawable;
private PixelMap mFilledDrawable;
private OnRatingChangeListener mOnRatingChangeListener;
protected List<PartialView> mPartialViews;
public BaseRatingBar(Context context, AttrSet attrs) {
this(context, attrs, "0");
}
/**
* @param context context
* @param attrs attributes from XML => app:mainText="mainText"
* @param defStyleAttr attributes from default style (Application theme or theme)
*/
public BaseRatingBar(Context context, AttrSet attrs, String defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(HORIZONTAL);
initParamsValue(attrs, context);
verifyParamsValue();
initRatingView();
float rating = 0;
if (isPresent(attrs, "srb_rating")) {
rating = attrs.getAttr("srb_rating").get().getFloatValue();
}
setRating(rating);
setTouchEventListener(this);
}
private void initParamsValue(AttrSet attrs, Context context) {
mEmptyDrawable = getPixelMap("resources/rawfile/empty.png");
mFilledDrawable = getPixelMap("resources/rawfile/filled.png");
if (isPresent(attrs, "srb_numStars")) {
mNumStars = attrs.getAttr("srb_numStars").get().getIntegerValue();
}
if (isPresent(attrs, "srb_starPadding")) {
mPadding = attrs.getAttr("srb_starPadding").get().getDimensionValue();
}
if (isPresent(attrs, "srb_starWidth")) {
mStarWidth = attrs.getAttr("srb_starWidth").get().getDimensionValue();
}
if (isPresent(attrs, "srb_starHeight")) {
mStarHeight = attrs.getAttr("srb_starHeight").get().getDimensionValue();
}
if (isPresent(attrs, "srb_stepSize")) {
mStepSize = attrs.getAttr("srb_stepSize").get().getFloatValue();
}
if (isPresent(attrs, "srb_minimumStars")) {
mMinimumStars = attrs.getAttr("srb_minimumStars").get().getFloatValue();
}
if (isPresent(attrs, "srb_isIndicator")) {
mIsIndicator = attrs.getAttr("srb_isIndicator").get().getBoolValue();
}
if (isPresent(attrs, "srb_scrollable")) {
mIsScrollable = attrs.getAttr("srb_scrollable").get().getBoolValue();
}
if (isPresent(attrs, "srb_clickable")) {
mIsClickable = attrs.getAttr("srb_clickable").get().getBoolValue();
}
if (isPresent(attrs, "srb_clearRatingEnabled")) {
mClearRatingEnabled = attrs.getAttr("srb_clearRatingEnabled").get().getBoolValue();
}
}
private boolean isPresent(AttrSet attrs, String name) {
return attrs.getAttr(name).isPresent();
}
private void verifyParamsValue() {
if (mNumStars <= 0) {
mNumStars = 5;
}
if (mPadding < 0) {
mPadding = 0;
}
if (mEmptyDrawable == null) {
mEmptyDrawable = getPixelMap("resources/rawfile/empty.png");
}
if (mFilledDrawable == null) {
mFilledDrawable = getPixelMap("resources/rawfile/filled.png");
}
if (mStepSize > 1.0f) {
mStepSize = 1.0f;
} else if (mStepSize < 0.1f) {
mStepSize = 0.1f;
}
mMinimumStars = RatingBarUtils.getValidMinimumStars(mMinimumStars, mNumStars, mStepSize);
}
private PixelMap getPixelMap(String rawfile) {
ResourceManager resManager = getResourceManager();
RawFileEntry rawFileEntry = resManager.getRawFileEntry(rawfile);
try {
InputStream resource = rawFileEntry.openRawFile();
ImageSource imageSource = ImageSource.create(resource, null);
return imageSource.createPixelmap(null);
} catch (IOException e) {
System.out.println(e);
}
return null;
}
private void initRatingView() {
mPartialViews = new ArrayList<>();
for (int pos = 1; pos <= mNumStars; pos++) {
PartialView partialView = getPartialView(pos, mStarWidth, mStarHeight, mPadding, mFilledDrawable, mEmptyDrawable);
addComponent(partialView);
mPartialViews.add(partialView);
}
}
private PartialView getPartialView(final int partialViewId, int starWidth, int starHeight, int padding,
PixelMap filledDrawable, PixelMap emptyDrawable) {
PartialView partialView = new PartialView(getContext(), partialViewId, starWidth, starHeight, padding);
partialView.setFilledDrawable(filledDrawable);
partialView.setEmptyDrawable(emptyDrawable);
return partialView;
}
/**
* Retain this method to let other RatingBar can custom their decrease animation.
*/
protected void emptyRatingBar() {
fillRatingBar(0);
}
/**
* the view which id is 3 also need to be filled
*
* @param rating rating
*/
protected void fillRatingBar(final float rating) {
double maxIntOfRating = Math.ceil(rating);
for (PartialView partialView : mPartialViews) {
int ratingViewId = (int) partialView.getTag();
if (ratingViewId > maxIntOfRating) {
partialView.setEmpty();
continue;
} else {
partialView.setFilled();
}
}
}
@Override
public void setNumStars(int numStars) {
if (numStars <= 0) {
return;
}
mPartialViews.clear();
removeAllComponents();
mNumStars = numStars;
initRatingView();
}
@Override
public int getNumStars() {
return mNumStars;
}
@Override
public void setRating(float rating) {
setRating(rating, false);
}
private void setRating(float rating, boolean fromUser) {
if (rating > mNumStars) {
rating = mNumStars;
}
if (rating < mMinimumStars) {
rating = mMinimumStars;
}
BigDecimal bignum1 = new BigDecimal(mRating);
BigDecimal bignum2 = new BigDecimal(rating);
float value = bignum1.subtract(bignum2).floatValue();
if (Math.abs(value) <= 0) {
return;
}
// Respect Step size. So if the defined step size is 0.5, and we're attributing it a 4.7 rating,
// it should actually be set to `4.5` rating.
mRating = Double.valueOf(Math.floor(rating / mStepSize)).floatValue() * mStepSize;
if (mOnRatingChangeListener != null) {
mOnRatingChangeListener.onRatingChange(this, mRating, fromUser);
}
fillRatingBar(mRating);
}
一个简单的星级评分,您可以更轻松地自定义图像和动画
需积分: 5 61 浏览量
2024-05-01
09:45:07
上传
评论
收藏 616KB ZIP 举报
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6732
最新资源
- Python 程序语言设计模式思路-行为型模式:职责链模式:将请求从一个处理者传递到下一个处理者
- 9241703124789646.16健身系统2.apk
- postgresql-16.3-1-windows-x64.exe
- Python 程序语言设计模式思路-结构型模式:装饰器讲解及利用Python装饰器模式实现高效日志记录和性能测试
- 基于YOLOv5和DeepSORT的多目标跟踪仿真与记录
- Python 程序语言设计模式思路-创建型模式:原型模式:通过复制现有对象来创建新对象,面向对象编程
- 卸载软件geek卸载软件geek
- Python 程序语言设计模式思路-创建型模式:单例模式,确保一个类的唯一实例(装饰器)面向对象编程、继承
- skywalking-plugins.jar skywalking-alarm.jar
- 独栋别墅图纸D020-两层-10.00&11.00米- 施工图.dwg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈