android 实现圆形进度条
16lz
2021-01-23
要实现圆形进度条只能使用自定义view了,实现代码比较简单,自定义view代码
package com.example.apple.memory;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * Created by apple on 17/5/26. */public class CircleProgressView extends View { private int mMaxProgress = 100; private int Progress = 0; private int Progress1 = 0; private int Progress2 = 0; private final int mCircleLineStrokeWidth = 100;//设置圆形画笔宽度 private final int mTxtStrokeWidth = 2;//设置文字画笔宽度 // 画圆所在的距形区域 private RectF mRectF; private Paint mPaint; public CircleProgressView(Context context, AttributeSet attrs) { super(context, attrs,0); mRectF = new RectF(); mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); if (width != height) { int min = Math.min(width, height); width = min; height = min; } // 设置画笔相关属性 mPaint.setAntiAlias(true); mPaint.setColor(Color.WHITE); canvas.drawColor(Color.TRANSPARENT); mPaint.setStrokeWidth(mCircleLineStrokeWidth); mPaint.setStyle(Paint.Style.STROKE); // 位置 mRectF.left = mCircleLineStrokeWidth / 2; mRectF.top = mCircleLineStrokeWidth / 2; mRectF.right = width - mCircleLineStrokeWidth / 2; mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 绘制圆圈,进度条背景 canvas.drawArc(mRectF, -180, 360, false, mPaint); mPaint.setColor(Color.RED); canvas.drawArc(mRectF, -180, ((float) Progress / mMaxProgress) * 360, false, mPaint); //绘制第二段长度 mPaint.setColor(Color.BLUE); canvas.drawArc(mRectF,-180+ ((float) Progress / mMaxProgress) * 360, ((float) Progress1 / mMaxProgress) * 360, false, mPaint); //绘制第三段长度 mPaint.setColor(Color.BLACK); canvas.drawArc(mRectF,-180+ ((float) (Progress+Progress1) / mMaxProgress) * 360, ((float) Progress2 / mMaxProgress) * 360, false, mPaint); //绘制百分百 mPaint.setStrokeWidth(mTxtStrokeWidth); String text = Progress + "%"; int textHeight = height / 4; mPaint.setTextSize(textHeight); int textWidth = (int) mPaint.measureText(text, 0, text.length()); mPaint.setStyle(Paint.Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint); } public int getMaxProgress() { return mMaxProgress; } public void setMaxProgress(int maxProgress) { this.mMaxProgress = maxProgress; } public void setProgress(int progress) { this.Progress = progress; this.invalidate(); } public void setProgress1(int progress) { this.Progress1 = progress; this.invalidate(); } public void setProgress2(int progress) { this.Progress2 = progress; this.invalidate(); }}
2、引用的话首先布局中直接添加就好 <?xml version="1.0" encoding="utf-8"?>
3、代码中使用,为了看到有个动画效果所以加了个定时器,可以看到慢慢增加到指定值。 package com.example.apple.memory;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { private CircleProgressView circleProgressView; private final int UPDATE_PROGRESS = 0; private int number = 0; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_PROGRESS: circleProgressView.setProgress(2 + number); circleProgressView.setProgress1(10 + number); circleProgressView.setProgress2(20 + number); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { circleProgressView = (CircleProgressView) findViewById(R.id.circleProgressBar); circleProgressView.setMaxProgress(100); new TestThread().start(); } class TestThread extends Thread { @Override public void run() { while (number < 20) { number++; mHandler.sendEmptyMessage(UPDATE_PROGRESS); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override protected void onDestroy() { if (mHandler != null) { mHandler.removeCallbacksAndMessages(0); mHandler = null; } super.onDestroy(); }}
实现效果
更多相关文章
- android 关于gallery 的平滑代码滚动
- Android客户端自动更新代码
- [CSDN]Android应用程序启动过程源代码分析
- android连续按两次返回退出程序(完整代码)
- Android Java代码执行adb Shell命令
- 探寻 Android 代码抄袭细节,情节还不算严重
- Android清除本地数据缓存代码案例
- 【Android学习笔记】AutoCompleteTextView(数组在代码中)
- Android 高通代码预制apk可卸载,恢复出厂设置apk可恢复 Android M