Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2010 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package com.android.browser; |
| 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.Path; |
| 24 | import android.graphics.RectF; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.widget.ImageButton; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | */ |
| 31 | public class CircularProgressView extends ImageButton { |
| 32 | |
| 33 | private static final int[] ALPHAS = { |
| 34 | 64, 96, 128, 160, 192, 192, 160, 128, 96, 64 |
| 35 | }; |
| 36 | |
| 37 | // 100 ms delay between frames, 10fps |
| 38 | private static int ALPHA_REFRESH_DELAY = 100; |
| 39 | |
| 40 | private int mEndAngle; |
| 41 | private int mProgress; |
| 42 | private Paint mPaint; |
| 43 | private int mAlpha; |
| 44 | private boolean mAnimated; |
| 45 | private RectF mRect; |
| 46 | private int mMaxProgress; |
| 47 | |
| 48 | /** |
| 49 | * @param context |
| 50 | * @param attrs |
| 51 | * @param defStyle |
| 52 | */ |
| 53 | public CircularProgressView(Context context, AttributeSet attrs, int defStyle) { |
| 54 | super(context, attrs, defStyle); |
| 55 | init(context); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param context |
| 60 | * @param attrs |
| 61 | */ |
| 62 | public CircularProgressView(Context context, AttributeSet attrs) { |
| 63 | super(context, attrs); |
| 64 | init(context); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param context |
| 69 | */ |
| 70 | public CircularProgressView(Context context) { |
| 71 | super(context); |
| 72 | init(context); |
| 73 | } |
| 74 | |
| 75 | private void init(Context ctx) { |
| 76 | mEndAngle = 0; |
| 77 | mProgress = 0; |
| 78 | mMaxProgress = 100; |
| 79 | mPaint = new Paint(); |
| 80 | mPaint.setAntiAlias(true); |
| 81 | mPaint.setColor(Color.BLACK); |
| 82 | mRect = new RectF(); |
| 83 | } |
| 84 | |
| 85 | void setMaxProgress(int max) { |
| 86 | mMaxProgress = max; |
| 87 | } |
| 88 | |
| 89 | private synchronized boolean isAnimated() { |
| 90 | return mAnimated; |
| 91 | } |
| 92 | |
| 93 | private synchronized void setAnimated(boolean animated) { |
| 94 | mAnimated = animated; |
| 95 | } |
| 96 | |
| 97 | void setProgress(int progress) { |
| 98 | mProgress = progress; |
| 99 | mEndAngle = 360 * progress / mMaxProgress; |
| 100 | invalidate(); |
| 101 | if (!isAnimated() && (progress > 0) && (progress < mMaxProgress)) { |
| 102 | setAnimated(true); |
| 103 | mAlpha = 0; |
| 104 | post(new Runnable() { |
| 105 | @Override |
| 106 | public void run() { |
| 107 | if (isAnimated()) { |
| 108 | mAlpha = (mAlpha + 1) % ALPHAS.length; |
| 109 | mPaint.setAlpha(ALPHAS[mAlpha]); |
| 110 | invalidate(); |
| 111 | postDelayed(this, ALPHA_REFRESH_DELAY); |
| 112 | } |
| 113 | } |
| 114 | }); |
| 115 | } else if ((progress <= 0) || (progress >= mMaxProgress)) { |
| 116 | setAnimated(false); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onDraw(Canvas canvas) { |
| 122 | int w = getWidth(); |
| 123 | int h = getHeight(); |
| 124 | float cx = w * 0.5f; |
| 125 | float cy = h * 0.5f; |
| 126 | mRect.set(0, 0, w, h); |
| 127 | if ((mProgress > 0) && (mProgress < mMaxProgress)) { |
| 128 | Path p = new Path(); |
| 129 | p.moveTo(cx, cy); |
| 130 | p.lineTo(cx, 0); |
| 131 | p.arcTo(mRect, 270, mEndAngle); |
| 132 | p.lineTo(cx, cy); |
| 133 | int state = canvas.save(); |
| 134 | canvas.drawPath(p, mPaint); |
| 135 | canvas.restoreToCount(state); |
| 136 | } |
| 137 | super.onDraw(canvas); |
| 138 | } |
| 139 | |
| 140 | } |