自定义View实现圆形进度条
来源:互联网 发布:vb.net picturebox 编辑:程序博客网 时间:2024/04/29 09:46
自定义实现圆形进度条,首先你需要在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>
这是attrs的属性对应,format的值有string,integer,color.dimension,boolean等等。
然后在自定义的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();//为了保持以后使用该属性一致性}@Overrideprotected 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型,不然全为0float 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;}}
这里需要注意的是进度条的实现,在setProgress(int progress)方法中是进行互斥使用的,因为在布局文件中添加的不止一个圆形进度条,所以需要互斥使用。接下来给出布局文件:
<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>
在布局文件中xmlns:android_custom="http://schemas.android.com/apk/res/com.example.cirprogresstdw"这个是自己定义空间,记得必须添加,...res/+清单文件下你的包名
实现自定义空间后,就可以在下面使用这个名字设置相应的属性。
这里给出了五个圆形进度条。
接下里给出主功能代码:
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(){@Overridepublic 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()));};};@Overrideprotected 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{@Overridepublic void onClick(View v) {new Thread(){@Overridepublic 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 blocke.printStackTrace();}}};}.start();;}}}
代码已经全部贴出。
完整代码:http://download.csdn.net/detail/tan313/8598753
0 0
- 自定义View实现圆形进度条
- 自定义View实现圆形进度条
- 自定义view实现圆形进度条
- 自定义View实现Android圆形进度条
- 自定义View实现圆形水波进度条
- 自定义view实现录音功能,圆形进度条
- 简单实现自定义View控件圆形进度条
- 自定义View圆形进度条
- 自定义view圆形进度条。
- 自定义View圆形进度条
- 自定义view圆形进度条
- 自定义view进度条圆形
- 自定义view 圆形进度条
- 自定义View 圆形进度条
- 自定义view 圆形进度条
- 自定义View实现圆形进度条及圆形Loading
- 自定义View画圆形进度条
- android 圆形进度条 自定义view
- javax.servlet.http.HTTPSession类方法
- EXIF格式定义
- php ignore_user_abort()实现计划任务功能,实现文件能够在浏览器关掉之后照样运行
- 第六周 【项目4-成员函数、友元函数和一般函数有区别】
- hdoj 1503 Advanced Fruits 【最长公共子序列 变形】
- 自定义View实现圆形进度条
- 3. PCRE 兼容正则表达式
- 李红霞作业52页21题
- Model/View 教程
- P53 第40题
- B-tree算法查询效率分析
- 示波器的使用总结
- pthread_key_t和pthread_key_create()详解
- [ACM]线性表操作