-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 11 additions & 132 deletions
143
ratingtrendlib/src/main/java/com/ritwik/ratingtrendlib/Rating.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,26 @@ | ||
package com.ritwik.ratingtrendlib; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.RectF; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import androidx.annotation.Nullable; | ||
public class Rating { | ||
|
||
public class Rating extends View { | ||
/*Default values*/ | ||
private static final float DEFAULT_STROKE_WIDTH = 4f; | ||
private static final float DEFAULT_FONT_SIZE = 4f; | ||
private int mValue; | ||
private int mStrokeColor; | ||
private int mFillColor; | ||
private Context mContext; | ||
|
||
private static final int DEFAULT_STROKE_COLOR = 0xFF305D02; | ||
private static final int DEFAULT_RATE_VALUE = 5; | ||
private static final float DEFAULT_CORNER_RADIUS = 2f; | ||
public Rating(int value) { | ||
this.mValue = value; | ||
|
||
|
||
|
||
/*** | ||
* User defined values | ||
*/ | ||
private int mWidth; | ||
private int mHeight; | ||
|
||
private Paint mStrokePaint; | ||
private Paint mFillPaint; | ||
private int mRateValue = DEFAULT_RATE_VALUE; | ||
private float mStrokeWidth = DEFAULT_STROKE_WIDTH; | ||
private int mStrokeColor = DEFAULT_STROKE_COLOR; | ||
private int mFillColor = DEFAULT_STROKE_COLOR; | ||
private float mFontSize = DEFAULT_FONT_SIZE; | ||
private float mCornerRadius = DEFAULT_CORNER_RADIUS; | ||
|
||
|
||
|
||
public Rating(Context context) { | ||
this(context, null); | ||
} | ||
|
||
public Rating(Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
public Rating(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
initPaints(); | ||
} | ||
|
||
/*** | ||
* Initializing user defined values | ||
* | ||
* @param context : | ||
* @param attrs : | ||
*/ | ||
private void initAttrs(Context context, AttributeSet attrs){ | ||
|
||
} | ||
|
||
public void initPaints(){ | ||
|
||
|
||
mFillPaint = new Paint(); | ||
mFillPaint.setColor(getResources().getColor(R.color.red)); | ||
mFillPaint.setAntiAlias(true); | ||
mFillPaint.setStyle(Paint.Style.FILL); | ||
|
||
mStrokePaint = new Paint(); | ||
mStrokePaint.setAntiAlias(true); | ||
mStrokePaint.setStyle(Paint.Style.STROKE); | ||
mStrokePaint.setColor(mStrokeColor); | ||
mStrokePaint.setStrokeWidth(mStrokeWidth); | ||
|
||
} | ||
|
||
private int getDefaultWidth(){ | ||
|
||
return 20; | ||
} | ||
private int getDefaultHeight(){ | ||
return 20; | ||
} | ||
|
||
private int getExpectSize(int size, int measureSpec){ | ||
private int getAppropriateStrokeColor(int value){ | ||
int color = 0xff000000; | ||
switch (value){ | ||
|
||
int result = size; | ||
int specMode = MeasureSpec.getMode(measureSpec); | ||
int specSize = MeasureSpec.getSize(measureSpec); | ||
|
||
switch (specMode) { | ||
case MeasureSpec.EXACTLY: | ||
result = specSize; | ||
break; | ||
case MeasureSpec.UNSPECIFIED: | ||
result = size; | ||
break; | ||
case MeasureSpec.AT_MOST: | ||
result = Math.min(size, specSize); | ||
break; | ||
default: | ||
break; | ||
} | ||
return result; | ||
|
||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
int defWidth = getDefaultWidth(); | ||
int defHeight = getDefaultHeight(); | ||
setMeasuredDimension(getExpectSize(defWidth, widthMeasureSpec), | ||
getExpectSize(defHeight, heightMeasureSpec)); | ||
} | ||
|
||
/*** | ||
* @deprecated {@link #DEFAULT_FONT_SIZE} | ||
* @param canvas | ||
*/ | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
|
||
super.onDraw(canvas); | ||
|
||
float left = mStrokeWidth * 0.5f; | ||
float top = mStrokeWidth * 0.5f; | ||
float right = getWidth() - mStrokeWidth * 0.5f; | ||
float bottom = getHeight() - mStrokeWidth * 0.5f; | ||
|
||
canvas.drawRoundRect(new RectF(left, top, right, bottom), | ||
20, 20, mStrokePaint); | ||
|
||
|
||
|
||
} | ||
|
||
@Override | ||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | ||
super.onSizeChanged(w, h, oldw, oldh); | ||
mWidth = getMeasuredWidth(); | ||
mHeight = getMeasuredHeight(); | ||
return color; | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
ratingtrendlib/src/main/java/com/ritwik/ratingtrendlib/RatingTrendView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.ritwik.ratingtrendlib; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.graphics.RectF; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
public class RatingTrendView extends View { | ||
/*Default values*/ | ||
private static final float DEFAULT_STROKE_WIDTH = 4f; | ||
private static final float DEFAULT_FONT_SIZE = 4f; | ||
|
||
private static final int DEFAULT_STROKE_COLOR = 0xFF305D02; | ||
private static final int DEFAULT_RATE_VALUE = 5; | ||
private static final float DEFAULT_CORNER_RADIUS = 2f; | ||
|
||
|
||
|
||
/*** | ||
* User defined values | ||
*/ | ||
private int mWidth; | ||
private int mHeight; | ||
|
||
private int[] mRatingSequence; | ||
|
||
|
||
private Paint mStrokePaint; | ||
private Paint mFillPaint; | ||
private float mStrokeWidth = DEFAULT_STROKE_WIDTH; | ||
private int mStrokeColor = DEFAULT_STROKE_COLOR; | ||
private int mFillColor = DEFAULT_STROKE_COLOR; | ||
private float mFontSize = DEFAULT_FONT_SIZE; | ||
private float mCornerRadius = DEFAULT_CORNER_RADIUS; | ||
|
||
|
||
|
||
public RatingTrendView(Context context) { | ||
this(context, null); | ||
} | ||
|
||
public RatingTrendView(Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
//Focus on this constructor | ||
public RatingTrendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
initAttrs(context, attrs); | ||
initPaints(); | ||
} | ||
|
||
/*** | ||
* Initializing user defined values | ||
* | ||
* @param context : | ||
* @param attrs : | ||
*/ | ||
private void initAttrs(Context context, AttributeSet attrs){ | ||
|
||
} | ||
|
||
public void initPaints(){ | ||
|
||
|
||
mFillPaint = new Paint(); | ||
mFillPaint.setAntiAlias(true); | ||
mFillPaint.setStyle(Paint.Style.FILL); | ||
|
||
mStrokePaint = new Paint(); | ||
mStrokePaint.setAntiAlias(true); | ||
mStrokePaint.setStyle(Paint.Style.STROKE); | ||
mStrokePaint.setColor(mStrokeColor); | ||
mStrokePaint.setStrokeWidth(mStrokeWidth); | ||
|
||
} | ||
|
||
private int getDefaultWidth(){ | ||
|
||
return 20; | ||
} | ||
private int getDefaultHeight(){ | ||
return 20; | ||
} | ||
|
||
private int getExpectSize(int size, int measureSpec){ | ||
|
||
int result = size; | ||
int specMode = MeasureSpec.getMode(measureSpec); | ||
int specSize = MeasureSpec.getSize(measureSpec); | ||
|
||
switch (specMode) { | ||
case MeasureSpec.EXACTLY: | ||
result = specSize; | ||
break; | ||
case MeasureSpec.UNSPECIFIED: | ||
result = size; | ||
break; | ||
case MeasureSpec.AT_MOST: | ||
result = Math.min(size, specSize); | ||
break; | ||
default: | ||
break; | ||
} | ||
return result; | ||
|
||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
int defWidth = getDefaultWidth(); | ||
int defHeight = getDefaultHeight(); | ||
setMeasuredDimension(getExpectSize(defWidth, widthMeasureSpec), | ||
getExpectSize(defHeight, heightMeasureSpec)); | ||
} | ||
|
||
/*** | ||
* @deprecated {@link #DEFAULT_FONT_SIZE} | ||
* @param canvas | ||
*/ | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
|
||
super.onDraw(canvas); | ||
|
||
float left = mStrokeWidth * 0.5f; | ||
float top = mStrokeWidth * 0.5f; | ||
float right = getWidth() - mStrokeWidth * 0.5f; | ||
float bottom = getHeight() - mStrokeWidth * 0.5f; | ||
|
||
canvas.drawRoundRect(new RectF(left, top, right, bottom), | ||
20, 20, mStrokePaint); | ||
|
||
|
||
|
||
} | ||
|
||
@Override | ||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | ||
super.onSizeChanged(w, h, oldw, oldh); | ||
mWidth = getMeasuredWidth(); | ||
mHeight = getMeasuredHeight(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<declare-styleable name="RatingTrendView"> | ||
<attr name="rtv_oneStarBackgroundColor" format="color"/> | ||
<attr name="rtv_oneStarStrokeColor" format="color"/> | ||
<attr name="rtv_twoStarBackgroundColor" format="color"/> | ||
<attr name="rtv_twoStarStrokeColor" format="color"/> | ||
<attr name="rtv_threeStarBackgroundColor" format="color"/> | ||
<attr name="rtv_threeStarStrokeColor" format="color"/> | ||
<attr name="rtv_fourStarBackgroundColor" format="color"/> | ||
<attr name="rtv_fourStarStrokeColor" format="color"/> | ||
<attr name="rtv_fiveStarBackgroundColor" format="color"/> | ||
<attr name="rtv_fiveStarStrokeColor" format="color"/> | ||
<attr name="rtv_starIcon" format="reference"/> | ||
<attr name="rtv_spacing" format="dimension"/> | ||
<attr name="rtv_strokeWidth" format="dimension"/> | ||
</declare-styleable> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="red">#ff0000</color> | ||
<color name="oneStarStroke">#ff0000</color> | ||
<color name="oneStarFill">#ffff00</color> | ||
</resources> |