日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android 自定义ProgressBar 实现进度圆环

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义ProgressBar 实现进度圆环 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現的效果如下圖

實現效果圖demo 的地址

代碼很簡單自定義ProgressBar?

下面直接列舉下代碼

progressBarView 的代碼如下

public class ProgressBarView extends View {// 底色圓環的畫筆private Paint bgPaint;// 底色圓環的顏色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>

?

總結

以上是生活随笔為你收集整理的Android 自定义ProgressBar 实现进度圆环的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。