-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathReviewBarView.java
122 lines (83 loc) · 3.66 KB
/
ReviewBarView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.example.customviewdemoapplication.Views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.example.customviewdemoapplication.R;
public class ReviewBarView extends View {
final static int LEFT_GAP = 350;
static float RIGHT_GAP = 100;
final static int RECT_HEIGHT = 20;
static int RECT_WIDTH = 600;
float mTextLeftGap;
String mStringStart, mStringEnd;
int mBgColor, mFgColor, mStringColor;
float mPercentage;
Paint mPaintBar;
Paint mPaintProgress;
Paint mPaintText;
Rect mRectBar;
Rect mRectProgress;
public ReviewBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(AttributeSet set){
TypedArray typedArray = getContext().obtainStyledAttributes(set, R.styleable.ReviewBarView);
mBgColor = typedArray.getInteger(R.styleable.ReviewBarView_bgColor, R.color.grey);
mFgColor = typedArray.getInteger(R.styleable.ReviewBarView_fgColor, R.color.colorPrimary);
mPercentage = typedArray.getFloat(R.styleable.ReviewBarView_percentage, 50f);
mTextLeftGap = typedArray.getDimension(R.styleable.ReviewBarView_margin_left, LEFT_GAP/4);
RIGHT_GAP = typedArray.getDimension(R.styleable.ReviewBarView_margin_right, 100);
mStringStart = typedArray.getString(R.styleable.ReviewBarView_string_start);
mStringEnd = typedArray.getString(R.styleable.ReviewBarView_string_end);
mStringColor = typedArray.getColor(R.styleable.ReviewBarView_string_color, Color.BLACK);
RECT_WIDTH = (int) typedArray.getDimension(R.styleable.ReviewBarView_bar_width, 600);
typedArray.recycle();
mRectBar = new Rect();
mRectProgress = new Rect();
mPaintBar = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintProgress = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBar.setColor(mBgColor);
mPaintProgress.setColor(mFgColor);
mPaintText.setTextSize(30);
mPaintText.setColor(mStringColor);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(widthMeasureSpec == MeasureSpec.AT_MOST){
setMeasuredDimension(getWidth(), getSuggestedMinimumHeight());
}
}
@Override
protected void onDraw(Canvas canvas) {
mRectBar.top = getHeight()/2 - RECT_HEIGHT/2;
mRectBar.left = getWidth() - RECT_WIDTH - (int)RIGHT_GAP;
mRectBar.bottom = mRectBar.top + RECT_HEIGHT;
mRectBar.right = mRectBar.left + RECT_WIDTH;
mRectProgress.top = mRectBar.top;
mRectProgress.left = mRectBar.left;
mRectProgress.bottom = mRectBar.bottom;
mRectProgress.right = mRectProgress.left + getProgressWidth();
canvas.drawRect(mRectBar, mPaintBar);
canvas.drawRect(mRectProgress, mPaintProgress);
canvas.drawText(mStringStart, mTextLeftGap, getHeight()/2 + RECT_HEIGHT/2, mPaintText);
canvas.drawText(mStringEnd, mRectBar.right + 10, getHeight()/2 + RECT_HEIGHT/2, mPaintText);
}
private int getProgressWidth(){
int progressWidth;
if (mPercentage<=100 && mPercentage>=0){
progressWidth = (int)(mRectBar.width()*mPercentage/100);
}else{
progressWidth = 0;
}
return progressWidth;
}
}