自定义ViewProgress

转载http://blog.youkuaiyun.com/lmj623565791/article/details/24500107

首先在values文件下穿件一个attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <attr name="firstColor" format="color"/>
    <attr name="secondColor" format="color"/>
    <attr name="circleWidth" format="dimension"/>
    <attr name="speed" format="integer"/>

    <declare-styleable name="CustomProgressBar" >

        <attr name="firstColor" />
        <attr name="secondColor" />
        <attr name="circleWidth" />
        <attr name="speed" />

    </declare-styleable>

</resources>
自定义一个CustomView
public class CustomProgressBar extends View {


    private int mCircleWidth;
    private int mFirstCoclor;
    private int mSecondColor;
    private int mSpeed;
    private Paint mPaint;
    int mProgress;
    boolean isNext;
    public CustomProgressBar(Context context) {
        this(context,null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs, defStyleAttr);
    }


    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomProgressBar,defStyleAttr,0);
        int indexCount = typedArray.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr){
                case R.styleable.CustomProgressBar_circleWidth:

                    mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));

                    break;
                case R.styleable.CustomProgressBar_firstColor:
                    mFirstCoclor = typedArray.getColor(attr, Color.GREEN);
                    break;
                case R.styleable.CustomProgressBar_secondColor:
                    mSecondColor = typedArray.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomProgressBar_speed:
                    mSpeed = typedArray.getInt(attr, 20);
                    break;
            }
        }
        typedArray.recycle();;
        mPaint = new Paint();
        new Thread(){
            @Override
            public void run() {
                while (true){

                    mProgress++;
                    if(mProgress == 360){
                        mProgress = 0;

                        if(!isNext){
                            isNext = true;
                        }else {
                            isNext = false;
                        }

                    }

                    postInvalidate();
                    try{
                        Thread.sleep(mSpeed);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }



    @Override
    protected void onDraw(Canvas canvas) {
        int center = getWidth()/2;
        int radius = center-mCircleWidth/2;
        mPaint.setStrokeWidth(mCircleWidth);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);

        RectF oval = new RectF(center-radius,center-radius,center+radius,center+radius);

        if(!isNext){
            mPaint.setColor(mFirstCoclor);
            canvas.drawCircle(center,center,radius,mPaint);
            mPaint.setColor(mSecondColor);
            canvas.drawArc(oval,-90,mProgress,false,mPaint);
        }else {
            mPaint.setColor(mSecondColor);
            canvas.drawCircle(center,center,radius,mPaint);
            mPaint.setColor(mFirstCoclor);
            canvas.drawArc(oval,-90,mProgress,false,mPaint);
        }

    }
}
在布局中展示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<com.example.administrator.testapplication.CustomProgressBar
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerInParent="true"
    custom:circleWidth="10dp"
    custom:firstColor="#D4F668"
    custom:secondColor="#2F9DD2"
    custom:speed="2"
    >
</com.example.administrator.testapplication.CustomProgressBar>


</RelativeLayout>

运行结果:这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值