Skip to content

Commit

Permalink
Some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ritwikdax committed Sep 2, 2019
1 parent d060db2 commit dc48995
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 134 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.ritwik.ratingtrendlib.Rating
<com.ritwik.ratingtrendlib.RatingTrendView
android:layout_width="30dp"
android:layout_height="30dp"/>

Expand Down
143 changes: 11 additions & 132 deletions ratingtrendlib/src/main/java/com/ritwik/ratingtrendlib/Rating.java
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;
}
}
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();
}
}
18 changes: 18 additions & 0 deletions ratingtrendlib/src/main/res/values/attrs.xml
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>
3 changes: 2 additions & 1 deletion ratingtrendlib/src/main/res/values/colors.xml
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>

0 comments on commit dc48995

Please sign in to comment.