Android自定义圆形ProgressBar

闲来无事做了一个自定义的进度条,大致效果图如下:


progressbar.gif

废话不多说,下面直接上代码:
自定义控件代码CircleProgressBar.java:

public class CircleProgressBar extends View{ // 画圆环的画笔 private Paint ringPaint; // 画字体的画笔 private Paint textPaint; // 圆环颜色 private int ringColor; // 字体颜色 private int textColor; // 半径 private float radius; // 圆环宽度 private float strokeWidth; // 字的长度 private float txtWidth; // 字的高度 private float txtHeight; // 总进度 private int totalProgress = 100; // 当前进度 private int currentProgress; // 透明度 private int alpha = 25; public CircleProgressBar(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.CircleProgressbar, 0 , 0); radius = typeArray.getDimension(R.styleable.CircleProgressbar_radius, 80); strokeWidth = typeArray.getDimension(R.styleable.CircleProgressbar_strokeWidth, 10); ringColor = typeArray.getColor(R.styleable.CircleProgressbar_ringColor, 0xFF0000); textColor = typeArray.getColor(R.styleable.CircleProgressbar_textColor, 0xFFFFFF); } private void initVariable() { ringPaint = new Paint(); ringPaint.setAntiAlias(true); ringPaint.setDither(true); ringPaint.setColor(ringColor); ringPaint.setStyle(Paint.Style.STROKE); ringPaint.setStrokeCap(Cap.ROUND); ringPaint.setStrokeWidth(strokeWidth); textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setStyle(Paint.Style.FILL); textPaint.setColor(textColor); textPaint.setTextSize(radius/2); FontMetrics fm = textPaint.getFontMetrics(); txtHeight = fm.descent + Math.abs(fm.ascent); } @Override protected void onDraw(Canvas canvas) { if (currentProgress >= 0) { ringPaint.setAlpha((int) (alpha + ((float) currentProgress / totalProgress)*230)); RectF oval = new RectF(getWidth() / 2 - radius, getHeight() / 2 - radius, getWidth() / 2 + radius, getHeight() / 2 + radius); canvas.drawArc(oval, 0, 0, false, ringPaint); canvas.drawArc(oval, -90, ((float) currentProgress / totalProgress) * 360, false, ringPaint); String txt = currentProgress + "%"; txtWidth = textPaint.measureText(txt, 0, txt.length()); canvas.drawText(txt, getWidth() / 2 - txtWidth / 2, getHeight() / 2 + txtHeight / 4, textPaint); } } public void setProgress(int progress) { currentProgress = progress; postInvalidate(); } } 

自定义控件的属性文件attrs.xml:

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

下面是主界面的布局文件和具体实现:
布局activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:cp="http://schemas.android.com/apk/res-auto"    
  android:layout_width="match_parent"    
  android:layout_height="match_parent"> <me.my.circleprogressbar.CircleProgressBar android:id="@+id/circleProgressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" cp:radius="80dp" cp:strokeWidth="10dp" cp:ringColor="#ff0000" cp:textColor="#0000ff"/> </RelativeLayout> 

实现MainActivity.java:

public class MainActivity extends Activity { private CircleProgressBar circleProgressBar; private int totalProgress = 100; private int currentProgress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); circleProgressBar = (CircleProgressBar) findViewById(R.id.circleProgressbar); new Thread(new ProgressRunable()).start(); } class ProgressRunable implements Runnable { @Override public void run() { while (currentProgress < totalProgress) { currentProgress += 1; circleProgressBar.setProgress(currentProgress); try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } } } } 

其实实现非常简单,就是通过进度画圆环,具体的我也就不多说了,代码中有不足之处希望大家批评指正,共同学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值