联系我们 - 广告服务
您的当前位置:官网首页 > 资讯 > 热评 >

Android 进度条按钮ProgressButton的实现代码

来源: 编辑: 时间:2019-08-08
导读: 有些app在点击下载按钮的时候,可以在按钮上显示进度,我们可以通过继承原生button,重写ondraw来实现带进度条的按钮。github:https://gi......

有些app在点击下载按钮的时候,可以在按钮上显示进度,我们可以通过继承原生button,重写ondraw来实现带进度条的按钮。

github:https://github.com/imcloudfloating/progressbar

1.效果:

2.原理:

创建三个gradientdrawable作为按钮背景、进度条背景和进度条前景,通过计算进度条的百分比来设置宽度,然后调用invalidate重绘。gradientdrawable设置颜色、圆角等参数,当然你也可以直接加载xml作为背景。

3.自定义参数:

在values目录建一个凯发网址娱乐attrs.xml文件

 xml version="." encoding="utf-" 
 resources 
 attr name="progresscolor" format="color" / 
 attr name="progressbackcolor" format="color" / 
 attr name="progress" format="integer" / 
 attr name="minprogress" format="integer" / 
 attr name="maxprogress" format="integer" / 
 declare-styleable name="progressbutton" 
 attr name="progresscolor" / 
 attr name="progressbackcolor" / 
 attr name="buttoncolor" format="color" / 
 attr name="cornerradius" format="dimension" / 
 attr name="progress" / 
 attr name="minprogress" / 
 attr name="maxprogress" / 
 attr name="progressmargin" format="dimension" / 
 /declare-styleable 
 /resources 

3.按钮类:


if  {
 progresswidth = mcornerradius * 2;
}

当进度条宽度小于2倍圆角半径的时候,进度条的圆角就和背景的圆角不一致,所以加上了上面这段代码。
获取宽度和高度其实用getwidth和getheight也可以,只不过在设计器中没法看到效果,所以我用了getmeasuredwidth和getmeasuredheight。


 package com.cloud.customviews;
 import android.content.context;
 import android.content.res.typedarray;
 import android.graphics.canvas;
 import android.graphics.drawable.gradientdrawable;
 import android.support.v.widget.appcompatbutton;
 import android.util.attributeset;
 public class progressbutton extends appcompatbutton {
 private float mcornerradius = ;
 private float mprogressmargin = ;
 private boolean mfinish;
 private int mprogress;
 private int mmaxprogress = ;
 private int mminprogress = ;
 private gradientdrawable mdrawablebutton;
 private gradientdrawable mdrawableprogressbackground;
 private gradientdrawable mdrawableprogress;
 public progressbutton {
 super;
 initialize;
 public progressbutton {
 super;
 initialize;
 private void initialize {
 //progress background drawable
 mdrawableprogressbackground = new gradientdrawable;
 //progress drawable
 mdrawableprogress = new gradientdrawable;
 //normal drawable
 mdrawablebutton = new gradientdrawable;
 //get default normal color
 int defaultbuttoncolor = getresources.getcolor;
 //get default progress color
 int defaultprogresscolor = getresources.getcolor;
 //get default progress background color
 int defaultbackcolor = getresources.getcolor;
 typedarray attr = context.obtainstyledattributes;
 try {
 mprogressmargin = attr.getdimension;
 mcornerradius = attr.getdimension;
 //get custom normal color
 int buttoncolor = attr.getcolor;
 //set normal color
 mdrawablebutton.setcolor;
 //get custom progress background color
 int progressbackcolor = attr.getcolor;
 //set progress background drawable color
 mdrawableprogressbackground.setcolor;
 //get custom progress color
 int progresscolor = attr.getcolor;
 //set progress drawable color
 mdrawableprogress.setcolor;
 //get default progress
 mprogress = attr.getinteger;
 //get minimum progress
 mminprogress = attr.getinteger;
 //get maximize progress
 mmaxprogress = attr.getinteger;
 } finally {
 attr.recycle;
 //set corner radius
 mdrawablebutton.setcornerradius;
 mdrawableprogressbackground.setcornerradius;
 mdrawableprogress.setcornerradius;
 setbackgrounddrawable;
 mfinish = false;
 @override
 protected void ondraw {
 if  {
 //calculate the width of progress
 float progresswidth =
  getmeasuredwidth *   / mmaxprogress - mminprogress);
 //if progress width less than x corner radius, the radius of progress will be wrong
 if  {
 progresswidth = mcornerradius * ;
 //set rect of progress
 mdrawableprogress.setbounds mprogressmargin,  mprogressmargin,
  , getmeasuredheight -  mprogressmargin);
 //draw progress
 mdrawableprogress.draw;
 if  {
 setbackgrounddrawable;
 mfinish = true;
 super.ondraw;
 * set current progress
 public void setprogress {
 if  {
 mprogress = progress;
 setbackgrounddrawable;
 invalidate;
 public void setmaxprogress {
 mmaxprogress = maxprogress;
 public void setminprogress {
 mminprogress = minprogress;
 public void reset {
 mfinish = false;
 mprogress = mminprogress;
 }

 使用:

 com.cloud.customviews.progressbutton
 android:id="@+id/button_progress_green"
 android:layout_width="dp"
 android:layout_height="wrap_content"
 android:layout_margintop="dp"
 android:textallcaps="false"
 android:textcolor="@color/colorwhite"
 android:text="@string/button_progress"
 app:cornerradius="dp"
 app:progressmargin="dp"
 app:progresscolor="@color/colorgreen"
 app:buttoncolor="@color/colorgreen" / 

总结

责任编辑:

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

上一篇:没有了
下一篇:Android studio将Module打包成Jar的方法

网友评论:

在“\templets\demo\comments.htm”原来的内容全部删除,插入第三方评论代码,如果不需要评论功能,删除comments.html里面的内容即可
推荐使用友言、多说、畅言(需备案后使用)等社会化评论插件

Copyright © 2018 凯发网址娱乐凯发网址娱乐-凯发网娱乐下载-凯发网娱乐官方网站 All Rights Reserved Power by DedeCms
本站所有资讯来源于网络 如有侵权请联系QQ:9490489
Top