Sunday, January 10, 2016

android arc progress bar, there are detailed notes!




            


android arc progress bar, there are detailed notes, relatively simple, interested friends can come and see.


 
package com.demo.eric.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/ **
* Arc the progress bar
*
*author Eric
*
* /
public class ArcProgressbar extends View {

public ArcProgressbar (Context context) {
super (context);
}

public ArcProgressbar (Context context, AttributeSet attrs) {
super (context, attrs);
}

Override
protected void onDraw (Canvas canvas) {
super.onDraw (canvas);
init (canvas);
}

private void init (Canvas canvas) {
// Draw curved matrix area.
rectBg = new RectF (15, 15, diameter, diameter);

// Calculate the arc center and radius.
int cx1 = (diameter + 15) / 2;
int cy1 = (diameter + 15) / 2;
int arcRadius = (diameter - 15) / 2;
// ProgressBar end and began to draw two circles, rounded achieve ProgressBar.
mPaintCircle = new Paint ();
mPaintCircle.setAntiAlias (true);
mPaintCircle.setColor (bgColor);

canvas.drawCircle (
(Float) (cx1 + arcRadius * Math.cos (startAngle * 3.14 / 180)),
(Float) (cy1 + arcRadius * Math.sin (startAngle * 3.14 / 180)),
bgStrokeWidth / 2, mPaintCircle); // small circle

canvas.drawCircle (
(Float) (cx1 + arcRadius
* Math.cos ((180 - startAngle) * 3.14 / 180)),
(Float) (cy1 + arcRadius
* Math.sin ((180 - startAngle) * 3.14 / 180)),
bgStrokeWidth / 2, mPaintCircle); // small circle

// Arc background.
mPaintBg = new Paint ();
mPaintBg.setAntiAlias (true);
mPaintBg.setStyle (Style.STROKE);
mPaintBg.setStrokeWidth (bgStrokeWidth);
mPaintBg.setColor (bgColor);
canvas.drawArc (rectBg, startAngle, endAngle, false, mPaintBg);

// Arc little background.
if (showSmallBg) {
mPaintSmallBg = new Paint ();
mPaintSmallBg.setAntiAlias (true);
mPaintSmallBg.setStyle (Style.STROKE);
mPaintSmallBg.setStrokeWidth (barStrokeWidth);
mPaintSmallBg.setColor (smallBgColor);
canvas.drawArc (rectBg, startAngle, endAngle, false, mPaintSmallBg);
}

// Arc ProgressBar.
mPaintBar = new Paint ();
mPaintBar.setAntiAlias (true);
mPaintBar.setStyle (Style.STROKE);
mPaintBar.setStrokeWidth (barStrokeWidth);
mPaintBar.setColor (barColor);
canvas.drawArc (rectBg, startAngle, progress, false, mPaintBar);

// Move the circle with the ProgressBar.
if (showMoveCircle) {
mPaintCircle.setColor (barColor);
canvas.drawCircle (
(Float) (cx1 + arcRadius
* Math.cos (angleOfMoveCircle * 3.14 / 180)),
(Float) (cy1 + arcRadius
* Math.sin (angleOfMoveCircle * 3.14 / 180)),
bgStrokeWidth / 2, mPaintCircle); // small circle
}

invalidate ();
}

/ **
*
*param Progress
* /
public void addProgress (int _progress) {
progress + = _progress;
angleOfMoveCircle + = _progress;
System.out.println (progress);
if (progress & gt; endAngle) {
progress = 0;
angleOfMoveCircle = startAngle;
}
invalidate ();
}

/ **
* Set pen width curved background.
* /
public void setBgStrokeWidth (int bgStrokeWidth) {
this.bgStrokeWidth = bgStrokeWidth;
}

/ **
* Set the pen width ProgressBar arc.
* /
public void setBarStrokeWidth (int barStrokeWidth) {
this.barStrokeWidth = barStrokeWidth;
}

/ **
* Set the background color of the arc.
* /
public void setBgColor (int bgColor) {
this.bgColor = bgColor;
}

/ **
* Set arc ProgressBar color.
* /
public void setBarColor (int barColor) {
this.barColor = barColor;
}

/ **
* Set the background color of a small arc.
* /
public void setSmallBgColor (int smallBgColor) {
this.smallBgColor = smallBgColor;
}

/ **
* Set the arc diameter.
* /
public void setDiameter (int diameter) {
this.diameter = diameter;
}

/ **
* Whether to display a small background.
* /
public void setShowSmallBg (boolean showSmallBg) {
this.showSmallBg = showSmallBg;
}

/ **
* Whether to move small circle.
* /
public void setShowMoveCircle (boolean showMoveCircle) {
this.showMoveCircle = showMoveCircle;
}

private int bgStrokeWidth = 44;
private int barStrokeWidth = 15;
private int bgColor = Color.GRAY;
private int barColor = Color.RED;
private int smallBgColor = Color.WHITE;
private int progress = 0;
private int angleOfMoveCircle = 140; // starting angle moving small garden.
private int startAngle = 140;
private int endAngle = 260;
private Paint mPaintBar = null;
private Paint mPaintSmallBg = null;
private Paint mPaintBg = null;
private Paint mPaintCircle = null;
private RectF rectBg = null;
/ **
* diameter.
* /
private int diameter = 450;

private boolean showSmallBg = true; // whether to show little background.
private boolean showMoveCircle = true; // whether to move small garden.

}


Reply:
Not bad, to support a
Reply:
This should be an improved version of the original post, right? !
Reply:
Code looks very nice little support what
Reply:
quote the second floor doveqian reply:
This should be an improved version of the original post, right? !

Improved version? Oh no, this is to write
Reply:
Support one study.
Reply:
The writing is really good
Reply:
The reply to be removed administrator 2013-04-23 09:21:12

Reply:
How to achieve the drag effect, similar to the thumb seekbar
Reply:
quote 4th floor digua_eric reply:
Quote: quote, 2nd Floor doveqian reply:

This should be an improved version of the original post, right? !

Improved version? Oh no, this is to write


Why do not you set the red. . Where green and blue are set? ?
Reply:
You have the source, find a source
Reply:
Landlord much love, too useful!
Reply:
Useful source, collection

No comments:

Post a Comment