这个有好多种做法,我就讲二种方法,第一种方法就是自定义一个view,通过canvas画线就可以,只要把paint的width改成view的高度就行:
package com.day01; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.math.BigDecimal; /** * Created by zhouguizhijxhz on 2018/5/24. */ public class CustomProgress extends View implements View.OnClickListener { private Paint defaultPaint; private Paint rollPaint; private float percent = 0.0f; private Paint textPaint; private Handler mHandler = new Handler(); private MyRunnable myRunnable; public CustomProgress(Context context) { this(context, null); } public CustomProgress(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CustomProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); myRunnable = new MyRunnable(); initPaint(); setOnClickListener(this); } private void initPaint() { defaultPaint = new Paint(); defaultPaint.setColor(Color.parseColor("#999999")); defaultPaint.setStyle(Paint.Style.STROKE); rollPaint = new Paint(); rollPaint.setColor(Color.parseColor("#1C86EE")); rollPaint.setStyle(Paint.Style.STROKE); textPaint = new Paint(); textPaint.setColor(Color.parseColor("#202020")); textPaint.setTextSize(sp2px(15)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureWidth(widthMeasureSpec); int height = measureHeight(heightMeasureSpec); setMeasuredDimension(width, height); } private int measureHeight(int heightMeasureSpec) { int mode = MeasureSpec.getMode(heightMeasureSpec); int height = MeasureSpec.ge