Skip to content

Commit da2227c

Browse files
author
Waheed Nazir
committed
Custom Circular View Added.
1 parent 169f699 commit da2227c

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ android {
44
compileSdkVersion 28
55
defaultConfig {
66
applicationId "wavetechstudio.androidCustomViews"
7-
minSdkVersion 16
7+
minSdkVersion 21
88
targetSdkVersion 28
99
versionCode 1
1010
versionName "1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package wavetechstudio.androidCustomViews;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Path;
7+
import android.graphics.Region;
8+
import android.util.AttributeSet;
9+
import android.view.View;
10+
11+
public class CustomCircularView extends View {
12+
private static final int DEFAULT_FG_COLOR = 0xffff0000;
13+
private static final int DEFAULT_BG_COLOR = 0xffa0a0a0;
14+
private Paint backgroundPaint;
15+
private Paint foregroundPaint;
16+
private int selectedAngle;
17+
private Path clipPath;
18+
19+
public CustomCircularView(Context context, AttributeSet attributeSet) {
20+
super(context, attributeSet);
21+
22+
backgroundPaint = new Paint();
23+
backgroundPaint.setColor(DEFAULT_BG_COLOR);
24+
backgroundPaint.setStyle(Paint.Style.FILL);
25+
26+
foregroundPaint = new Paint();
27+
foregroundPaint.setColor(DEFAULT_FG_COLOR);
28+
foregroundPaint.setStyle(Paint.Style.FILL);
29+
30+
selectedAngle = 280;
31+
}
32+
33+
@Override
34+
protected void onDraw(Canvas canvas) {
35+
int circleSize = getWidth();
36+
if (getHeight() < circleSize) circleSize = getHeight();
37+
38+
int horMargin = (getWidth() - circleSize) / 2;
39+
int verMargin = (getHeight() - circleSize) / 2;
40+
41+
// create a clipPath the first time
42+
if (clipPath == null) {
43+
int clipWidth = (int) (circleSize * 0.75);
44+
45+
int clipX = (getWidth() - clipWidth) / 2;
46+
int clipY = (getHeight() - clipWidth) / 2;
47+
clipPath = new Path();
48+
clipPath.addArc(
49+
clipX,
50+
clipY,
51+
clipX + clipWidth,
52+
clipY + clipWidth,
53+
0, 360);
54+
}
55+
56+
canvas.clipRect(0, 0, getWidth(), getHeight());
57+
canvas.clipPath(clipPath, Region.Op.DIFFERENCE);
58+
59+
canvas.save();
60+
canvas.rotate(-90, getWidth() / 2, getHeight() / 2);
61+
62+
canvas.drawArc(
63+
horMargin,
64+
verMargin,
65+
horMargin + circleSize,
66+
verMargin + circleSize,
67+
0, 360, true, backgroundPaint);
68+
69+
canvas.drawArc(
70+
horMargin,
71+
verMargin,
72+
horMargin + circleSize,
73+
verMargin + circleSize,
74+
0, selectedAngle, true, foregroundPaint);
75+
76+
canvas.restore();
77+
}
78+
79+
80+
}

app/src/main/res/layout/activity_main.xml

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
android:textColor="@color/white"
6868
android:textSize="16sp" />
6969

70+
<wavetechstudio.androidCustomViews.CustomCircularView
71+
android:layout_width="100dp"
72+
android:layout_height="100dp"
73+
android:layout_marginTop="8dp" />
74+
7075
<ScrollView
7176
android:layout_width="match_parent"
7277
android:layout_height="match_parent"

0 commit comments

Comments
 (0)