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

废话不多说,下面直接上代码:
自定义控件代码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();
}
}
}
}
}
其实实现非常简单,就是通过进度画圆环,具体的我也就不多说了,代码中有不足之处希望大家批评指正,共同学习!
Android自定义圆形ProgressBar的更多相关文章
- 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变
带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115 近期有 ...
- Android 自定义圆形图片 CircleImageView
1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...
- android 自定义圆形进度条
一.通过动画实现 定义res/anim/loading.xml如下: [html] view plain copy print ? <?xml version="1.0" ...
- Android 自定义圆形旋转进度条,仿微博头像加载效果
微博 App 的用户头像有一个圆形旋转进度条的加载效果,看上去效果非常不错,如图所示: 据说 Instagram 也采用了这种效果.最近抽空研究了一下,最后实现的效果是这样: 基本上能模拟出个大概,代 ...
- Android自定义圆形图片工具类(CTRL+C加CTRL+V直接使用)
先贴一下工具类的代码!可直接复制粘贴 public class RoundImageView extends ImageView { private Paint mPaint; //画笔 privat ...
- [转]android 自定义圆形imageview控件
android布局 首先,定义定义圆形Imageview类: import android.content.Context; import android.graphics.Bitmap; imp ...
- Android 修改圆形progressBar颜色
查了半天资料,没查到怎样修改progressBar的方法,全都是重新写个ProgressBar,其实很简单在只要一句xml里一句化就可以 android:indeterminateTint=" ...
- android自定义圆形图片和遇到的问题
画圆遇到的问题:图片单位不一样,导致图片只能显示出圆的一部分:看代码: public class MyCircleIamge extends ImageView { private Context c ...
- Android自定义圆形进度条,完成类似LOFTER效果
1.http://stackoverflow.com/questions/3760381/rotating-image-animation-list-or-animated-rotate-androi ...
随机推荐
- Mybatis框架学习总结-优化Mybatis配置文件中的配置
连接数据库的配置单独放在一个properties文件中 之前,是直接将数据库的连接配置信息卸载了Mybatis的conf.xml文件中,如下: <?xml version="1.0&q ...
- Spark2.0机器学习系列之8:多类分类问题(方法归总和分类结果评估)
一对多(One-vs-Rest classifier) 将只能用于二分问题的分类(如Logistic回归.SVM)方法扩展到多类. 参考:http://www.cnblogs.com/CheeseZH ...
- php about session store db or cache
PHP关于Session的配置: 在php.ini中配置为:session.name = PHPSESSID 在请求开始的时候,会话名称会被重置并存储到session.name配置项. 所以要想在不改 ...
- 简单认识TCP/IP协议
HTTP协议—— 简单认识TCP/IP协议 本文转自: https://www.cnblogs.com/roverliang/p/5176456.html 大学没读计算机专业,所以很多的专业知识都 ...
- 使用js在网页上显示时间
<html> <script> function getDate(){ var d,s,t; d = new Date(); s = d.getFullYear().toStr ...
- Code signing is required for product type 'Application' in SDK 'iOS 11.2'
在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2' ,就是说代码签名证书 ...
- python16_day24【restful、crm表构、认证】
一.restful 1. pip install djangorestframework 2.settings.py INSTALLED_APPS = ( ... 'rest_framework', ...
- 设置 Quick-Cocos2d-x 在 Windows 下的编译环境
http://cn.cocos2d-x.org/tutorial/show?id=1304 设置 Quick-Cocos2d-x 在 Windows 下的编译环境 Liao Yulei2014-08- ...
- 实现:左边为菜单导航,当一个菜单中包含多个Tabs,并且不同的Tab要根据权限的不同显示。
1.前台代码 //当点击左侧菜单时,将访问Controller中的Home方法,这样就会根据用户权限的不同,通过后台的判断来决定显示的页面<li class="@(ViewBag.Se ...
- 处理函数和数组声明[条款17]---《C++必知必会》
指向函数的指针声明和指向数组的指针声明容易混淆,原因在于函数和数组修饰符的优先级比指针修饰符的优先级高,因此通常需要使用圆括号. int *f1( );//一个返回值为 int* 的函数 int ( ...