Android ProgressBar ImageLoading 旋转进度值

非常好用的一个 旋转进度值 ProgressBar~

1.重写View

package com.flag.imageloader.app.overwrite;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
 
public class CircleProgressBar extends View {
	//最大数值
	private int maxProgress = 100;
	//初始化数值
	private int progress = 0;
	//旋转宽度
	private int progressStrokeWidth = 4;
	//画圆所在的距形区域
	RectF oval;
	Paint paint;
	public CircleProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO 自动生成的构造函数存根
		oval = new RectF();
		paint = new Paint();
	}
 
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO 自动生成的方法存根
		super.onDraw(canvas);
		int width = this.getWidth();
		int height = this.getHeight();
 
		if(width!=height)
		{
			int min=Math.min(width, height);
			width=min;
			height=min;
		}
 
		paint.setAntiAlias(true); // 设置画笔为抗锯齿
		paint.setColor(Color.WHITE); // 设置画笔颜色
		canvas.drawColor(Color.TRANSPARENT); // 白色背景
		paint.setStrokeWidth(progressStrokeWidth); //线宽
		paint.setStyle(Style.STROKE);
 
		oval.left = progressStrokeWidth / 2; // 左上角x
		oval.top = progressStrokeWidth / 2; // 左上角y
		oval.right = width - progressStrokeWidth / 2; // 左下角x
		oval.bottom = height - progressStrokeWidth / 2; // 右下角y
 
		canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景
		paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
		canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色
 
		paint.setStrokeWidth(1);
		String text = progress + "%";
		int textHeight = height / 4;
		paint.setTextSize(textHeight);
		int textWidth = (int) paint.measureText(text, 0, text.length());
		paint.setStyle(Style.FILL);
		canvas.drawText(text, width / 2 - textWidth / 2, height / 2 +textHeight/2, paint);
		MaxGONE();
	}
 
	/**
	 * 当等于满值可以隐藏
	 */
	private void MaxGONE() {
		if(progress == 100)
			this.setVisibility(View.GONE);
	}
 
	public int getMaxProgress() {
		return maxProgress;
	}
 
	public void setMaxProgress(int maxProgress) {
		this.maxProgress = maxProgress;
	}
 
	public void setProgress(int progress) {
		this.progress = progress;
		this.invalidate();
	}
 
	/**
	 * 非UI线程调用
	 */
	public void setProgressNotInUiThread(int progress) {
		this.progress = progress;
		this.postInvalidate();
	}
}


2 .布局文件

    <com.flag.imageloader.app.overwrite.CircleProgressBar
        android:id="@+id/circleProgressbar"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true" />


3.调用方式

	private void ProgressBarStar() {
		// TODO Auto-generated method stub
		progressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar);
		new Thread() {
			public void run() {
				int i = 0;
				while (i <= 100) {
					progressBar.setProgressNotInUiThread(i);
					i++;
					try {
						sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		}.start();
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值