自定义progressbar

本文教你如何定制属于自己的progressbar,效果图如下所视


代码如下:

//注意自定义View的宽和高如果指定了具体的宽高那么宽高就是指定的那个值,如果没用指定具体的宽或者高即用的wrap_content那么自定义View的宽高即为该父容器的大小
//注意获取View的宽和高:getMeasuredWidth()在setMearsuredDemision()中执行onMeasured()会调用该方法
//getWidth()在onLayout中执行而在执行onLayout之后onDraw()才会执行所以可以在onDraw中通过getWidth()获取view的宽和高

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CostomProgressbar extends View {

	private int firstColor=Color.parseColor("#00FFFF"),secondColor=Color.parseColor("#E0E0E0");
	private int speed=90;
	private int progress=0;
	private boolean isChange=false;
	private int width=20;
	
	public CostomProgressbar(Context context) {
		this(context,null);
			
	}
	
	public CostomProgressbar(Context context, AttributeSet attrs) {
		super(context, attrs);
		new Thread(){
			public void run() {
				while(true){
					
					progress++;
					if(progress>360){
						progress=0;
						isChange=!isChange;
					}
					postInvalidate();
					
					try {
						Thread.sleep(100-speed);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(!isChange){
			drawArc(firstColor,360,canvas);
			drawArc(secondColor, progress,canvas);
		}else {
			drawArc(secondColor, 360,canvas);
			drawArc(firstColor, progress,canvas);
		}
		
	}
	
	private void drawArc(int color,int sweepAngle,Canvas canvas){
		Paint paint=new Paint();
		paint.setAntiAlias(true);
		paint.setColor(color);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(width);
		int cx=getMeasuredWidth()/2;
		int cy=getMeasuredHeight()/2;
		int radius=Math.min(getMeasuredHeight(), getMeasuredWidth())/2-width/2;
		RectF rectf=new RectF(cx-radius, cy-radius, cx+radius, cy+radius);
		canvas.drawArc(rectf, -90, sweepAngle, false, paint);
	}
	
	public void setColor(int firstColor,int secondColor){
		this.firstColor=firstColor;
		this.secondColor=secondColor;
	}
	
	public void setWitdh(int width){
		this.width=width;
	}
	
	public void setSpeed(int seepd){
		this.speed=speed;
	}
	
}
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center_vertical">

<com.solo.costomprograssbar.view.CostomProgressbar
    android:id="@+id/id_cp" 
    android:layout_width="300dp"
    android:layout_height="200dp"/>
   

</LinearLayout>
import com.solo.costomprograssbar.view.CostomProgressbar;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        CostomProgressbar cp=new CostomProgressbar(this);
        cp.setColor(Color.parseColor("#FFD306"), Color.parseColor("#FF2D2D"));
        cp.setWitdh(15);
<span style="white-space:pre">	</span>cp.setSpeed(20);//这里没有起作用,难道是线程的缘故
        addContentView(cp,new LayoutParams(100, 100)) ;
    }
    
    
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值