博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用开源项目circular progress button实现有进度条的Button
阅读量:6918 次
发布时间:2019-06-27

本文共 10133 字,大约阅读时间需要 33 分钟。

circular progress button可以让button实现进度条,效果和动画都做的很赞,只是有点小bug。需要注意的是按钮上的文字不能太大,否则会出现错位。

项目的地址:

下面我们来看看怎么使用它。

 

一、添加依赖并在xml中放入控件

从项目地址中下载好lib后导入自己的工程,在xml中放入这样一个button

 

 

二、在xml文件中设置各种属性

用这些属性需要自定义命名空间。xmlns:app="http://schemas.android.com/apk/res-auto"

textColor:全局字体颜色设置 textSize:全局字体大小设置cpb_cornerRadius:按钮的边缘弧度数cpb_textIdle:默认的text文字cpb_textComplete:完成时显示的textcpb_textError:出错时显示的textcpb_iconComplete:完成时显示的图标,这个和cpb_textComplete不能共用cpb_iconError:出错时显示的图标,这个和cpb_textError不能共用cpb_paddingProgress:圆形进度条和按钮的边界的差值,设定后按钮会在走进度时缩小到小进度条,走完后再由小进度条扩大到原始按钮cpb_selectorComplete:自定义完成时按钮背景的颜色,包括获取焦点、按下、普通各种状态cpb_selectorError:自定义出错时按钮背景的颜色,包括获取焦点、按下、普通各种状态cpb_selectorIdle:自定义初始时按钮的背景色,包括获取焦点、按下、普通各种状态

 

对于cpb_selectorComplete、cpb_selectorError、cpb_selectorIdle需要在drawable中写上selector文件。这样就可以设置各种背景色了。

 

complete_state_selector.xml

 

error_state_selector.xml

 

idle_state_selector.xml

 

三、在java代码中使用

由于是有进度条的button,我们先来对进度条的设计进行了解。

按钮按下后会触发一个圆形的进度条,这个进度条上面的进度我们是可以自行控制的,如果进度为100,那么说明加载完毕,直接会显示按钮成功的时的状态,如果进度是-1,那么就说明加载失败,按钮会显示出错的状态,如果进度是0,那么就是按钮默认的状态。

 按钮默认的状态,进度=0

按钮加载成功的状态,进度=100.这里面显示图片(对勾)还是显示文字都可以自定义。

加载是吧,按钮显示错误的状态,进度=-1,使用×还是用文字或别的图片由自己定义。

 

CircularProgressButton是这个按钮的对象,我们找到这个控件后就可以进行设置了,设置方式也很简单。

举例:

1.显示不精准进度的按钮

CircularProgressButton circularButton01 = (CircularProgressButton) findViewById(R.id.circularButton01);        circularButton01.setIndeterminateProgressMode(true); // 进入不精准进度模式        circularButton01.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                CircularProgressButton btn = (CircularProgressButton) v;                int progress = btn.getProgress();                System.out.println("progress = " + progress);                if (progress == 0) { // 初始时progress = 0                    btn.setProgress(50); // 点击后开始不精准进度,不精准进度的进度值一直为50                } else if (progress == 100) { // 如果当前进度为100,即完成状态,那么重新回到未完成的状态                    btn.setProgress(0);                } else if (progress == 50) { // 如果当前进度为50,那么点击后就显示完成的状态                    btn.setProgress(100); // -1表示出错,显示出错的图片和背景,100表示完成,显示完成的图片和背景                }            }        });

 

2.显示精准进度的按钮

按钮点击后调用simulateSuccessProgress方法,如果在进度加载时点击了按钮,那么按钮重新回到初始状态。

