欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android自定义ProgressBar实现漂亮的进度提示框,

来源: 开发者 投稿于  被查看 23708 次 评论:187

Android自定义ProgressBar实现漂亮的进度提示框,


在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()
            }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • android 弹出提示框的使用(图文实例)
  • Android使用Toast显示消息提示框
  • android实现弹出提示框
  • Android仿QQ、微信聊天界面长按提示框效果
  • Android编程之自定义AlertDialog(退出提示框)用法实例
  • Android仿IOS自定义AlertDialog提示框
  • Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用示例
  • Android超实用的Toast提示框优化分享
  • Android中仿IOS提示框的实现方法
  • Android模拟美团客户端进度提示框

用户评论