当前位置:首页 » 《随便一记》 » 正文

Android自定义ProgressBar,实现漂亮的进度提示框_独行猫A 的沉淀、积累、总结。天天学习,好好向上...linux,Android,Vue,Go)

2 人参与  2021年11月17日 12:03  分类 : 《随便一记》  评论

点击全文阅读


在android智能平板设备应用中,一项耗时的操作总要有个提示进度的框来提高用户的操作体验,操作进度提示框就显得很常用了。

系统自带的有进度条ProgressBar,一般用于显示一个过程,例如数据加载过程,文件下载进度,音乐播放进度等。但是样式太单一不好看,因此有必要自定义一个方便使用。

以下记录下封装的进度展示对话框ProgressDialog。

先来展示下效果图:

需要准备好素材。如上图中的那个旋转的圈圈,素材图是一张png图片,分辨率114x114:

如何实现自动旋转的效果呢,使用android的Rotate动画。

在res/drawable下建一个rotate_dialog_progress.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/loading_white"
    android:fromDegrees="0"
    android:interpolator="@android:anim/cycle_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1440" />

 这里面的几个属性解释:

<?xml version="1.0" encoding="utf-8"?>
 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"            #初始角度
    android:toDegrees="1440"           #结束时角度,值为正时顺时针旋转,值为负时逆时针旋转
    android:pivotX="50%"               #旋转中心x轴坐标,取值可以是数值(50)、百分数(50%)、百
                                        分数p(50%p),当取值为数值时,缩放起点为View左上角坐标
                                        加具体数值像素,当取值为百分数时,表示在当前View左上角坐
                                        加上View宽度的具体百分比,当取值为百分数p时,表示在View
                                        左上角坐标加上父控件宽度的具体百分比
    android:pivotY="50%"               #同上

    android:duration="700"             #动画持续时间,毫秒为单位
    android:fillAfter="true"           #动画结束后,保持结束时的状态
    android:fillBefore="true"          #动画结束后,恢复为初始状态
    android:fillEnabled="true"         #效果同上
    android:repeatCount="5"            #重复次数,取值为-1时无限重复,默认动画执行一次
    android:repeatMode ="reverse"      #重复模式,有reverse和restart两个值,前者为倒序回放,后者为重新开始
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" #插值器            
    />

接下来在styles.xml文件中定义一个样式文件供使用。内容如下:

<style name="myProgressBarStyleLarge">
        <item name="android:indeterminateDrawable">@drawable/rotate_dialog_progress</item>
        <item name="android:width">200dp</item>
        <item name="android:height">200dp</item>
    </style>

然后就可以这样使用我们自定义的progressbar啦:

<ProgressBar
        android:id="@+id/loadingImageView"
        style="@style/myProgressBarStyleLarge"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:padding="20dp" />

这还不算完,一般progressbar要放在dialog对话框中来用。看下对框框dialog的样式dialog_progress.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/corner_bg_dialog_progress"
    android:orientation="vertical"
    android:gravity="center"
    android:paddingTop="30dp"
    android:paddingBottom="30dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp">

    <ProgressBar
        android:id="@+id/loadingImageView"
        style="@style/myProgressBarStyleLarge"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:padding="20dp" />

    <TextView
        android:id="@+id/loadingmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:text="正在处理中..."
        android:textSize="30dp"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="10dp" />

</LinearLayout>

为了使Dialog的背景和边框的棱角好看,这里自定义了Dialog的背景。

android:background="@drawable/corner_bg_dialog_progress"

它就是一个放在res/drawable文件夹下的一个自定义shape。

corner_bg_dialog_progress.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/light_black" />
    <corners android:radius="@dimen/corner_radius_one" />
</shape>

最后,实现一个Dialog并加载这个dialog_progress.xml布局,显示出来即可。

在需要提示进度的地方,showProgressDialog。在结束时closeProgressDialog。

override fun showProgressDialog(msg: String) {
                if (dialogProgress == null) {
                    dialogProgress = DialogProgress(mPresentation!!.context, activity.getString(R.string.loading), false)
                }
                dialogProgress!!.setMessage(msg)
                dialogProgress!!.show()
            }

override fun closeProgressDialog() {
                dialogProgress?.dismiss()
            }


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/30978.html

值为  进度  自定义  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 全文神女老婆,却在背地承欢作乐全局(顾尘傅雅宁)列表_全文神女老婆,却在背地承欢作乐全局
  • (番外)+(全书)此去经年人未还全书+番外+后续免费下载_(沈青禾霍沉洲)此去经年人未还全书+番外+后续列表_笔趣阁(沈青禾霍沉洲)
  • 完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续列表_完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续(林梦婉)
  • 妻子辱我爸受贿自杀,我掏出一等军功章节选推荐_[陈素云辰朋友]小说精彩章节分享
  • 全书浏览苔藓爬满旧日诺言新上(顾砚廷慕晚夏)_苔藓爬满旧日诺言新上(顾砚廷慕晚夏)全书结局
  • 顾尘傅雅宁(神女老婆,却在背地承欢作乐+后续+结局)结局_(顾尘傅雅宁神女老婆,却在背地承欢作乐+后续+结局全书结局)结局列表_笔趣阁(顾尘傅雅宁)
  • 「老婆怀上助理的孩子后,助理要求我净身出户」章节限时抢先看‌_「黄秋雅秋雅姐刘嘉铭」后续完结版
  • 此去经年人未还,沈青禾霍沉洲_此去经年人未还,沈青禾霍沉洲
  • 我爸娶了九十九个媳妇都死了,这次准备娶我的女同学小说精彩章节免费试读_[小梅娶媳妇孤儿]全文免费在线阅读
  • 此去经年人未还结局+番外文章简述(沈青禾霍沉洲)列表_此去经年人未还结局+番外文章简述
  • 完结文寻你寻不到归期结局+完结列表_完结文寻你寻不到归期结局+完结(姜昭意盛西)
  • 江以蓁的潮起时问归期高分佳作江以蓁秦司礼全书在线

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1