1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.permissioncontroller.permission.ui.handheld.v31; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Color; 22 import android.graphics.Paint; 23 import android.graphics.RectF; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 /** 28 * Draws a partial circular shape given some simple parameters: 29 * <li>Color via {@link #setColor(int)}</li> 30 * <li>Stroke width via {@link #setStrokeWidth(float)}</li> 31 * <li>Start angle via {@link #setStartAngle(float)}</li> 32 * <li>Sweep angle via {@link #setSweepAngle(float)}</li> 33 */ 34 public class PartialCircleView extends View { 35 36 /** Some defaults that should be obvious if not changed. */ 37 private int mColor = Color.MAGENTA; 38 private float mStrokeWidth = 2; 39 private float mStartAngle = 0; 40 private float mSweepAngle = 360; 41 PartialCircleView(Context context)42 public PartialCircleView(Context context) { 43 super(context); 44 } 45 PartialCircleView(Context context, AttributeSet attrs)46 public PartialCircleView(Context context, AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 PartialCircleView(Context context, AttributeSet attrs, int defStyleAttr)50 public PartialCircleView(Context context, AttributeSet attrs, int defStyleAttr) { 51 this(context, attrs, defStyleAttr, 0); 52 } 53 PartialCircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54 public PartialCircleView(Context context, AttributeSet attrs, 55 int defStyleAttr, int defStyleRes) { 56 super(context, attrs, defStyleAttr, defStyleRes); 57 } 58 59 /** Sets the color used to draw this partial circle. */ setColor(int color)60 public void setColor(int color) { 61 if (mColor != color) { 62 mColor = color; 63 invalidate(); 64 } 65 } 66 67 /** Sets the stroke width used to draw this partial circle. */ setStrokeWidth(float strokeWidth)68 public void setStrokeWidth(float strokeWidth) { 69 if (mStrokeWidth != strokeWidth) { 70 mStrokeWidth = strokeWidth; 71 invalidate(); 72 } 73 } 74 75 /** Sets the start angle used to draw this partial circle. */ setStartAngle(float startAngle)76 public void setStartAngle(float startAngle) { 77 if (mStartAngle != startAngle) { 78 mStartAngle = startAngle; 79 invalidate(); 80 } 81 } 82 83 /** Sets the sweep angle used to draw this partial circle. */ setSweepAngle(float sweepAngle)84 public void setSweepAngle(float sweepAngle) { 85 if (mSweepAngle != sweepAngle) { 86 mSweepAngle = sweepAngle; 87 invalidate(); 88 } 89 } 90 91 @Override onDraw(Canvas canvas)92 protected void onDraw(Canvas canvas) { 93 super.onDraw(canvas); 94 final float width = (float) getWidth(); 95 final float height = (float) getHeight(); 96 97 // We subtract half stroke width as to fit the outside edge of the circle just inside 98 // our bounds. 99 final float radius = ((width > height) ? height / 2 : width / 2) - (mStrokeWidth / 2); 100 101 Paint paint = new Paint(); 102 paint.setColor(mColor); 103 paint.setStrokeWidth(mStrokeWidth); 104 paint.setStyle(Paint.Style.FILL); 105 paint.setAntiAlias(true); 106 107 final RectF oval = new RectF(); 108 final float x = width / 2; 109 final float y = height / 2; 110 oval.set(x - radius, 111 y - radius, 112 x + radius, 113 y + radius); 114 paint.setStyle(Paint.Style.STROKE); 115 canvas.drawArc(oval, mStartAngle, mSweepAngle, false, paint); 116 } 117 } 118