安卓圆心进度条CircleProgressView

来源:互联网 发布:coc飞龙升级数据 编辑:程序博客网 时间:2024/04/29 06:57


最近做了一个圆形进度条控件 顺便就把他抽出来哒直接贴出代码了  类中提供了各种颜色的设置 字体大小 已经进度的宽度 加上了进度回调(虽然感觉没啥卵用)



这是自定义的类  就这么一个类咯


package com.example.progress;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * 圆形进度条 *  * @author yung7086 *         <p> *         2015年12月9日 20:44:29 *         <p> *         ps: 圆环会自动根据宽度高度取小边的大小的比例 你也可以自己设置圆环的大小@see #scrollBy(int, int) */public class CircleProgress extends View {private int width;// 控件的宽度private int height;// 控件的高度private int radius;// 圆形的半径private int socktwidth = dp2px(8);// 圆环进度条的宽度private Paint paint = new Paint();private Rect rec = new Rect();private int value = 70;// 百分比0~100;private int textSize = dp2px(70);// 文字大小private Bitmap bitmap;@Deprecatedfloat scale = 0.15f;// 中间背景图片相对圆环的大小的比例private int preColor = Color.parseColor("#2c2200");// 进度条未完成的颜色private int progressColor = Color.parseColor("#6bb849");// 进度条颜色private float paddingscale = 0.8f;// 控件内偏距占空间本身的比例private int CircleColor = Color.parseColor("#CCCCCC");// 圆中间的背景颜色private int textColor = progressColor;// 文字颜色private onProgressListener monProgress;// 进度时间监听private int startAngle = 270;RectF rectf = new RectF();public CircleProgress(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {width = getWidth();int size = height = getHeight();if (height > width)size = width;radius = (int) (size * paddingscale / 2f);paint.setAntiAlias(true);paint.setColor(preColor);// 绘制最大的圆 进度条圆环的背景颜色(未走到的进度)就是这个哦canvas.drawCircle(width / 2, height / 2, radius, paint);rectf.set((width - radius * 2) / 2f, (height - radius * 2) / 2f,((width - radius * 2) / 2f) + (2 * radius),((height - radius * 2) / 2f) + (2 * radius));paint.setColor(progressColor);canvas.drawArc(rectf, startAngle, value * 3.6f, true, paint);paint.setColor(CircleColor);// 绘制用于遮住伞形两个边的小圆canvas.drawCircle(width / 2, height / 2, radius - socktwidth, paint);if (bitmap != null) {// 绘制中间的图片int width2 = (int) (rectf.width() * scale);int height2 = (int) (rectf.height() * scale);rectf.set(rectf.left + width2, rectf.top + height2, rectf.right- width2, rectf.bottom - height2);canvas.drawBitmap(bitmap, null, rectf, null);}String v = value + "%";paint.setColor(textColor);paint.setTextSize(textSize);paint.getTextBounds(v, 0, v.length(), rec);int textwidth = rec.width();int textheight = rec.height();// 绘制中间文字canvas.drawText(v, (width - textwidth) / 2,((height + textheight) / 2), paint);super.onDraw(canvas);}public int dp2px(int dp) {return (int) ((getResources().getDisplayMetrics().density * dp) + 0.5);}/** * 设置进度 *  * @param value *            <p> *            ps: 百分比 0~100; */public void setValue(int value) {if (value > 100)return;this.value = value;invalidate();if (monProgress != null)monProgress.onProgress(value);}/** * 设置圆环进度条的宽度 px */public CircleProgress setProdressWidth(int width) {this.socktwidth = width;return this;}/** * 设置文字大小 *  * @param value */public CircleProgress setTextSize(int value) {textSize = value;return this;}/** * 设置文字大小 *  * @param value */public CircleProgress setTextColor(int color) {this.textColor = color;return this;}/** * 设置进度条之前的颜色 *  * @param color */public CircleProgress setPreProgress(int precolor) {this.preColor = precolor;return this;}/** * 设置进度颜色 *  * @param color */public CircleProgress setProgress(int color) {this.progressColor = color;return this;}/** * 设置圆心中间的背景颜色 *  * @param color * @return */public CircleProgress setCircleBackgroud(int color) {this.CircleColor = color;return this;}/** * 设置圆相对整个控件的宽度或者高度的占用比例 *  * @param scale */public CircleProgress setPaddingscale(float scale) {this.paddingscale = scale;return this;}/** * 设置开始的位置 *  * @param startAngle *            0~360 *            <p> *            ps 0代表在最右边 90 最下方 按照然后顺时针旋转 */public CircleProgress setStartAngle(int startAngle) {this.startAngle = startAngle;return this;}public interface onProgressListener {void onProgress(int value);}}


下面是简单的调用类了啊

package com.example.progress;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;/** * 测试类 *  * @author yung7086 2015年12月9日 21:51:49 */public class MainActivity extends Activity {private int preColor = Color.parseColor("#2c2200");private int progressColor = Color.parseColor("#6bb849");private int CircleColor = Color.parseColor("#CCCCCC");private int textColor = Color.parseColor("#9bb879");;private CircleProgress pv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pv = (CircleProgress) findViewById(R.id.progressview1);pv.setTextColor(textColor).setCircleBackgroud(CircleColor).setPreProgress(progressColor).setProgress(preColor).setProdressWidth(50).setPaddingscale(0.8f);han.sendEmptyMessageDelayed(1, 100);}Handler han = new Handler() {public void handleMessage(android.os.Message msg) {pv.setValue(msg.what);han.sendEmptyMessageDelayed(msg.what + 1, 100);};};}
布局文件很明显没啥
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.progress.MainActivity" >    <com.example.progress.CircleProgress        android:id="@+id/progressview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#ffffff"        android:text="@string/hello_world" /></RelativeLayout>


草草的写了下 ..尊重作者 yung7086

 

PS:不贴出源码的博主勃不起  

http://download.csdn.net/detail/yung7086/9342027


1 0