|
| 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 | +} |
0 commit comments