自定义ViewProgress

来源:互联网 发布:淘宝信誉度怎么说 编辑:程序博客网 时间:2024/04/27 15:17

转载http://blog.csdn.net/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>

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

0 0
原创粉丝点击