Skip to content

Commit

Permalink
Quite good
Browse files Browse the repository at this point in the history
  • Loading branch information
ritwikdax committed Sep 2, 2019
1 parent dc48995 commit 6e59877
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 44 deletions.
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
tools:context=".MainActivity">

<com.ritwik.ratingtrendlib.RatingTrendView
android:layout_width="30dp"
android:layout_height="30dp"/>
android:id="@+id/rtv_ratingTrend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>



Expand Down
50 changes: 41 additions & 9 deletions ratingtrendlib/src/main/java/com/ritwik/ratingtrendlib/Rating.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
package com.ritwik.ratingtrendlib;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

public class Rating {

private int mValue;
private int mStrokeColor;
private int mFillColor;
private Context mContext;
private Paint mStrokePaint;
private Paint mFillPaint;
private float mCornerRadius;
private RectF filledRect;
private RectF strokeRect;

public Rating(int value, float cornerRadius) {

public Rating(int value) {
this.mValue = value;
this.mCornerRadius = cornerRadius;
filledRect = new RectF();
strokeRect = new RectF();

}

public void setmValue(int mValue) {
this.mValue = mValue;
}

public void setmStrokePaint(Paint mStrokePaint) {
this.mStrokePaint = mStrokePaint;
}

public void setmFillPaint(Paint mFillPaint) {
this.mFillPaint = mFillPaint;
}

public int getmValue() {
return mValue;
}

public Paint getmStrokePaint() {
return mStrokePaint;
}

public Paint getmFillPaint() {
return mFillPaint;
}

private int getAppropriateStrokeColor(int value){
int color = 0xff000000;
switch (value){
public void drawSelf(float left, float top, float right, float bottom, Canvas canvas){

filledRect.set(left, top, right, bottom);
strokeRect.set(left, top, right, bottom);
canvas.drawRoundRect(filledRect, mCornerRadius, mCornerRadius, mFillPaint);
canvas.drawRoundRect(strokeRect, mCornerRadius, mCornerRadius, mStrokePaint);

}
return color;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package com.ritwik.ratingtrendlib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;



public class RatingTrendView extends View {

private static final String TAG = "Rating";

/*Default values*/
private static final float DEFAULT_STROKE_WIDTH = 4f;
private static final float DEFAULT_FONT_SIZE = 4f;
private static final float DEFAULT_SPACING = 5f;

private static final int DEFAULT_STROKE_COLOR = 0xFF305D02;
private static final int DEFAULT_RATE_VALUE = 5;
private static final float DEFAULT_CORNER_RADIUS = 2f;
private static final float DEFAULT_CORNER_RADIUS = 20f;
private static final float DEFAULT_WIDTH_BY_HEIGHT_RATIO = 1.3f;




Expand All @@ -27,15 +38,36 @@ public class RatingTrendView extends View {
private int mHeight;

private int[] mRatingSequence;
private Rating[] mRatings;

private int mOneStarStrokeColor;
private int mOneStarFillColor;
private int mTwoStarStrokeColor;
private int mTwoStarFillColor;
private int mThreeStarStrokeColor;
private int mThreeStarFillColor;
private int mFourStarStrokeColor;
private int mFourStarFillColor;
private int mFiveStarStrokeColor;
private int mFiveStarFillColor;

private float mStrokeWidth = DEFAULT_STROKE_WIDTH;
private float mCornerRadius = DEFAULT_CORNER_RADIUS;
private float mSpacing = DEFAULT_SPACING;
private int mStarIcon;


private int mBoxWidth;
private int mBoxHeight;



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;




Expand All @@ -51,9 +83,10 @@ public RatingTrendView(Context context, @Nullable AttributeSet attrs) {
public RatingTrendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
initPaints();
prepareRatingSeq();
}


/***
* Initializing user defined values
*
Expand All @@ -62,32 +95,66 @@ public RatingTrendView(Context context, @Nullable AttributeSet attrs, int defSty
*/
private void initAttrs(Context context, AttributeSet attrs){

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingTrendView);
mCornerRadius = typedArray.getDimension(R.styleable.RatingTrendView_rtv_cornerRadius,
DEFAULT_CORNER_RADIUS);
mStrokeWidth = typedArray.getDimension(R.styleable.RatingTrendView_rtv_strokeWidth,
DEFAULT_STROKE_WIDTH);
mStarIcon = typedArray.getResourceId(R.styleable.RatingTrendView_rtv_starIcon, R.drawable.ic_star);




/***
* Initializing colours
*/
mOneStarStrokeColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.oneStarStroke));
mOneStarFillColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.oneStarFill));
mTwoStarStrokeColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.twoStarStroke));
mTwoStarFillColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.twoStarFill));
mThreeStarStrokeColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.threeStarStroke));
mThreeStarFillColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.threeStarFill));
mFourStarStrokeColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.fourStarStroke));
mFourStarFillColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.fourStarFill));
mFiveStarStrokeColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.fiveStarStroke));
mFiveStarFillColor = typedArray.getColor(R.styleable.RatingTrendView_rtv_oneStarStrokeColor,
getResources().getColor(R.color.fiveStarFill));

typedArray.recycle();

}

