Skip to content

Commit eb7fcc7

Browse files
committed
Added ProgressBarCircularIndeterminateRounded.java
1 parent 924775e commit eb7fcc7

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package com.gc.materialdesign.views;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.Canvas;
6+
import android.graphics.Color;
7+
import android.graphics.Paint;
8+
import android.graphics.Path;
9+
import android.graphics.Point;
10+
import android.graphics.PorterDuff;
11+
import android.graphics.PorterDuffXfermode;
12+
import android.graphics.RectF;
13+
import android.util.AttributeSet;
14+
15+
import com.gc.materialdesign.utils.Utils;
16+
17+
public class ProgressBarCircularIndeterminateRounded extends CustomView {
18+
19+
private static final String ANDROIDXML = "http://schemas.android.com/apk/res/android";
20+
21+
private static final int DEFAULT_COLOR = Color.parseColor("#1E88E5");
22+
23+
private int backgroundColor = DEFAULT_COLOR;
24+
25+
private Paint arcPaint;
26+
27+
private final Path arcPath = new Path();
28+
29+
private final int thicknessDp = 10;
30+
31+
private final int thicknessPx;
32+
33+
private final int increment = 2;
34+
35+
private final int maxArcAngle = 180;
36+
37+
private final int minArcAngle = 15;
38+
39+
private final Paint transparentPaint;
40+
41+
int arcWidth = 1;
42+
43+
int startAngle = 0;
44+
45+
float rotateAngle = 0;
46+
47+
int limit = 0;
48+
49+
public ProgressBarCircularIndeterminateRounded(Context context, AttributeSet attrs) {
50+
super(context, attrs);
51+
setAttributes(attrs);
52+
53+
transparentPaint = new Paint();
54+
transparentPaint.setAntiAlias(true);
55+
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
56+
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
57+
58+
thicknessPx = Utils.dpToPx(thicknessDp, context.getResources());
59+
}
60+
61+
// Set atributtes of XML to View
62+
protected void setAttributes(AttributeSet attrs) {
63+
64+
//Set background Color
65+
// Color by resource
66+
int backgroundColor = attrs.getAttributeResourceValue(ANDROIDXML, "background", -1);
67+
if (backgroundColor != -1) {
68+
setBackgroundColor(getResources().getColor(backgroundColor));
69+
} else {
70+
// Color by hexadecimal
71+
int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
72+
if (background != -1) {
73+
setBackgroundColor(background);
74+
}
75+
else {
76+
setBackgroundColor(DEFAULT_COLOR);
77+
}
78+
}
79+
80+
arcPaint = new Paint();
81+
arcPaint.setAntiAlias(true);
82+
arcPaint.setColor(this.backgroundColor);
83+
}
84+
85+
@Override
86+
protected void onDraw(Canvas canvas) {
87+
super.onDraw(canvas);
88+
drawSecondAnimation(canvas);
89+
invalidate();
90+
}
91+
92+
/**
93+
* Draw second animation of view
94+
*
95+
* @param canvas - the canvas to draw on.
96+
*/
97+
private void drawSecondAnimation(Canvas canvas) {
98+
rotateCanvas(canvas);
99+
100+
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
101+
Canvas temp = drawOuterCircle(bitmap);
102+
drawTransparentCircle(temp);
103+
104+
canvas.drawBitmap(bitmap, 0, 0, new Paint());
105+
}
106+
107+
private Canvas drawOuterCircle(Bitmap bitmap) {
108+
Canvas temp = new Canvas(bitmap);
109+
drawArc(temp, startAngle, arcWidth, arcPaint);
110+
return temp;
111+
}
112+
113+
private void rotateCanvas(Canvas canvas) {
114+
if (startAngle == limit) {
115+
arcWidth += increment;
116+
}
117+
if ( (arcWidth >= maxArcAngle) || (startAngle > limit) ) {
118+
startAngle += increment;
119+
arcWidth -= increment;
120+
}
121+
if (startAngle > limit + maxArcAngle - minArcAngle) {
122+
limit = startAngle;
123+
startAngle = limit;
124+
arcWidth = minArcAngle;
125+
}
126+
rotateAngle += increment;
127+
canvas.rotate(rotateAngle, getWidth() / 2, getHeight() / 2);
128+
}
129+
130+
private void drawTransparentCircle(Canvas temp) {
131+
temp.drawCircle(getWidth()/2, getHeight()/2,
132+
(getWidth()/2) - Utils.dpToPx(thicknessDp, getResources()),
133+
transparentPaint);
134+
}
135+
136+
private void drawArc(Canvas canvas, float startAngle, float sweepDegrees, Paint arcPaint) {
137+
138+
if (sweepDegrees <= 0) {
139+
return;
140+
}
141+
142+
arcPath.reset();
143+
144+
int width = getWidth();
145+
int height = getHeight();
146+
147+
int halfWidth = width/2;
148+
int halfHeight = height/2;
149+
150+
int radius = halfWidth - thicknessPx / 2;
151+
152+
arcPath.arcTo(new RectF(0, 0, width, height), startAngle, sweepDegrees);
153+
arcPath.arcTo(new RectF(thicknessPx, thicknessPx, width - thicknessPx, height - thicknessPx),
154+
startAngle + sweepDegrees, -sweepDegrees);
155+
156+
Point startPoint = calculatePointOnArc(halfWidth, halfHeight, radius, startAngle);
157+
arcPath.addCircle(startPoint.x, startPoint.y, thicknessPx / 2, Path.Direction.CW);
158+
159+
Point endPoint = calculatePointOnArc(halfWidth, halfHeight, radius, startAngle + sweepDegrees);
160+
arcPath.addCircle(endPoint.x, endPoint.y, thicknessPx / 2, Path.Direction.CW);
161+
162+
arcPath.close();
163+
canvas.drawPath(arcPath, arcPaint);
164+
}
165+
166+
// this is to calculate the end points of the arc
167+
private Point calculatePointOnArc(int centerX, int centerY, int circleRadius, float endAngle)
168+
{
169+
double endAngleRadian = endAngle * (Math.PI / 180);
170+
171+
int x = (int) Math.round((centerX + circleRadius * Math.cos(endAngleRadian)));
172+
int y = (int) Math.round((centerY + circleRadius * Math.sin(endAngleRadian)));
173+
174+
return new Point(x, y);
175+
}
176+
177+
// Set color of background
178+
public void setBackgroundColor(int color) {
179+
super.setBackgroundColor(getResources().getColor(android.R.color.transparent));
180+
if (isEnabled()) {
181+
beforeBackground = backgroundColor;
182+
}
183+
this.backgroundColor = color;
184+
}
185+
}

0 commit comments

Comments
 (0)