Saturday, January 25, 2014

android arc progress bar, there are detailed notes!


            
android arc progress bar, detailed notes, relatively simple, and interested friends can look at.


 
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;

/ **
* Curved 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) {
/ / Matrix area painted curved.
rectBg = new RectF (15, 15, diameter, diameter);

/ / Calculate the center and radius of the arc.
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) ;/ / Magi

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) ;/ / Magi

/ / Curved 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);

/ / With ProgressBar moving circle.
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) ;/ / Magi
}

invalidate ();
}

/ **
*
* @ Param progress
* /
public void addProgress (int _progress) {
progress + = _progress;
angleOfMoveCircle + = _progress;
System.out.println (progress);
if (progress> endAngle) {
progress = 0;
angleOfMoveCircle = startAngle;
}
invalidate ();
}

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

/ **
* Set ProgressBar curved brush width.
* /
public void setBarStrokeWidth (int barStrokeWidth) {
this.barStrokeWidth = barStrokeWidth;
}

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

/ **
* Set the 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 show a small background.
* /
public void setShowSmallBg (boolean showSmallBg) {
this.showSmallBg = showSmallBg;
}

/ **
* Whether to show the movement of the 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 = starting angle of 140 ;/ / move a small park.
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 a small background.
private boolean showMoveCircle = true ;/ / whether to show the movement of a small park.

}


Reply:
Yes Yes, supports a
Reply:
This should be an improved version of the original post, right? !
Reply:
Code looks very good support what little
Reply:
reference to 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 was deleted administrator at 2013-04-23 09:21:12

Reply:
How to achieve drag effect, similar to the seekbar's thumb
Reply:
references, 4th Floor digua_eric reply:
Quote: references to the second 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 is the green and blue set? ?

1 comment:

  1. Do you have the complete source code on github or similar? Thanks!

    ReplyDelete