Android自定义View实现炫酷进度条
作者:AD钙奶-lalala 发布时间:2023-09-22 00:16:07
标签:Android,进度条
本文实例为大家分享了Android实现炫酷进度条的具体代码,供大家参考,具体内容如下
下面我们来实现如下效果:
第一步:创建attrs文件夹,自定义属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyProgress">
<attr name="out_color" format="color"/>
<attr name="inner_color" format="color"/>
<attr name="border_width" format="dimension"/>
<attr name="text_color" format="color"/>
<attr name="text_size" format="dimension"/>
</declare-styleable>
</resources>
第二步:自定义View:
/**
* Created by Michael on 2019/11/1.
*/
public class MyProgress extends View {
private int outColor;
private int innerColor;
private int textColor;
private float borderWidth;
private int textSize;
private Paint mOutPaint;
private Paint mInnerPaint;
private Paint mTextPaint;
private float percent;
private int p;
public MyProgress(Context context) {
this(context,null);
}
public MyProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyProgress);
outColor = array.getColor(R.styleable.MyProgress_out_color, Color.GREEN);
innerColor = array.getColor(R.styleable.MyProgress_inner_color, Color.BLUE);
textColor = array.getColor(R.styleable.MyProgress_text_color, Color.BLACK);
borderWidth = array.getDimension(R.styleable.MyProgress_border_width,10);
textSize = array.getDimensionPixelSize(R.styleable.MyProgress_text_size,20);
array.recycle();
init();
}
private void init() {
mOutPaint = new Paint();
mOutPaint.setAntiAlias(true);
mOutPaint.setDither(true);
mOutPaint.setStyle(Paint.Style.STROKE);
mOutPaint.setStrokeWidth(borderWidth);
mOutPaint.setColor(outColor);
mInnerPaint = new Paint();
mInnerPaint.setAntiAlias(true);
mInnerPaint.setDither(true);
mInnerPaint.setStyle(Paint.Style.STROKE);
mInnerPaint.setStrokeWidth(borderWidth);
mInnerPaint.setColor(innerColor);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setDither(true);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTextSize(textSize);
mTextPaint.setColor(textColor);
percent = 0;
p = 100;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = 0,height =0;
if (widthMode == MeasureSpec.AT_MOST){
}else{
width = MeasureSpec.getSize(widthMeasureSpec);
}
if (heightMode == MeasureSpec.AT_MOST){
}else{
height = MeasureSpec.getSize(heightMeasureSpec);
}
setMeasuredDimension(width>height?height:width,width>height?height:width);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int rWidth = getWidth()>getHeight()?getHeight():getWidth();
int rHeight = getWidth()>getHeight()?getHeight():getWidth();
//int rWidth = getWidth();
//int rHeight = getHeight();
float radius = rWidth/2 - borderWidth/2;
canvas.drawCircle(rWidth/2,rHeight/2,radius,mOutPaint);
RectF r = new RectF(borderWidth/2,borderWidth/2,
rWidth-borderWidth/2,rHeight-borderWidth/2);
canvas.drawArc(r,0,360*percent,false,mInnerPaint);
String s1 = (int)(percent*100) + "%";
Rect r2 = new Rect();
mTextPaint.getTextBounds(s1,0,s1.length(),r2);
int tWidth = r2.width();
int tHeight = r2.height();
Paint.FontMetricsInt fontMetricsInt = new Paint.FontMetricsInt();
int dy = (fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;
int baseLine = tHeight/2+dy+rHeight/2-tHeight/2;
int x0 = rWidth/2-tWidth/2;
canvas.drawText(s1,x0,baseLine,mTextPaint);
}
public void setProgress(float percent,int value){
this.percent = percent;
invalidate();
}
}
然后在布局中使用:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.michael.view_02.MainActivity">
<com.example.michael.view_02.MyProgress
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:out_color="@color/colorPrimary"
app:inner_color="@color/colorAccent"
app:text_color="@color/colorPrimaryDark"
app:border_width="10dp"
app:text_size="20sp"
/>
</android.support.constraint.ConstraintLayout>
在activity中使用属性动画完成效果:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MyProgress progress = findViewById(R.id.progress);
ValueAnimator animator = ValueAnimator.ofInt(0,5000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float p = animation.getAnimatedFraction();
int value = (int)animation.getAnimatedValue();
progress.setProgress(p,value);
}
});
animator.setDuration(10000);
animator.start();
}
}
如果我们改动一下代码:
//int rWidth = getWidth();
//int rHeight = getHeight();
我们使用onDraw()方法的时候如果使用上面的方法确定宽高,将会绘制成下图所示:
很奇怪!欢迎大家解决此问题。
来源:https://blog.csdn.net/qq_36428821/article/details/102858337


猜你喜欢
- 1.概述本文中,我将向你介绍Spring Cloud Netflix Turbine。它将多个Hystrix Metrics Streams
- 1.简介其实这个效果几天之前就写了,但是一直没有更新博客,本来想着把芝麻分雷达图也做好再发博客的,然后今天看到鸿洋的微信公众号有朋友发了芝麻
- 背景在写一个东西滑动删除列表的时候,出现了一个问题。我的需求是,左滑然后出现delete,然后点击delete,让该滑块消失。我在点列表的第
- drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如Men
- 一、线性布局LinearLayout有两种排序方式orientation属性值为horizontal时,内部视图在水平方向从左往右排列。or
- 目前只实现了java生成的固定的uuid:85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28的序列化package m
- /*开机自动启动APP*/public class BootReceiver extends BroadcastReceiver {@Ove
- 背景Swagger 可以提供 API 操作的测试文档,本文记录 Swagger 使用过程中遇到的两个小问题:全局响应结果进行包装后导致 Sw
- 先随便创建一个存储过程DELIMITER $$CREATE PROCEDUREgetUserNameById (IN i_id BIGINT
- 恩 主要大家可以看下思路吧 图形界面里 除了图标和音乐两个资源 别的都是代码。 时间没有用timer组件 是自创的T
- C#控制台程序使用Log4net日志组件,供大家参考,具体内容如下1、Log4net一般都不陌生,但是在配置上不同类型的项目又不相同的地方比
- 问题:在用Java程序进行读写含中文的txt文件时,经常会出现读出或写入的内容会出现乱码。原因其实很简单,就是系统的编码和程序的编码采用了不
- @Autowired注解和静态方法及new关系一、@autowired 与new new出来的对象无法调用@Autowired注入
- properties和yml的区别这几天刚好看到Spring Boot当中有两种配置文件的方式,但是这两种配置方式有什么区别呢?proper
- 本文实例为大家分享了C#添加Windows服务的具体方法,供大家参考,具体内容如下源码下载地址:http://xiazai.jb51.net
- 本文实例为大家分享了Android实现竖直跑马灯效果的具体代码,供大家参考,具体内容如下首先给出跑马灯效果图中间的色块是因为视频转成GIF造
- java Long类型转为String类型1、Long.ValueOf("String")返回Long包装类型数据包装类
- 如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作。同时,这个问题在Stack Overflow
- 循环轮播的方法有两种,一种是使用定时器另外一种是使用手指拨动,相比较而言,定时器实现循环播放比较容易,只要在定时器的消息里加上简单几段代码即
- 使用方法 首先在Github或者Gitee上面新建一个仓库复制仓库的链接用idea在本地新建一个demo项目点击菜单栏的VCS,按