效果如下:

贴出代码:
注意:对于自定义View,必须要有两个参数的构造函数
MyProgressBar.java
-
package cn.com.xiebiao.circleprogress;
-
-
import android.content.Context;
-
import android.content.res.TypedArray;
-
import android.graphics.Canvas;
-
import android.graphics.Color;
-
import android.graphics.Paint;
-
import android.graphics.RectF;
-
import android.util.AttributeSet;
-
import android.util.Log;
-
import android.view.View;
-
-
import java.text.DecimalFormat;
-
-
-
/**
-
* Created by vibexie on 4/2/15.
-
*/
-
public class MyProgressBar extends View {
-
/**
-
*内圈的颜色
-
*/
-
private int innerColor;
-
/**
-
*外圈的颜色
-
*/
-
private int outerColor;
-
/**
-
*内圆的半径
-
*/
-
private int circleRadius;
-
/**
-
* 圆环的宽度
-
*/
-
private int ringWidth;
-
/**
-
*速度
-
*/
-
private int speed;
-
/**
-
*当前进度
-
*/
-
private int currentProgress;
-
/*
-
*环的画笔
-
*/
-
private Paint circlePaint;
-
-
/**
-
* 进度画笔
-
*/
-
private Paint progressPaint;
-
-
/**
-
* 进度文本字体大小
-
*/
-
private int progressTextSize;
-
-
public MyProgressBar(Context context) {
-
this(context, null);
-
}
-
-
public MyProgressBar(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
-
/**
-
*获取自定义属性数组
-
*/
-
TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyProgressBar);
-
int attrCount=typedArray.getIndexCount();
-
-
/**
-
*提取自定义属性的值
-
*/
-
for(int i=0;i<attrCount;i++){
-
int attr=typedArray.getIndex(i);
-
switch (attr){
-
case R.styleable.MyProgressBar_innerColor:
-
//getColor的第二个参数是在第一个参数为空的时候设置的值
-
innerColor=typedArray.getColor(R.styleable.MyProgressBar_innerColor,Color.GREEN);
-
break;
-
case R.styleable.MyProgressBar_outerColor:
-
outerColor=typedArray.getColor(R.styleable.MyProgressBar_outerColor,Color.RED);
-
break;
-
case R.styleable.MyProgressBar_innerCircleRadius:
-
circleRadius=typedArray.getDimensionPixelOffset(R.styleable.MyProgressBar_innerCircleRadius,100);
-
break;
-
case R.styleable.MyProgressBar_ringWidth:
-
ringWidth=typedArray.getDimensionPixelOffset(R.styleable.MyProgressBar_ringWidth, 10);
-
break;
-
case R.styleable.MyProgressBar_progressTextSize:
-
progressTextSize=typedArray.getDimensionPixelSize(R.styleable.MyProgressBar_progressTextSize,25);
-
break;
-
case R.styleable.MyProgressBar_speed:
-
speed=typedArray.getInt(R.styleable.MyProgressBar_speed,20);
-
break;
-
}
-
}
-
/**
-
* Recycle the TypedArray, to be re-used by a later caller. After calling
-
* this function you must not ever touch the typed array again.
-
*/
-
typedArray.recycle();
-
-
/**
-
* new出画笔
-
*/
-
circlePaint=new Paint();
-
progressPaint=new Paint();
-
/**
-
* 绘图线程
-
*/
-
-
new Thread()
-
{
-
public void run()
-
{
-
while (true)
-
{
-
currentProgress++;
-
if (currentProgress == 360)
-
{
-
currentProgress = 0;
-
}
-
postInvalidate();
-
try
-
{
-
Thread.sleep(speed);
-
} catch (InterruptedException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
};
-
}.start();
-
}
-
-
-
@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
/**
-
* 获取圆心的x坐标
-
*/
-
int x=getWidth()/2;
-
/**
-
* 获取圆心的Y坐标
-
*/
-
int y=getHeight()/2;
-
-
/**
-
*设置环画笔的属性
-
*/
-
circlePaint.setStyle(Paint.Style.STROKE);
-
circlePaint.setStrokeWidth(ringWidth);
-
circlePaint.setAntiAlias(true);
-
-
/**
-
* 设置进度画笔的属性
-
*/
-
progressPaint.setStyle(Paint.Style.FILL);
-
progressPaint.setTextSize(progressTextSize);
-
progressPaint.setAntiAlias(true);
-
/**
-
* 进度文本的X坐标
-
*/
-
int progressX=(int)(x-(progressTextSize*3/4));
-
int progressY=y+(progressTextSize/2);
-
/**
-
* 用于定义的圆弧的形状和大小的界限
-
*/
-
RectF oval = new RectF(x-circleRadius, y-circleRadius, x+circleRadius, y+circleRadius);
-
/**
-
* 底层的圆圈完整,上层的圆圈跑
-
*/
-
circlePaint.setColor(innerColor);
-
canvas.drawCircle(x, y, circleRadius, circlePaint);
-
circlePaint.setColor(outerColor);
-
-
DecimalFormat decimalFormat=new DecimalFormat("#.0");
-
canvas.drawText("" + decimalFormat.format(currentProgress/3.6), progressX, progressY, progressPaint);
-
canvas.drawArc(oval, -90, currentProgress, false, circlePaint);
-
}
- }
/value/attr.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<declare-styleable name="MyProgressBar">
-
<attr name="innerColor" format="color"/>
-
<attr name="outerColor" format="color"/>
-
<!--内圆的半径-->
-
<attr name="innerCircleRadius" format="dimension"/>
-
<!--圆环的宽度-->
-
<attr name="ringWidth" format="dimension"/>
-
<!--进度文本字体大小-->
-
<attr name="progressTextSize" format="dimension"/>
-
<attr name="speed" format="integer"/>
-
</declare-styleable>
- </resources>
MainActivity.java
-
package cn.com.xiebiao.circleprogress;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
-
-
public class MainActivity extends Activity {
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
}
-
- }
AndroidMainfest.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="cn.com.xiebiao.circleprogress" >
-
-
<application
-
android:allowBackup="true"
-
android:icon="@mipmap/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity
-
android:name="cn.com.xiebiao.circleprogress.MainActivity"
-
android:label="@string/app_name" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
</application>
-
- </manifest>
注意,在android Studio上开发安卓,对于自定义View的命名空间,只要""即可。
activity_main.xml
-
<RelativeLayout xmlns:android=""
-
xmlns:tools=""
-
xmlns:myProgressBar=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent">
-
-
<cn.com.xiebiao.circleprogress.MyProgressBar
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
myProgressBar:innerColor="#ff1f8a3c"
-
myProgressBar:outerColor="#ffa60a0e"
-
myProgressBar:ringWidth="30dp"
-
myProgressBar:innerCircleRadius="100dp"
-
myProgressBar:progressTextSize="50dp"
-
myProgressBar:speed="20"
-
/>
-
- </RelativeLayout>