公司有這樣一個需求,實現這個圓弧進度條
所以,現在就將它抽取出來分享
- 如果需要是圓帽的就將,下面這句代碼放開即可
- mRingPaint.setStrokeCap(Paint.Cap.ROUND);//設置線冒樣式,有圓 有方
不廢話,直接上代碼
- 自定義view
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; import android.view.View; /** * Created by Administrator on 2017/8/5. */ public class CompletedView extends View { // 畫實心圓的畫筆 private Paint mCirclePaint; // 畫圓環的畫筆 private Paint mRingPaint; // 畫圓環的畫筆背景色 private Paint mRingPaintBg; // 畫字體的畫筆 private Paint mTextPaint; // 圓形顏色 private int mCircleColor; // 圓環顏色 private int mRingColor; // 圓環背景顏色 private int mRingBgColor; // 半徑 private float mRadius; // 圓環半徑 private float mRingRadius; // 圓環寬度 private float mStrokeWidth; // 圓心x坐標 private int mXCenter; // 圓心y坐標 private int mYCenter; // 字的長度 private float mTxtWidth; // 字的高度 private float mTxtHeight; // 總進度 private int mTotalProgress = 100; // 當前進度 private int mProgress; public CompletedView(Context context, AttributeSet attrs) { super(context, attrs); // 獲取自定義的屬性 initAttrs(context, attrs); initVariable(); } //屬性 private void initAttrs(Context context, AttributeSet attrs) { TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TasksCompletedView, 0, 0); mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius, 80); mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 10); mCircleColor = typeArray.getColor(R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF); mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF); mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFFFFFFF); mRingRadius = mRadius + mStrokeWidth / 2; } //初始化畫筆 private void initVariable() { //內圓 mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setColor(mCircleColor); mCirclePaint.setStyle(Paint.Style.FILL); //外圓弧背景 mRingPaintBg = new Paint(); mRingPaintBg.setAntiAlias(true); mRingPaintBg.setColor(mRingBgColor); mRingPaintBg.setStyle(Paint.Style.STROKE); mRingPaintBg.setStrokeWidth(mStrokeWidth); //外圓弧 mRingPaint = new Paint(); mRingPaint.setAntiAlias(true); mRingPaint.setColor(mRingColor); mRingPaint.setStyle(Paint.Style.STROKE); mRingPaint.setStrokeWidth(mStrokeWidth); //mRingPaint.setStrokeCap(Paint.Cap.ROUND);//設置線冒樣式,有圓 有方 //中間字 mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setColor(mRingColor); mTextPaint.setTextSize(mRadius / 2); Paint.FontMetrics fm = mTextPaint.getFontMetrics(); mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent); } //畫圖 @Override protected void onDraw(Canvas canvas) { mXCenter = getWidth() / 2; mYCenter = getHeight() / 2; //內圓 canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint); //外圓弧背景 RectF oval1 = new RectF(); oval1.left = (mXCenter - mRingRadius); oval1.top = (mYCenter - mRingRadius); oval1.right = mRingRadius * 2 + (mXCenter - mRingRadius); oval1.bottom = mRingRadius * 2 + (mYCenter - mRingRadius); canvas.drawArc(oval1, 0, 360, false, mRingPaintBg); //圓弧所在的橢圓對象、圓弧的起始角度、圓弧的角度、是否顯示半徑連線 //外圓弧 if (mProgress > 0 ) { RectF oval = new RectF(); oval.left = (mXCenter - mRingRadius); oval.top = (mYCenter - mRingRadius); oval.right = mRingRadius * 2 + (mXCenter - mRingRadius); oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius); canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); // //字體 String txt = mProgress + "分"; mTxtWidth = mTextPaint.measureText(txt, 0, txt.length()); canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint); } } //設置進度 public void setProgress(int progress) { mProgress = progress; postInvalidate();//重繪 } }
- attrs.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <!--圓弧進度條--> <declare-styleable name="TasksCompletedView"> <attr name="radius" format="dimension"/> <attr name="strokeWidth" format="dimension"/> <attr name="circleColor" format="color"/> <attr name="ringColor" format="color"/> <attr name="ringBgColor" format="color"/> </declare-styleable> </resources>
- color.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="white">#FFFFFF</color> <color name="white2">#f5f3f3</color> <color name="colorRed">#d50f09</color> </resources>
- MainActivity文件!,,,注意,,,開一個子線程去刷新進度,如果用系統handler去更新,阻塞主線程會出現界面刷新的卡頓情況,因為handler就是程操作主線程的
public class MainActivity extends AppCompatActivity { private int mTotalProgress = 90; private int mCurrentProgress = 0; //進度條 private CompletedView mTasksView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTasksView = (CompletedView) findViewById(R.id.tasks_view); new Thread(new ProgressRunable()).start(); } class ProgressRunable implements Runnable { @Override public void run() { while (mCurrentProgress < mTotalProgress) { mCurrentProgress += 1; mTasksView.setProgress(mCurrentProgress); try { Thread.sleep(90); } catch (Exception e) { e.printStackTrace(); } } } } }
- activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tc="http://schemas.android.com/apk/res/com.example.administrator.arcprogresbar" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#addafd" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <com.example.administrator.arcprogresbar.CompletedView android:id="@+id/tasks_view" android:layout_width="223dp" android:layout_height="223dp" tc:circleColor="@color/white" tc:radius="50dip" tc:ringBgColor="@color/white2" tc:ringColor="@color/colorRed" tc:strokeWidth="10dip" /> </LinearLayout> </LinearLayout>
布局文件中后面自定義命名空間接着的是應用的包名:xmlns:tc=”http://schemas.android.com/apk/res/com.example.administrator.arcprogresbar”
-
注意:xmlns:tc=”http://schemas.android.com/apk/res/包名”
-
再次提醒:刷新進度條要用子線程去執行,防止主線程出現卡頓情況