分享好友 移动开发首页 频道列表

【Android 自定义 View 实战】之自定义项目通用的加载等待对话框 LoadingDialog

Android开发  2016-10-31 12:580

在平时的Android开发中,我们难免会遇到像登陆.注册.获取数据这样的操作,而用的的网络状况不同,导致操作需要等待一定的时间,那么为了友好期间,我们需要给用户提供一个在操作完之后的一个友好的等待界面,而Android系统自带的等待对话框比较难看,而且根据项目的不同,产品经理也会有不同的要求,或者客户有特殊的要求。所以,需要我们去自己自定义一个加载等待对话框。

下面我们来做一个给中石油做的一个App中的加载等待对话框。

1.先看效果图

2.准备资源文件

  • 等待对话框的背景图片->loading_bg.9.png
     【Android 自定义 View 实战】之自定义项目通用的加载等待对话框 LoadingDialog
  • 等待对话框的旋转动画图片->loading.png
     【Android 自定义 View 实战】之自定义项目通用的加载等待对话框 LoadingDialog

3.进度条动画旋转实现

  • custom_progress_draw.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/loading"
    android:pivotX="50%"
    android:pivotY="50%"
    />

4.自定义等待对话框的布局文件

  • loading.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:background="@mipmap/loading_bg">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|bottom"
            android:orientation="vertical">

            <ProgressBar
                style="@android:style/Widget.ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminateDrawable="@drawable/custom_progress_draw" />


            <TextView
                android:layout_marginBottom="20dp"
                android:id="@+id/tv_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="正在登录"
                android:layout_marginTop="20dp"
                android:textColor="#fff" />
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

5.自定义对话框代码:

package cn.bluemobi.dylan;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.widget.TextView;

/**
 * 自定义加载进度对话框
 * Created by Dylan on 2016-10-28.
 */

public class LoadingDialog extends Dialog {
    private TextView tv_text;

    public LoadingDialog(Context context) {
        super(context);
        /**设置对话框背景透明*/
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.loading);
        tv_text = (TextView) findViewById(R.id.tv_text);
        setCanceledOnTouchOutside(false);
    }

    /**
     * 为加载进度个对话框设置不同的提示消息
     *
     * @param message 给用户展示的提示信息
     * @return build模式设计,可以链式调用
     */
    public LoadingDialog setMessage(String message) {
        tv_text.setText(message);
        return this;
    }
}

6.用法:一句代码搞定

  • MainActivity中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new LoadingDialog(this).setMessage("正在加载...").show();
    
    }

    7.再次看效果

    8. GitHub地址https://github.com/linglongxin24/LoadingDialog

查看更多关于【Android开发】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Supporting Multiple Screens
术语和概念Screen size 屏幕尺寸又称「屏幕大小」,是屏幕对角线的物理尺寸。单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。Resolution 屏幕分辨率屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440

0评论2017-02-05363

Android插件化(4):OpenAtlasの插件的卸载与更新
如果看过我的前两篇博客Android插件化(2):OpenAtlas插件安装过程分析和Android插件化(3):OpenAtlas的插件重建以及使用时安装,就知道在插件的安装过程中OpenAtlas做了哪些事,那么插件的卸载就只需要把持久化和内存中的内容移除即可。1.插件的卸载插件卸载的

0评论2017-02-05229

个人简历
吴朝晖/男/1993.1本科/南京师范大学中北学院信息系工作年限:1年以内技术博客:wuzhaohui026.github.ioGitHub:https://github.com/wuzhaohui026期望职位:Android开发(初级Android工程师)期望薪资:税前月薪5.5k~7k期望城市:常州工作经历常州慧展信息科技有

0评论2017-02-05126

Android插件化(五):OpenAtlasの四大组件的Hack
引言到目前为止,我们已经分析了OpenAtlas中插件的安装,卸载,更新,以及安装好插件之后组件类的加载过程,但是对于这些是如何引发的还不知道,比如,在宿主的一个Activit中调用startActivity()跳转到插件中的一个Activity,如何判断这个Activity在的插件是否

0评论2017-02-0598

更多推荐