[Android][自定义进度条]④--圆形进度条的onMeasure

RoundProgressBarWithProgress .java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
 * Created by TONG on 2017/3/25.
 */

public class RoundProgressBarWithProgress extends HorizontalProgressbarWithProgress{

    private int mRadius=dp2px(30);

    private int mMaxPaintWidth;

    public RoundProgressBarWithProgress(Context context) {
        this(context,null);
    }

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

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

        mReachHeight= (int) (mUnReachHeight*2.5f);

        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBarWithProgress);
        mRadius= (int) ta.getDimension(R.styleable.RoundProgressBarWithProgress_radius,mRadius);
        ta.recycle();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMaxPaintWidth=Math.max(mReachHeight,mUnReachHeight);
        //默认四个padding一致
        int expect=mRadius*2+mMaxPaintWidth+getPaddingLeft()+getPaddingRight();

        //设置默认warp_content大小
        int width=resolveSize(expect,widthMeasureSpec);
        int height=resolveSize(expect,heightMeasureSpec);

        int readWidth=Math.min(width,height);

        mRadius=(readWidth-getPaddingLeft()-getPaddingRight()-mMaxPaintWidth)/2;

        setMeasuredDimension(readWidth,readWidth);

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text=getProgress()+"%";
        float textWidth=mPaint.measureText(text);
        float textHeight=(mPaint.descent()+mPaint.ascent())/2;

        canvas.save();

        canvas.translate(getPaddingLeft()+mMaxPaintWidth/2,getPaddingTop()+mMaxPaintWidth/2);
        mPaint.setStyle(Paint.Style.STROKE);

        //draw unreach bar
        mPaint.setColor(mUnReachColor);
        mPaint.setStrokeWidth(mUnReachHeight);
        //圆心XY 半径
        canvas.drawCircle(mRadius,mRadius,mRadius,mPaint);

        //draw reach bar
        mPaint.setColor(mReachColor);
        mPaint.setStrokeWidth(mReachHeight);

        float sweepAngle=getProgress()*1.0f/getMax()*360;
        //绘制弧
        canvas.drawArc(new RectF(0,0,mRadius*2,mRadius*2),0,sweepAngle,false,mPaint);

        //draw text
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(text,mRadius-textWidth/2,mRadius-textHeight,mPaint);
        canvas.restore();

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:hyman="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <myapplication4.xt.com.myapplication4.RoundProgressBarWithProgress
        android:layout_width="wrap_content"
        hyman:radius="60dp"
        android:layout_marginTop="30dp"
        android:padding="15dp"
        android:progress="30"
        hyman:progress_text_size="30dp"
        hyman:progress_text_color="#44ff0000"
        hyman:progress_unreach_color="#ffff0000"
        android:layout_height="wrap_content" />
    <myapplication4.xt.com.myapplication4.RoundProgressBarWithProgress
        android:layout_width="wrap_content"
        hyman:radius="60dp"
        android:id="@+id/id_progress"
        android:layout_marginTop="30dp"
        android:padding="15dp"
        android:progress="0"
        hyman:progress_text_color="#44ff0000"
        hyman:progress_unreach_color="#ffff0000"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package myapplication4.xt.com.myapplication4;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RoundProgressBarWithProgress pb;

    private static final  int MSG_UPDATE=0x110;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==MSG_UPDATE){
                int p= pb.getProgress();
                pb.setProgress(p);
                pb.setProgress(++p);
                if(p>=100){
                    handler.removeMessages(MSG_UPDATE);
                }
            }
            sendEmptyMessageDelayed(MSG_UPDATE,100);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (RoundProgressBarWithProgress) findViewById(R.id.id_progress);
        handler.sendEmptyMessage(MSG_UPDATE);


    }
}

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容