Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
ritwikdax committed Sep 17, 2019
1 parent 6e59877 commit fe3a64c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 157 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/ritwik/ratingtrend/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

import android.os.Bundle;

import com.ritwik.ratingtrendlib.RatingTrendView;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RatingTrendView rtv = findViewById(R.id.rtv_ratingTrend);
rtv.setRatingSequence(new int[]{1,2, 3,4,5,1,2,3});

RatingTrendView rtv1 = findViewById(R.id.rtv_ratingTrend1);
rtv1.setRatingSequence(new int[]{5,5, 1,1,3});
}
}
33 changes: 33 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,47 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="32dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Recent rating trends"
android:layout_margin="8dp"
android:textColor="@android:color/black"
android:textSize="12dp"/>



<com.ritwik.ratingtrendlib.RatingTrendView

android:padding="8dp"
android:id="@+id/rtv_ratingTrend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Maximum 8 sequence"
android:layout_margin="12dp"
android:textColor="@android:color/black"
android:textSize="12dp"/>



<com.ritwik.ratingtrendlib.RatingTrendView

android:padding="8dp"
android:id="@+id/rtv_ratingTrend1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>




</LinearLayout>
106 changes: 79 additions & 27 deletions ratingtrendlib/src/main/java/com/ritwik/ratingtrendlib/Rating.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,107 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;
import androidx.core.graphics.drawable.DrawableCompat;


public class Rating {
public static final String TAG = "Rating";

public static final float WIDTH_BY_HEIGHT_RATIO = 1.5f;
public static final int STAR_ICON_SIZE = 20;
public static final int FONT_SIZE = 30;


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

private float mStrokeWidth;
private int mStarIconSize = STAR_ICON_SIZE;

private int mStarIcon;

public Rating(int value, float cornerRadius) {

this.mValue = value;
/***
*
*
* @param cornerRadius
*
*
*/
public Rating(int starIcon, float cornerRadius, float strokeWidth, Context context) {

this.mCornerRadius = cornerRadius;
filledRect = new RectF();
strokeRect = new RectF();
this.mStrokeWidth = strokeWidth;
this.mPaint = new Paint();
mPaint.setAntiAlias(true);
this.mRect = new RectF();
this.mContext = context;
this.mStarIcon = starIcon;

}

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

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

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

public int getmValue() {
return mValue;
}

public Paint getmStrokePaint() {
return mStrokePaint;
public void setmWidth(float width){
this.mWidth = width;
this.mHeight = mWidth/WIDTH_BY_HEIGHT_RATIO;
this.mStarIconSize = (int) (width/2);
}

public Paint getmFillPaint() {
return mFillPaint;
public void setmValue(int mValue) {
this.mValue = mValue;
}

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);
/***
* Draw the individual rectangle
* @param canvas
*/

public void drawSelf(Canvas canvas){

mRect.set((0.5f * mStrokeWidth), (0.5f*mStrokeWidth),
mWidth - (0.5f * mStrokeWidth), mHeight - (0.5f* mStrokeWidth));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mFillColor);
canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mStrokeColor);
mPaint.setStrokeWidth(mStrokeWidth);
canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);

//drawing text
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(2);
mPaint.setTextSize(FONT_SIZE);
canvas.drawText(String.valueOf(mValue), (mWidth/2) - 24, (mHeight/2) +10, mPaint);

//drawing the icons
Drawable drawable = mContext.getResources().getDrawable(mStarIcon);
Drawable wrapDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrapDrawable, mStrokeColor);
wrapDrawable.setBounds((int)(mWidth/2)+5,(int)(mHeight/2) -10, (int)(mWidth/2) + 25, (int)(mHeight/2) +10 );
wrapDrawable.mutate();
wrapDrawable.draw(canvas);

Log.d(TAG, "drawSelf: Colour is" + mStrokeColor);

}
}
Loading

0 comments on commit fe3a64c

Please sign in to comment.