安卓圆心进度条CircleProgressView


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



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


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;
	@Deprecated
	float 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);
	}

	@Override
	protected 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;

	@Override
	protected 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


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 提供了一个 `ProgressBar` 控件,可以用来显示进度条。如果要实现半圆环形进度条,可以通过自定义控件来实现。 以下是一个简单的实现方法: 1. 创建一个自定义控件类 `CircularProgressBar`,继承自 `View` 类。 2. 在 `CircularProgressBar` 类中添加一些属性,如进度条的颜色、宽度等。 3. 重写 `onDraw` 方法,绘制半圆环进度条。 具体实现步骤如下: 1. 在 `CircularProgressBar` 类中添加属性: ```java private int mProgress = 0; // 当前进度 private int mMax = 100; // 最大进度 private int mProgressColor = Color.BLUE; // 进度条颜色 private int mBackgroundColor = Color.GRAY; // 背景颜色 private float mStrokeWidth = 10f; // 进度条宽度 ``` 2. 在 `onDraw` 方法中绘制半圆环进度条: ```java @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 获取控件宽度和高度 int width = getWidth(); int height = getHeight(); // 计算圆心坐标和半径 int centerX = width / 2; int centerY = height / 2; int radius = Math.min(centerX, centerY) - (int) (mStrokeWidth / 2); // 绘制背景圆环 Paint backgroundPaint = new Paint(); backgroundPaint.setColor(mBackgroundColor); backgroundPaint.setStyle(Paint.Style.STROKE); backgroundPaint.setStrokeWidth(mStrokeWidth); canvas.drawCircle(centerX, centerY, radius, backgroundPaint); // 绘制进度圆环 Paint progressPaint = new Paint(); progressPaint.setColor(mProgressColor); progressPaint.setStyle(Paint.Style.STROKE); progressPaint.setStrokeWidth(mStrokeWidth); progressPaint.setStrokeCap(Paint.Cap.ROUND); // 设置为圆角形状 float sweepAngle = ((float) mProgress / mMax) * 180f; // 计算当前进度所占的角度 canvas.drawArc(new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius), -90f, sweepAngle, false, progressPaint); } ``` 3. 在 `CircularProgressBar` 类中添加方法,用于设置进度: ```java public void setProgress(int progress) { mProgress = progress; invalidate(); // 通知控件重绘 } ``` 这样就完成了一个简单的半圆环进度条控件的实现。在使用时,可以通过调用 `setProgress` 方法来更新进度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值