Skip to content

Commit

Permalink
Merge pull request #41 from ome450901/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
williamyyu committed Nov 29, 2017
2 parents 95db0ba + 6cffb3d commit 9870803
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 70 deletions.
35 changes: 19 additions & 16 deletions README.md
Expand Up @@ -16,9 +16,12 @@ Current we already have three RatingBars :
![](images/screenshot.png)
Icon made by [Freepik](http://www.freepik.com/) from www.flaticon.com

## What's New (v1.3.4)
- Fix AnimationRatingBar out of sync bug.
## What's New (v1.3.5)
- Rename the attributes for more easily know all this library's attributes.
   (`app:rating="2"` change to `app:srb_rating="2"`)
- Implement IsIndicator, Scrollable and Clickable settings.
- Fix Handler null pointer bug.
- Fix AnimationRatingBar out of sync bug.

## Feature
- Allow half star through click event. (contributed by [ANPez](https://github.com/ANPez))
Expand Down Expand Up @@ -48,30 +51,30 @@ allprojects {
}
dependencies {
compile 'com.github.ome450901:SimpleRatingBar:1.3.4'
compile 'com.github.ome450901:SimpleRatingBar:1.3.5'
}
```


### In Xml
```xml
<com.willy.ratingbar.ScaleRatingBar
xmlns:rb="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/simpleRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
rb:numStars="3"
rb:rating="2"
rb:starWidth="30dp"
rb:starHeight="30dp"
rb:starPadding="15dp"
rb:stepSize="0.5"
rb:isIndicator="false"
rb:clickable="true"
rb:scrollable="true"
rb:clearRatingEnabled="true"
rb:drawableEmpty="@drawable/start_empty"
rb:drawableFilled="@drawable/star_filled">
app:srb_numStars="3"
app:srb_rating="2"
app:srb_starWidth="30dp"
app:srb_starHeight="30dp"
app:srb_starPadding="15dp"
app:srb_stepSize="0.5"
app:srb_isIndicator="false"
app:srb_clickable="true"
app:srb_scrollable="true"
app:srb_clearRatingEnabled="true"
app:srb_drawableEmpty="@drawable/start_empty"
app:srb_drawableFilled="@drawable/star_filled">
</com.willy.ratingbar.ScaleRatingBar>
```

Expand Down
16 changes: 7 additions & 9 deletions example/src/main/res/layout/fragment_demo.xml
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:rb="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.willy.example.MainActivity">
android:layout_height="match_parent">

<TextView
android:id="@+id/textview_main_baseratingbar_title"
Expand All @@ -32,9 +29,9 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview_main_baseratingbar_title"
rb:numStars="7"
rb:rating="2.5"
rb:starPadding="5dp" />
app:srb_numStars="7"
app:srb_rating="2.5"
app:srb_starPadding="5dp" />

<TextView
android:id="@+id/textview_main_scaleratingbar_title"
Expand All @@ -60,8 +57,9 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview_main_scaleratingbar_title"
rb:starPadding="10dp"
rb:stepSize="0.5" />
app:srb_starPadding="10dp"
app:srb_rating="4"
app:srb_stepSize="0.5" />

<TextView
android:id="@+id/textview_main_rotationratingbar_title"
Expand Down
Expand Up @@ -2,9 +2,12 @@

import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import java.util.UUID;

/**
* Created by willy on 2017/5/5.
*/
Expand All @@ -13,7 +16,7 @@ public class AnimationRatingBar extends BaseRatingBar {

protected Handler mHandler;
protected Runnable mRunnable;
protected String mRunnableToken = "AnimationRatingBar";
protected String mRunnableToken = UUID.randomUUID().toString();

protected AnimationRatingBar(Context context) {
super(context);
Expand All @@ -34,5 +37,14 @@ private void init() {
mHandler = new Handler();
}

protected void postRunnable(Runnable runnable, long ANIMATION_DELAY) {
if (mHandler == null) {
mHandler = new Handler();
}

long timeMillis = SystemClock.uptimeMillis() + ANIMATION_DELAY;
mHandler.postAtTime(runnable, mRunnableToken, timeMillis);
}

}

54 changes: 31 additions & 23 deletions library/src/main/java/com/willy/ratingbar/BaseRatingBar.java
Expand Up @@ -34,7 +34,7 @@ public interface OnRatingChangeListener {
private static final int MAX_CLICK_DISTANCE = 5;
private static final int MAX_CLICK_DURATION = 200;

private final DecimalFormat mDecimalFormat;
private DecimalFormat mDecimalFormat;

private int mNumStars;
private int mPadding = 0;
Expand Down Expand Up @@ -76,31 +76,30 @@ public BaseRatingBar(Context context, @Nullable AttributeSet attrs) {
public BaseRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingBarAttributes);
float rating = typedArray.getFloat(R.styleable.RatingBarAttributes_rating, mRating);
mNumStars = typedArray.getInt(R.styleable.RatingBarAttributes_numStars, mNumStars);
mPadding = typedArray.getDimensionPixelSize(R.styleable.RatingBarAttributes_starPadding, mPadding);
mStarWidth = typedArray.getDimensionPixelSize(R.styleable.RatingBarAttributes_starWidth, 0);
mStarHeight = typedArray.getDimensionPixelSize(R.styleable.RatingBarAttributes_starHeight, 0);
mStepSize = typedArray.getFloat(R.styleable.RatingBarAttributes_stepSize, mStepSize);
mEmptyDrawable = typedArray.hasValue(R.styleable.RatingBarAttributes_drawableEmpty) ? ContextCompat.getDrawable(context, typedArray.getResourceId(R.styleable.RatingBarAttributes_drawableEmpty, View.NO_ID)) : null;
mFilledDrawable = typedArray.hasValue(R.styleable.RatingBarAttributes_drawableFilled) ? ContextCompat.getDrawable(context, typedArray.getResourceId(R.styleable.RatingBarAttributes_drawableFilled, View.NO_ID)) : null;
mIsIndicator = typedArray.getBoolean(R.styleable.RatingBarAttributes_isIndicator, mIsIndicator);
mIsScrollable = typedArray.getBoolean(R.styleable.RatingBarAttributes_scrollable, mIsScrollable);
mIsClickable = typedArray.getBoolean(R.styleable.RatingBarAttributes_clickable, mIsClickable);
mClearRatingEnabled = typedArray.getBoolean(R.styleable.RatingBarAttributes_clearRatingEnabled, mClearRatingEnabled);
typedArray.recycle();

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
mDecimalFormat = new DecimalFormat("#.##", symbols);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseRatingBar);
final float rating = typedArray.getFloat(R.styleable.BaseRatingBar_srb_rating, 0);

initParamsValue(typedArray, context);
verifyParamsValue();

initRatingView();
setRating(rating);
}

private void initParamsValue(TypedArray typedArray, Context context) {
mNumStars = typedArray.getInt(R.styleable.BaseRatingBar_srb_numStars, mNumStars);
mPadding = typedArray.getDimensionPixelSize(R.styleable.BaseRatingBar_srb_starPadding, mPadding);
mStarWidth = typedArray.getDimensionPixelSize(R.styleable.BaseRatingBar_srb_starWidth, 0);
mStarHeight = typedArray.getDimensionPixelSize(R.styleable.BaseRatingBar_srb_starHeight, 0);
mStepSize = typedArray.getFloat(R.styleable.BaseRatingBar_srb_stepSize, mStepSize);
mEmptyDrawable = typedArray.hasValue(R.styleable.BaseRatingBar_srb_drawableEmpty) ? ContextCompat.getDrawable(context, typedArray.getResourceId(R.styleable.BaseRatingBar_srb_drawableEmpty, View.NO_ID)) : null;
mFilledDrawable = typedArray.hasValue(R.styleable.BaseRatingBar_srb_drawableFilled) ? ContextCompat.getDrawable(context, typedArray.getResourceId(R.styleable.BaseRatingBar_srb_drawableFilled, View.NO_ID)) : null;
mIsIndicator = typedArray.getBoolean(R.styleable.BaseRatingBar_srb_isIndicator, mIsIndicator);
mIsScrollable = typedArray.getBoolean(R.styleable.BaseRatingBar_srb_scrollable, mIsScrollable);
mIsClickable = typedArray.getBoolean(R.styleable.BaseRatingBar_srb_clickable, mIsClickable);
mClearRatingEnabled = typedArray.getBoolean(R.styleable.BaseRatingBar_srb_clearRatingEnabled, mClearRatingEnabled);
typedArray.recycle();
}

private void verifyParamsValue() {
if (mNumStars <= 0) {
mNumStars = 5;
Expand Down Expand Up @@ -142,7 +141,6 @@ private void initRatingView() {

private PartialView getPartialView(final int ratingViewId, Drawable filledDrawable, Drawable emptyDrawable) {
PartialView partialView = new PartialView(getContext());
// partialView.setId(ratingViewId);
partialView.setTag(ratingViewId);
partialView.setPadding(mPadding, mPadding, mPadding, mPadding);
partialView.setFilledDrawable(filledDrawable);
Expand Down Expand Up @@ -329,9 +327,10 @@ private void handleMoveEvent(float eventX) {
}

private float calculateRating(float eventX, PartialView partialView) {
float ratioOfView = Float.parseFloat(mDecimalFormat.format((eventX - partialView.getLeft()) / partialView.getWidth()));
DecimalFormat decimalFormat = getDecimalFormat();
float ratioOfView = Float.parseFloat(decimalFormat.format((eventX - partialView.getLeft()) / partialView.getWidth()));
float steps = Math.round(ratioOfView / mStepSize) * mStepSize;
return Float.parseFloat(mDecimalFormat.format((int) partialView.getTag() - (1 - steps)));
return Float.parseFloat(decimalFormat.format((int) partialView.getTag() - (1 - steps)));
}

private void handleClickEvent(float eventX) {
Expand Down Expand Up @@ -410,6 +409,15 @@ public void setStepSize(@FloatRange(from = 0.1, to = 1.0) float stepSize) {
this.mStepSize = stepSize;
}

public DecimalFormat getDecimalFormat() {
if (mDecimalFormat == null) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
mDecimalFormat = new DecimalFormat("#.##", symbols);
}
return mDecimalFormat;
}

@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
Expand Down
@@ -1,7 +1,6 @@
package com.willy.ratingbar;

import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
Expand Down Expand Up @@ -64,9 +63,7 @@ protected void fillRatingBar(final float rating) {
}

mRunnable = getAnimationRunnable(rating, partialView, ratingViewId, maxIntOfRating);

long timeMillis = SystemClock.uptimeMillis() + ANIMATION_DELAY;
mHandler.postAtTime(mRunnable, mRunnableToken, timeMillis);
postRunnable(mRunnable, ANIMATION_DELAY);
}
}

Expand Down
@@ -1,7 +1,6 @@
package com.willy.ratingbar;

import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
Expand Down Expand Up @@ -64,9 +63,7 @@ protected void fillRatingBar(final float rating) {
}

mRunnable = getAnimationRunnable(rating, partialView, ratingViewId, maxIntOfRating);

long timeMillis = SystemClock.uptimeMillis() + ANIMATION_DELAY;
mHandler.postAtTime(mRunnable, mRunnableToken, timeMillis);
postRunnable(mRunnable, ANIMATION_DELAY);
}
}

Expand Down
26 changes: 13 additions & 13 deletions library/src/main/res/values/attrs.xml
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="RatingBarAttributes">
<attr name="numStars" format="integer" />
<attr name="rating" format="float" />
<attr name="starPadding" format="dimension" />
<attr name="drawableEmpty" format="reference" />
<attr name="drawableFilled" format="reference" />
<attr name="isIndicator" format="boolean" />
<attr name="scrollable" format="boolean" />
<attr name="clickable" format="boolean" />
<attr name="clearRatingEnabled" format="boolean" />
<attr name="starWidth" format="dimension" />
<attr name="starHeight" format="dimension" />
<attr name="stepSize" format="float" />
<declare-styleable name="BaseRatingBar">
<attr name="srb_numStars" format="integer" />
<attr name="srb_rating" format="float" />
<attr name="srb_starPadding" format="dimension" />
<attr name="srb_drawableEmpty" format="reference" />
<attr name="srb_drawableFilled" format="reference" />
<attr name="srb_isIndicator" format="boolean" />
<attr name="srb_scrollable" format="boolean" />
<attr name="srb_clickable" format="boolean" />
<attr name="srb_clearRatingEnabled" format="boolean" />
<attr name="srb_starWidth" format="dimension" />
<attr name="srb_starHeight" format="dimension" />
<attr name="srb_stepSize" format="float" />
</declare-styleable>
</resources>

0 comments on commit 9870803

Please sign in to comment.