自定义控件_画圆

来源:互联网 发布:淘宝页面怎么设计 编辑:程序博客网 时间:2024/05/16 05:00
public class MyProgressRound extends View{    Paint paint;    private int mProgress = 0;    private int mCountProgress = 0;    private float mRadiuSize = 0;    private float mRingSize = 0;    private float mTextSize = 0;    private int mProgressColor = 0;    public MyProgressRound(Context context) {        super(context);        init();    }    /**     * 所有在xml布局文件中 标签里面声明的属性 都可以在AttributeSet类的对象中获取出来     * @param context     * @param attrs     */    public MyProgressRound(Context context, @Nullable AttributeSet attrs) {        //在该构造方法中可以获取到  所有参数        //把参数传出去  在onDraw方法中可以操作  onMeasure中也可以操作        super(context, attrs);        getCustomAttr(context, attrs);        init();    }    private void getCustomAttr(Context context, AttributeSet attrs) {        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyProgressRound);        mRadiuSize = array.getDimension(R.styleable.MyProgressRound_radiuSize, 100);        mRingSize = array.getDimension(R.styleable.MyProgressRound_ringSize, 10);        mTextSize = array.getDimension(R.styleable.MyProgressRound_textSize, 10);        mProgressColor = array.getColor(R.styleable.MyProgressRound_progressColor, Color.BLACK);    }    public MyProgressRound(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    public void init(){        paint = new Paint();        paint.setAntiAlias(true);    }    //widthMeasureSpec/heightMeasureSpec 是一个32为的int类型    //01000000 00000000 00000000 00000000    //高两位 代表类型    //warpcontent类型 MeasureSpec.AT_MOST    //matchparent类型 MeasureSpec.EXACTLY 或者具体的长度 100dp 200dp    // 其他类型    //    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int heightMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        int width = 0;        int height = 0;        if(widthMode == MeasureSpec.AT_MOST){            width = (int) (mRadiuSize * 2);        }else{            width = Math.max(widthSize, (int) (mRadiuSize * 2));        }        if(heightMode == MeasureSpec.AT_MOST){            height = (int) (mRadiuSize * 2);        }else{            height = Math.max(heightSize, (int) (mRadiuSize * 2));        }        setMeasuredDimension(width, height);//        switch (widthMode){//            case MeasureSpec.AT_MOST://                //如果宽度使用的是warp_content那么 我们需要手动设置控件的宽值, 标准是宽是半径的2倍//                Log.i("=============widthMode", "onMeasure: " + "AT_MOST---> warp_content");//                //确定了宽高后,修改控件的宽高//                setMeasuredDimension((int)(mRadiuSize * 2), (int)(mRadiuSize * 2));//                break;//            case MeasureSpec.EXACTLY://                Log.i("=============widthMode", "onMeasure: " + "Exactly---> match_parent");//                break;//        }////        switch (heightMode){//            case MeasureSpec.AT_MOST://                Log.i("=============heightMode", "onMeasure: " + "AT_MOST---> warp_content");//                break;//            case MeasureSpec.EXACTLY://                Log.i("=============heightMode", "onMeasure: " + "Exactly---> match_parent");//                break;//        }    }    @Override    protected void onDraw(Canvas canvas) {        //在布局文件中设置的圆环半径大小就可以不用写死        paint.setStrokeWidth(0);        paint.setColor(Color.BLACK);        paint.setStyle(Paint.Style.STROKE);        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize, paint);        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize - mRingSize, paint);        paint.setTextSize(mTextSize);        String text = mCountProgress + "%";        float textWidth = paint.measureText(text);        canvas.drawText(text, getMeasuredWidth()/2-textWidth/2 , getMeasuredWidth()/2 + mTextSize/2, paint);        RectF rectF = new RectF(getMeasuredWidth()/2 - mRadiuSize + mRingSize/2,getMeasuredHeight()/2 - mRadiuSize + mRingSize/2,getMeasuredWidth()/2 + mRadiuSize - mRingSize/2,getMeasuredHeight()/2 + mRadiuSize - mRingSize/2);        paint.setStrokeWidth(mRingSize);        paint.setColor(mProgressColor);        canvas.drawArc(rectF, 0, mProgress, false, paint);    }    public void setProgress(int progress){        mProgress = progress;        mCountProgress = progress * 100 / 360;        invalidate();    }}
======================================================================
public class MainActivity extends AppCompatActivity {    MyProgressRound mpr;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mpr = (MyProgressRound) findViewById(R.id.mpr);        new AsyncTask<String, Integer, String>(){            @Override            protected String doInBackground(String... params) {                for (int i = 0; i <= 360; i++) {                    SystemClock.sleep(10);                    publishProgress(i);                }                return null;            }            //该方法的调用条件是   publishProgress            @Override            protected void onProgressUpdate(Integer... values) {                mpr.setProgress(values[0]);            }        }.execute();    }}
 
原创粉丝点击