]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/CircleProgressBar.java
e9772e68b5ecb642c8f91f1d19d9d2e541301027
[~andy/freeotp] / src / org / fedorahosted / freeotp / CircleProgressBar.java
1 /*
2  * FreeOTP
3  *
4  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
5  *
6  * Copyright (C) 2013  Nathaniel McCallum, Red Hat
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 package org.fedorahosted.freeotp;
22
23 import android.content.Context;
24 import android.graphics.Canvas;
25 import android.graphics.Paint;
26 import android.graphics.Paint.Style;
27 import android.graphics.Rect;
28 import android.graphics.RectF;
29 import android.util.AttributeSet;
30 import android.widget.ProgressBar;
31
32 public class CircleProgressBar extends ProgressBar {
33         private Paint paint;
34         private RectF rectf;
35         private Rect rect;
36
37         public CircleProgressBar(Context context, AttributeSet attrs, int defStyle) {
38                 super(context, attrs, defStyle);
39                 setup();
40         }
41
42         public CircleProgressBar(Context context, AttributeSet attrs) {
43                 super(context, attrs);
44                 setup();
45         }
46
47         public CircleProgressBar(Context context) {
48                 super(context);
49                 setup();
50         }
51
52         private void setup() {
53                 paint = new Paint();
54         rectf = new RectF();
55         rect = new Rect();
56
57         paint.setColor(0x33333300);
58         paint.setAlpha(0x99);
59         paint.setAntiAlias(true);
60         paint.setStyle(Style.FILL_AND_STROKE);
61         }
62
63         @Override
64         protected synchronized void onDraw(Canvas canvas) {
65                 getDrawingRect(rect);
66                 rect.left += getPaddingLeft() + 2;
67                 rect.top += getPaddingTop() + 2;
68                 rect.right -= getPaddingRight() + 2;
69                 rect.bottom -= getPaddingBottom() + 2;
70
71                 rectf.set(rect);
72                 canvas.drawArc(rectf, -90, getProgress() * 360 / getMax(), true, paint);
73         }
74 }