自定义实现圆形进度条,首先你需要在value目录下新建attrs.xml属性文件:
<?xml version="1.0" encoding="UTF-8"?> <resources> <declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>
然后在自定义的view类中获取这些属性:
//从values/attrs.xml加载属性 TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); //获取各个属性的值 roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN); roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5); textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN); textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15); max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100); textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true); style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0); mTypedArray.recycle();//为了保持以后使用该属性一致性
要实现自定义圆形进度条,最后需要在重写onDraw()方法,在里面可以设置空心,或者实心,颜色,字体大小等等。
给出示例代码;
package com.example.cirprogresstdw; 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.graphics.Typeface; import android.util.AttributeSet; import android.view.View; public class RoundProgressBar extends View{ /** * 圆环的颜色 */ private int roundColor; /** * 圆环进度的颜色 */ private int roundProgressColor; /** * 圆环的宽度 */ private float roundWidth; /** * 中间进度百分比的字符串的颜色 */ private int textColor; /** * 中间进度百分比的字符串的字体 */ private float textSize; /** * 最大进度 */ private int max; /** * 是否显示中间的进度 */ private boolean textIsDisplayable; /** * 进度的风格,实心或者空心 */ private int style; public static final int STROKE = 0; public static final int FILL = 1; /** * 当前进度 */ private int progress; /** * 画笔对象的引用 */ private Paint paint; //**构造函数 public RoundProgressBar(Context context) { this(context, null); } public RoundProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundProgressBar(Context context, AttributeSet attrs,int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); //从values/attrs.xml加载属性 TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); //获取各个属性的值 roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN); roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5); textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN); textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15); max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100); textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true); style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0); mTypedArray.recycle();//为了保持以后使用该属性一致性 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 画最外层的圆环 */ int centre = getWidth()/2;//获得圆心的坐标 int radius = (int) (centre - roundWidth/2);//圆环的半径 paint.setColor(roundColor);//设置圆环的颜色 paint.setStyle(Paint.Style.STROKE);//设置空心 // paint.setStyle(Paint.Style.FILL);//设置实心 paint.setStrokeWidth(roundWidth);//设置圆环的宽度 paint.setAntiAlias(true);//消除锯齿 canvas.drawCircle(centre, centre, radius, paint); /** * 画进度百分比 */ paint.setStrokeWidth(0); paint.setColor(textColor); paint.setTextSize(textSize); paint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体 int percent = (int) (((float)progress/(float)max)*100);//必须先全转化为float型,不然全为0 float textWidth = paint.measureText(percent+"%");//测量字体的大小,让其在圆心处显示 if(textIsDisplayable && percent != 0 && style == STROKE){ canvas.drawText(percent+"%", centre - textWidth/2, centre + textWidth/2, paint); } /** * 画圆弧 ,画圆环的进度 */ paint.setStrokeWidth(roundWidth);//设置圆环的宽度 paint.setColor(roundProgressColor);//设置圆弧的进度颜色 RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);//画圆弧 switch (style) { case STROKE://空心 paint.setStyle(Paint.Style.STROKE); canvas.drawArc(oval, 0, 360 * progress/max, false, paint);//根据进度画圆弧 break; case FILL://实心 paint.setStyle(Paint.Style.FILL); if(progress != 0){ canvas.drawArc(oval, 0, 360 * progress/max, true, paint);//根据进度画圆弧 } break; } } public synchronized int getMax() { return max; } /** * 设置进度的最大值 * @param max */ public synchronized void setMax(int max) { if(max < 0){ throw new IllegalArgumentException("max not less than 0"); } this.max = max; } /** * 获取进度.需要同步 * @return */ public synchronized int getProgress() { return progress; } /** * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 * 刷新界面调用postInvalidate()能在非UI线程刷新 * @param progress */ public synchronized void setProgress(int progress) { if(progress < 0){ throw new IllegalArgumentException("progress not less than 0"); } if(progress > max){ progress = max; } if(progress <= max){ this.progress = progress; postInvalidate(); } } public int getCricleColor() { return roundColor; } public void setCricleColor(int cricleColor) { this.roundColor = cricleColor; } public int getCricleProgressColor() { return roundProgressColor; } public void setCricleProgressColor(int cricleProgressColor) { this.roundProgressColor = cricleProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android_custom="http://schemas.android.com/apk/res/com.example.cirprogresstdw" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" /> <com.example.cirprogresstdw.RoundProgressBar android:id="@+id/roundProgressBar1" android:layout_below="@+id/button" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentLeft="true" android:layout_marginLeft="16dp" android:layout_marginTop="60dp" /> <com.example.cirprogresstdw.RoundProgressBar android:id="@+id/roundProgressBar2" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignLeft="@+id/roundProgressBar1" android:layout_alignParentBottom="true" android:layout_marginBottom="78dp" android_custom:roundColor="#D1D1D1" android_custom:roundProgressColor="@android:color/black" android_custom:textColor="#9A32CD" android_custom:roundWidth="10dip" android_custom:textSize="18sp" /> <com.example.cirprogresstdw.RoundProgressBar android:id="@+id/roundProgressBar3" android:layout_alignParentRight="true" android:layout_alignTop="@+id/roundProgressBar1" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginRight="38dp" android_custom:style="FILL" android_custom:roundWidth="1dip" android_custom:roundProgressColor="#C2C2C2" /> <com.example.cirprogresstdw.RoundProgressBar android:id="@+id/roundProgressBar4" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignTop="@+id/roundProgressBar2" android:layout_alignLeft="@+id/roundProgressBar3" android_custom:roundColor="#C6E2FF" android_custom:roundWidth="10dip" android_custom:roundProgressColor="#CD3333" android_custom:textIsDisplayable="false" /> <com.example.cirprogresstdw.RoundProgressBar android:id="@+id/roundProgressBar5" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@+id/roundProgressBar1" android:layout_toRightOf="@+id/roundProgressBar1" android:layout_marginLeft="30dp" /> </RelativeLayout>
实现自定义空间后,就可以在下面使用这个名字设置相应的属性。
这里给出了五个圆形进度条。
接下里给出主功能代码:
package com.example.cirprogresstdw; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; private RoundProgressBar roundProgressBar1; private RoundProgressBar roundProgressBar2; private RoundProgressBar roundProgressBar3; private RoundProgressBar roundProgressBar4; private RoundProgressBar roundProgressBar5; private int progress = 0; Handler handler = new Handler(){ @Override public void handleMessage(android.os.Message msg) { roundProgressBar1.setProgress(Integer.valueOf(msg.obj.toString())); roundProgressBar2.setProgress(Integer.valueOf(msg.obj.toString())); roundProgressBar3.setProgress(Integer.valueOf(msg.obj.toString())); roundProgressBar4.setProgress(Integer.valueOf(msg.obj.toString())); roundProgressBar5.setProgress(Integer.valueOf(msg.obj.toString())); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); roundProgressBar1 = (RoundProgressBar) findViewById(R.id.roundProgressBar1); roundProgressBar2 = (RoundProgressBar) findViewById(R.id.roundProgressBar2); roundProgressBar3 = (RoundProgressBar) findViewById(R.id.roundProgressBar3); roundProgressBar4 = (RoundProgressBar) findViewById(R.id.roundProgressBar4); roundProgressBar5 = (RoundProgressBar) findViewById(R.id.roundProgressBar5); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener()); } private final class OnClickListener implements android.view.View.OnClickListener{ @Override public void onClick(View v) { new Thread(){ @Override public void run() { while(true){ progress = progress + 3; handler.sendMessage(handler.obtainMessage(10, progress)); if(progress >= 100){ break; } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start();; } } }
代码已经全部贴出。
完整代码:http://download.csdn.net/detail/tan313/8598753