final CircularProgressButton circularButton03 = (CircularProgressButton) findViewById(R.id.circularButton03);        circularButton03.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (circularButton03.getProgress() == 0) {                    simulateSuccessProgress(circularButton03); // 如果是初始状态就开始进入进度条动画                } else {                    circularButton03.setProgress(0); // 如果不是初始状态,那么就回到初始状态                }            }        });

 

/**     * 设置成功的进度     * @param button     */    private void simulateSuccessProgress(final CircularProgressButton button) {        // 这里巧妙运用了valueAnimator这个类来计算动画的值,这个类本身就起计算作用,不处理任何动画,这里在计算好后自行进行了进度的设定        ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100); // 设定范围为1到100        widthAnimation.setDuration(1500); // 设定动画的持续时间        widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); // 设定动画的插值器        widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // 在动画进行时进行处理            @Override            public void onAnimationUpdate(ValueAnimator animation) {                Integer value = (Integer) animation.getAnimatedValue();                button.setProgress(value); // 设置进度为当前动画的进度            }        });        widthAnimation.start(); // 开始动画的计算工作    }

 

三、主要代码

1. xml

 

2. java

package com.kale.buttontest;import com.dd.CircularProgressButton;import android.animation.ValueAnimator;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.AccelerateDecelerateInterpolator;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        CircularProgressButton circularButton01 = (CircularProgressButton) findViewById(R.id.circularButton01);        circularButton01.setIndeterminateProgressMode(true); // 进入不精准进度模式        circularButton01.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                CircularProgressButton btn = (CircularProgressButton) v;                int progress = btn.getProgress();                System.out.println("progress = " + progress);                if (progress == 0) { // 初始时progress = 0                    btn.setProgress(50); // 点击后开始不精准进度,不精准进度的进度值一直为50                } else if (progress == 100) { // 如果当前进度为100,即完成状态,那么重新回到未完成的状态                    btn.setProgress(0);                } else if (progress == 50) { // 如果当前进度为50,那么点击后就显示完成的状态                    btn.setProgress(100); // -1表示出错,显示出错的图片和背景,100表示完成,显示完成的图片和背景                }            }        });        CircularProgressButton circularButton02 = (CircularProgressButton) findViewById(R.id.circularButton02);        circularButton02.setIndeterminateProgressMode(true);        circularButton02.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                CircularProgressButton btn = (CircularProgressButton) v;                int progress = btn.getProgress();                System.out.println("progress = " + progress);                if (progress == 0) { // 初始时progress = 0                    btn.setProgress(50); // 点击后开始不精准旋转,进度为50                } else if (progress == -1) { // 如果当前进度为-1,即出错状态,那么重新回到初始状态                    btn.setProgress(0);                } else if (progress == 50) { // 如果当前进度为50,那么点击后就显示出错的状态                    btn.setProgress(-1); // -1表示出错,显示出错的图片和背景                }            }        });        final CircularProgressButton circularButton03 = (CircularProgressButton) findViewById(R.id.circularButton03);        circularButton03.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (circularButton03.getProgress() == 0) {                    simulateSuccessProgress(circularButton03); // 如果是初始状态就开始进入进度条动画                } else {                    circularButton03.setProgress(0); // 如果不是初始状态,那么就回到初始状态                }            }        });        final CircularProgressButton circularButton04 = (CircularProgressButton) findViewById(R.id.circularButton04);        circularButton04.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (circularButton04.getProgress() == 0) { // 如果是初始状态就开始进入进度条动画                    simulateErrorProgress(circularButton04);                } else {                    circularButton04.setProgress(0); // 如果不是初始状态,那么就回到初始状态                }            }        });                        final CircularProgressButton circularButton05 = (CircularProgressButton) findViewById(R.id.circularButton05);        circularButton05.setIndeterminateProgressMode(true);        circularButton05.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (circularButton05.getProgress() == 0) {                    circularButton05.setProgress(50);                } else if (circularButton05.getProgress() == 100) {                    circularButton05.setProgress(0);                } else {                    circularButton05.setProgress(100);                }            }        });        final CircularProgressButton circularButton06 = (CircularProgressButton) findViewById(R.id.circularButton06);        circularButton06.setIndeterminateProgressMode(true);        circularButton06.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (circularButton06.getProgress() == 0) {                    circularButton06.setProgress(50);                } else if (circularButton06.getProgress() == -1) {                    circularButton06.setProgress(0);                } else {                    circularButton06.setProgress(-1);                }            }        });    }    /**     * 设置成功的进度     * @param button     */    private void simulateSuccessProgress(final CircularProgressButton button) {        // 这里巧妙运用了valueAnimator这个类来计算动画的值,这个类本身就起计算作用,不处理任何动画,这里在计算好后自行进行了进度的设定        ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 100); // 设定范围为1到100        widthAnimation.setDuration(1500); // 设定动画的持续时间        widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); // 设定动画的插值器        widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // 在动画进行时进行处理            @Override            public void onAnimationUpdate(ValueAnimator animation) {                Integer value = (Integer) animation.getAnimatedValue();                button.setProgress(value); // 设置进度为当前动画的进度            }        });        widthAnimation.start(); // 开始动画的计算工作    }    /**     * 设置出错时的进度条     * @param button     */    private void simulateErrorProgress(final CircularProgressButton button) {        ValueAnimator widthAnimation = ValueAnimator.ofInt(1, 99);        widthAnimation.setDuration(1500);        widthAnimation.setInterpolator(new AccelerateDecelerateInterpolator());        widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                Integer value = (Integer) animation.getAnimatedValue();                button.setProgress(value); // 将当前动画的进度设置为按钮的进度                if (value == 99) { // 如果按钮的进度到了99,那么直接设置为出错状态                    button.setProgress(-1);                }            }        });        widthAnimation.start(); // 开始动画的计算    }}

 

源码下载:

 

转载地址:http://unhcl.baihongyu.com/

你可能感兴趣的文章
MyBatis简单使用和入门理解
查看>>
图片移动效果
查看>>
基于Web的Kafka管理器工具之Kafka-manager安装之后第一次进入web UI的初步配置(图文详解)...
查看>>
C# Winform反序列化复杂json字符串
查看>>
SilverLight:布局(1) Border(边框)对象、Grid(网格)对象
查看>>
MonoBehaviour.print和Debug.Log是同样的作用
查看>>
SAP 接口概述
查看>>
BW Delta (增量)更新方法 .
查看>>
5.2. WebRTC
查看>>
Java的注解机制——Spring自动装配的实现原理
查看>>
正式生产环境下hadoop集群的DNS+NFS+ssh免password登陆配置
查看>>
SQL Server数据库大型应用解决方案总结
查看>>
[Think]故事几则
查看>>
gettools.exe 已停止工作
查看>>
视频监控相关文章
查看>>
Linux驱动的编译与加载
查看>>
Redis源代码分析-内存数据结构intset
查看>>
Chrome测试网站加载时间与流量消耗
查看>>
C++多重继承与虚基类及与.NET的比较
查看>>
[J2ME] 获得MIDlet信息
查看>>