Android 自定义ProgressBar 实现进度圆环
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android 自定义ProgressBar 实现进度圆环
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                實現(xiàn)的效果如下圖
實現(xiàn)效果圖demo 的地址
代碼很簡單自定義ProgressBar?
下面直接列舉下代碼
progressBarView 的代碼如下
public class ProgressBarView extends View {// 底色圓環(huán)的畫筆private Paint bgPaint;// 底色圓環(huán)的顏色private int bgColor;// 進度圓的畫筆private Paint ringProgressPaint;// 進度圓顏色private int ringProgressColor;private float ringWidth;private int max;private int progress;public ProgressBarView(Context context) {this(context, null);initPaint();}public ProgressBarView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);initAttrs(context, attrs);initPaint();}public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initAttrs(context, attrs);initPaint();}private void initPaint() {bgPaint = new Paint();bgPaint.setColor(bgColor);bgPaint.setStyle(Paint.Style.STROKE);bgPaint.setStrokeWidth(ringWidth);bgPaint.setAntiAlias(true);ringProgressPaint = new Paint();ringProgressPaint.setColor(ringProgressColor);ringProgressPaint.setStrokeWidth(ringWidth);ringProgressPaint.setStrokeCap(Paint.Cap.ROUND);ringProgressPaint.setAntiAlias(true);ringProgressPaint.setStyle(Paint.Style.STROKE);}private void initAttrs(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressBarView);bgColor = typedArray.getColor(R.styleable.ProgressBarView_ringColor, Color.GRAY);ringProgressColor = typedArray.getColor(R.styleable.ProgressBarView_ringProgressColor, Color.GREEN);ringWidth = typedArray.getDimension(R.styleable.ProgressBarView_ringWidth, 20);max = typedArray.getInteger(R.styleable.ProgressBarView_max, 100);//資源回收typedArray.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int xCenter = getWidth() / 2;int yCenter = getHeight() / 2;int radius = (int) (xCenter - ringWidth / 2);// 繪制背景圓canvas.drawCircle(xCenter, yCenter, radius, bgPaint);// 繪制進度圓RectF rectF = new RectF(xCenter - radius, yCenter - radius, xCenter + radius, yCenter + radius);canvas.drawArc(rectF, -90, progress * 360 / max, false, ringProgressPaint);}public synchronized int getMax() {return max;}public synchronized void setMax(int max) {if (max < 0) {throw new IllegalArgumentException("max not less than 0");}this.max = max;}public synchronized int getProgress() {return progress;}public synchronized void setProgress(int progress) {if (progress < 0) {throw new IllegalArgumentException("progress not less than 0");}if (progress > max) {progress = max;}if (progress <= max) {this.progress = progress;postInvalidate();}}public Paint getBgPaint() {return bgPaint;}public void setBgPaint(Paint bgPaint) {this.bgPaint = bgPaint;}public int getBgColor() {return bgColor;}public void setBgColor(int bgColor) {this.bgColor = bgColor;}public Paint getRingProgressPaint() {return ringProgressPaint;}public void setRingProgressPaint(Paint ringProgressPaint) {this.ringProgressPaint = ringProgressPaint;}public int getRingProgressColor() {return ringProgressColor;}public void setRingProgressColor(int ringProgressColor) {this.ringProgressColor = ringProgressColor;}public float getRingWidth() {return ringWidth;}public void setRingWidth(float ringWidth) {this.ringWidth = ringWidth;}
}
 
attr?ProgressBarView 的code 如下:
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="ProgressBarView"><attr name="ringColor" format="color" /><attr name="ringProgressColor" format="color" /><attr name="ringWidth" format="dimension" /><attr name="max" format="integer" /><attr name="showTextProgress" format="boolean" /></declare-styleable>
</resources> 
?
activity 代碼如下
public class ProgressViewActivity extends AppCompatActivity implements Handler.Callback {private ProgressBarView progressBarView;private Handler mHandler;private int progress = 0;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.progress_view_activity_layout);progressBarView = findViewById(R.id.progress_view);mHandler = new Handler(this);new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(50);progress++;mHandler.sendEmptyMessage(1);if (progress >= 100) {progress = 0;}} catch (Exception e) {e.printStackTrace();}}}}).start();}@Overridepublic boolean handleMessage(Message msg) {switch (msg.what) {case 1:progressBarView.setProgress(progress);progressBarView.invalidate();break;default:break;}return false;}
}
 
activity 中代碼xml 的代碼如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.hly.projressbar.ProgressBarViewandroid:id="@+id/progress_view"android:layout_width="200dp"android:layout_height="200dp"android:layout_centerInParent="true" /></RelativeLayout> 
?
總結(jié)
以上是生活随笔為你收集整理的Android 自定义ProgressBar 实现进度圆环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。