自定义ProgressBar

在项目中有时候难免会用到自定义的加载圈,下面是一个简单的自定义加载圈


下面我们来看一下怎么实现的

1.在res/values下面创建一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="oneColor" format="color"/>
    <attr name="twoColor" format="color"/>
    <attr name="circleWidth" format="dimension"/>
    <attr name="speed" format="integer"/>
    
    <declare-styleable name="CustomProgress">  
        <attr name="oneColor" />  
        <attr name="twoColor" />  
        <attr name="circleWidth" />  
        <attr name="speed" />  
    </declare-styleable>  
</resources>

2.创建一个类继承View,重写相应的方法

public class MyCustomProgress extends View{


	private int myOneColor;
	private int myTwoColor;
	private int myCircleWidth;
	private int mySpeed;
	private int myProgress = 0;
	
	private boolean isStart = false;  //是否进行下一个
	private Paint myPaint;


	public MyCustomProgress(Context context) {
		this(context, null);
	}
	
	public MyCustomProgress(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public MyCustomProgress(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgress, defStyleAttr, 0);
		int indexCount = ta.getIndexCount();
		for (int i = 0; i < indexCount; i++) {
			int index = ta.getIndex(i);
			switch (index) {
			case R.styleable.CustomProgress_oneColor:
				myOneColor = ta.getColor(index, Color.BLUE);
				break;
				
			case R.styleable.CustomProgress_twoColor:
				myTwoColor = ta.getColor(index, Color.RED);
				break;
				
			case R.styleable.CustomProgress_circleWidth:
				myCircleWidth = ta.getDimensionPixelSize(index, (int) TypedValue.applyDimension(  
                        TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
				break;


			case R.styleable.CustomProgress_speed:
				mySpeed = ta.getInt(index, 20);
				break;
			}
		}
		ta.recycle();
		myPaint = new Paint();
		
		new Thread()
				{
					public void run()
					{
						while (true)
						{
							myProgress++;
							if (myProgress == 360)
							{
								myProgress = 0;
								if (!isStart)
									isStart = true;
								else
									isStart = false;
							}
							postInvalidate();
							try
							{
								Thread.sleep(mySpeed);
							} catch (InterruptedException e)
							{
								e.printStackTrace();
							}
						}
					};
				}.start();
	}
	
	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas)
	{


		int centre = getWidth() / 2; 
		int radius = centre - myCircleWidth / 2;
		myPaint.setStrokeWidth(myCircleWidth); 
		myPaint.setAntiAlias(true); 
		myPaint.setStyle(Paint.Style.STROKE); //设置空心
		RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
		if (!isStart)
		{
			myPaint.setColor(myOneColor); 
			canvas.drawCircle(centre, centre, radius, myPaint); 
			myPaint.setColor(myTwoColor); 
			canvas.drawArc(oval, -90, myProgress, false, myPaint); 
		}else{
			myPaint.setColor(myTwoColor); 
			canvas.drawCircle(centre, centre, radius, myPaint); 
			myPaint.setColor(myOneColor);
			canvas.drawArc(oval, -90, myProgress, false, myPaint);
		}


	}
	


3.在布局中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.mycustomprogress"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.mycustomprogress.MyCustomProgress
        android:layout_width="50dp"
        android:layout_height="50dp"
        custom:circleWidth="6dp"
        custom:oneColor="#789089"
        custom:twoColor="#563685"
        android:layout_centerInParent="true"
        custom:speed="5" />

</RelativeLayout>

这样就能够完美的运行了,小弟菜鸟起步,有问题的地方希望大家指正批评



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值