public void initPaints(){
private void prepareRatingSeq(){

mRatingSequence = new int[]{1, 3, 5, 4, 1, 5,3,4};
mRatings = new Rating[8];

mFillPaint = new Paint();
mFillPaint.setAntiAlias(true);
mFillPaint.setStyle(Paint.Style.FILL);
for (int i=0; i<8 ; i++){
mRatings[i] = getRating(mRatingSequence[i]);
}

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 getDefaultHeight(int measureSpec){
int width = MeasureSpec.getSize(measureSpec);
return (int) ((width/8) / DEFAULT_WIDTH_BY_HEIGHT_RATIO) + getPaddingBottom() + getPaddingTop();
}

private int getExpectSize(int size, int measureSpec){
private int getExpectedSize(int size, int measureSpec){

int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
Expand All @@ -113,9 +180,15 @@ private int getExpectSize(int size, int measureSpec){
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int defWidth = getDefaultWidth();
int defHeight = getDefaultHeight();
setMeasuredDimension(getExpectSize(defWidth, widthMeasureSpec),
getExpectSize(defHeight, heightMeasureSpec));
int defHeight = getDefaultHeight(widthMeasureSpec);

setMeasuredDimension(getExpectedSize(defWidth, widthMeasureSpec),
getExpectedSize(defHeight, heightMeasureSpec));

mBoxHeight = getHeight();
mBoxWidth = getWidth();

Log.d(TAG, "onMeasure: "+ mBoxWidth +" "+mBoxHeight );
}

/***
Expand All @@ -128,15 +201,26 @@ 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;
int bw = mBoxWidth/8;


canvas.drawRoundRect(new RectF(left, top, right, bottom),
20, 20, mStrokePaint);
float left = 0.5f *mStrokeWidth;
float top = 0.5f * mStrokeWidth;
float right =(mBoxWidth/8) - (0.5f *mStrokeWidth) ;
float bottom = mBoxHeight - (0.5f *mStrokeWidth);
int i=1;
canvas.save();
for (Rating rating: mRatings){


rating.drawSelf(left, top, right, bottom, canvas);
canvas.translate(bw + mSpacing, 0);

i++;

}
canvas.restore();


}

Expand All @@ -145,5 +229,83 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();

mBoxHeight = getHeight();
mBoxWidth = getWidth();
mBoxWidth = (int) (mBoxWidth - (7* mSpacing));

Log.d(TAG, "onSizeChanged: getHeight()"+ mBoxHeight + " "+ mBoxWidth);
Log.d(TAG, "onSizeChanged: getMeasuredWidth()" + " " + mWidth +" "+ mHeight );

}

/***
* Give appropriate rating obj according to value like 1* 2* 3* etc
*
* @param value : must be in range(1,5)
* @return : Rating obj
*
*/
private Rating getRating(int value){

Rating rating = new Rating(value, mCornerRadius);
Paint strokePaint = new Paint();
Paint fillPaint = new Paint();

strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(mStrokeWidth);
strokePaint.setAntiAlias(true);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setAntiAlias(true);


switch (value){
case 1:
strokePaint.setColor(mOneStarStrokeColor);
fillPaint.setColor(mOneStarFillColor);
rating.setmStrokePaint(strokePaint);
rating.setmFillPaint(fillPaint);
break;

case 2:
strokePaint.setColor(mTwoStarStrokeColor);
fillPaint.setColor(mTwoStarFillColor);
rating.setmStrokePaint(strokePaint);
rating.setmFillPaint(fillPaint);
break;

case 3:
strokePaint.setColor(mThreeStarStrokeColor);
fillPaint.setColor(mThreeStarFillColor);
rating.setmStrokePaint(strokePaint);
rating.setmFillPaint(fillPaint);
break;

case 4:
strokePaint.setColor(mFourStarStrokeColor);
fillPaint.setColor(mFourStarFillColor);
rating.setmStrokePaint(strokePaint);
rating.setmFillPaint(fillPaint);
break;

case 5:
strokePaint.setColor(mFiveStarStrokeColor);
fillPaint.setColor(mFiveStarFillColor);
rating.setmStrokePaint(strokePaint);
rating.setmFillPaint(fillPaint);
break;
}
return rating;
}


/***
* Setting last 8 rating Sequence
* @param ratingSeq : array [5,4,2,1,2,1,1,4]
*
*/
public void setRatingSequence(int[] ratingSeq){
this.mRatingSequence = ratingSeq;
invalidate();
}
}
9 changes: 9 additions & 0 deletions ratingtrendlib/src/main/res/drawable/ic_star.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="49.94dp"
android:height="49.94dp"
android:viewportWidth="49.94"
android:viewportHeight="49.94">
<path
android:fillColor="#FF000000"
android:pathData="M48.856,22.73c0.983,-0.958 1.33,-2.364 0.906,-3.671c-0.425,-1.307 -1.532,-2.24 -2.892,-2.438l-12.092,-1.757c-0.515,-0.075 -0.96,-0.398 -1.19,-0.865L28.182,3.043c-0.607,-1.231 -1.839,-1.996 -3.212,-1.996c-1.372,0 -2.604,0.765 -3.211,1.996L16.352,14c-0.23,0.467 -0.676,0.79 -1.191,0.865L3.069,16.622c-1.359,0.197 -2.467,1.131 -2.892,2.438c-0.424,1.307 -0.077,2.713 0.906,3.671l8.749,8.528c0.373,0.364 0.544,0.888 0.456,1.4L8.224,44.701c-0.183,1.06 0.095,2.091 0.781,2.904c1.066,1.267 2.927,1.653 4.415,0.871l10.814,-5.686c0.452,-0.237 1.021,-0.235 1.472,0l10.815,5.686c0.526,0.277 1.087,0.417 1.666,0.417c1.057,0 2.059,-0.47 2.748,-1.288c0.687,-0.813 0.964,-1.846 0.781,-2.904l-2.065,-12.042c-0.088,-0.513 0.083,-1.036 0.456,-1.4L48.856,22.73z"/>
</vector>
Loading

0 comments on commit 6e59877

Please sign in to comment.