Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現環形進度條代碼

Android實現環形進度條代碼

編輯:關於Android編程

本文參考借鑒:http://www.jb51.net/article/102983.htm

先上效果圖:

自定義控件:AttendanceProgressBar

代碼如下:

 public class AttendanceProgressBar extends View {
 // 畫圓環底部的畫筆
 private Paint mCirclePaint;
 // 畫圓環的畫筆
 private Paint mRingPaint;
 // 畫字體的畫筆
 private Paint mTextPaint;
 // 圓形顏色
 private int mCircleColor;
 // 圓環顏色
 private int mRingColor;
 // 半徑
 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 = 80;
 //字體顏色
 private int mTextColor;
 // 字體大小
 private float mTextSize;

 public AttendanceProgressBar(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.AttendanceProgressBar, 0, 0);
 mRadius = typeArray.getDimension(R.styleable.AttendanceProgressBar_radius, 80);
 mStrokeWidth = typeArray.getDimension(R.styleable.AttendanceProgressBar_strokeWidth, 10);
 mCircleColor = typeArray.getColor(R.styleable.AttendanceProgressBar_circleColor, 0xFFFFFFFF);
 mRingColor = typeArray.getColor(R.styleable.AttendanceProgressBar_ringColor, 0xFFFFFFFF);
 mTextColor = typeArray.getColor(R.styleable.AttendanceProgressBar_textColor, 0xFF000000);
 mTextSize = typeArray.getDimension(R.styleable.AttendanceProgressBar_textSize, 80);

 mRingRadius = mRadius + mStrokeWidth / 2;
 }

 private void initVariable() {
 mCirclePaint = new Paint();
 mCirclePaint.setAntiAlias(true);
 mCirclePaint.setColor(mCircleColor);
// mCirclePaint.setStyle(Paint.Style.FILL);
 mCirclePaint.setStyle(Paint.Style.STROKE);
 mCirclePaint.setStrokeWidth(mStrokeWidth);

 mRingPaint = new Paint();
 mRingPaint.setAntiAlias(true);
 mRingPaint.setColor(mRingColor);
 mRingPaint.setStyle(Paint.Style.STROKE);
 mRingPaint.setStrokeWidth(mStrokeWidth);

 mTextPaint = new Paint();
 mTextPaint.setAntiAlias(true);
 mTextPaint.setStyle(Paint.Style.FILL);
 mTextPaint.setColor(mTextColor);
// mTextPaint.setARGB(255, 255, 255, 255);
// mTextPaint.setTextSize(mRadius / 2);
 mTextPaint.setTextSize(mTextSize);
 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);

 canvas.drawCircle(mXCenter, mYCenter, mRingRadius, mCirclePaint);
 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); //
//   canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2, mRingPaint);
//  String txt = mProgress + "%";
 String txt = "16/18";
 mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
//  canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
 canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4 - dipToPx(10), mTextPaint);
 txt = "出勤人數";
 mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
 canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4 + dipToPx(10), mTextPaint);
 }

 public void setProgress(int progress) {
 mProgress = progress;
 postInvalidate();
 }
 private int dipToPx(int dip) {
 float scale = getContext().getResources().getDisplayMetrics().density;
 return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
 }


}

因為是自定義控件,所以在attr.xml文件定義了一些控件屬性,以便在xml文件中設置這些屬性

代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="AttendanceProgressBar">
 <attr name="radius" format="dimension"/>
 <attr name="strokeWidth" format="dimension"/>
 <attr name="circleColor" format="color"/>
 <attr name="ringColor" format="color"/>
 <attr name="textColor" format="color"/>
 <attr name="textSize" format="dimension"/>
 </declare-styleable>
</resources>

最後,在xml文件中,可以這樣使用

 <com.ztd.lieyi.widget.AttendanceProgressBar
  android:layout_gravity="center"
  android:layout_width="100dp"
  android:layout_height="100dp"
  app:radius="45dp"
  app:strokeWidth="5dp"
  app:textSize="@dimen/text_16"
  app:textColor="@color/color_333333"
  app:circleColor="@color/color_d5ebfd"
  app:ringColor="@color/color_2c9df7"/>

這只是初步處理,使用時可以根據需求酌情處理。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved