Back to project page My-Wallet-Android.
The source code is released under:
GNU General Public License
If you think the Android project My-Wallet-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2011 Google Inc.//from ww w.j a v a 2 s .c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package piuk.blockchain.android.util; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.graphics.Path.Direction; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * @author Andreas Schildbach */ public class CircularProgressView extends View { private int width; private int height; private int progress = 0; private int maxProgress = 7; private final Path path = new Path(); private final Paint fillPaint = new Paint(); private final Paint strokePaint = new Paint(); public CircularProgressView(final Context context, final AttributeSet attrs) { super(context, attrs); final float density = getResources().getDisplayMetrics().density; fillPaint.setStyle(Style.FILL); fillPaint.setColor(Color.parseColor("#44ff44")); fillPaint.setAntiAlias(true); strokePaint.setStyle(Style.STROKE); strokePaint.setColor(Color.DKGRAY); strokePaint.setStrokeWidth(1 * density); strokePaint.setAntiAlias(true); } @Override protected void onDraw(final Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, fillPaint); canvas.drawPath(path, strokePaint); } public void setColor(final int color) { fillPaint.setColor(color); postInvalidate(); } public void setProgress(final int progress) { this.progress = progress; updatePath(getWidth(), getHeight()); postInvalidate(); } public void setMaxProgress(final int maxProgress) { this.maxProgress = maxProgress; updatePath(getWidth(), getHeight()); postInvalidate(); } @Override protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) { updatePath(w, h); super.onSizeChanged(w, h, oldw, oldh); } private void updatePath(final int w, final int h) { path.reset(); if (progress == 0) { path.close(); } else if (progress < maxProgress) { final float angle = progress * 360 / maxProgress; path.moveTo(w / 2f, h / 2f); path.arcTo(new RectF(1, 1, w - 1, h - 1), 270, angle); path.close(); } else { path.addCircle(w / 2f, h / 2f, (w / 2f) - 1, Direction.CW); } } @Override protected void onMeasure(final int wMeasureSpec, final int hMeasureSpec) { final int wMode = MeasureSpec.getMode(wMeasureSpec); final int wSize = MeasureSpec.getSize(wMeasureSpec); if (wMode == MeasureSpec.EXACTLY) width = wSize; else if (wMode == MeasureSpec.AT_MOST) width = Math.min(width, wSize); final int hMode = MeasureSpec.getMode(hMeasureSpec); final int hSize = MeasureSpec.getSize(hMeasureSpec); if (hMode == MeasureSpec.EXACTLY) height = hSize; else if (hMode == MeasureSpec.AT_MOST) height = Math.min(height, hSize); setMeasuredDimension(this.width, this.height); } @Override public int getBaseline() { return getMeasuredHeight() - 1; } }