Android自定义ProgressDialog

Android自定义ProgressDialog我们在开发Android上应用程序时,有很多时候会遇到“延时”等待的情况,例如数据加载时,尤其是在联网的时候,请求网络会有个等待时间,在这个等待的时间里需要给用户一个友好的提示,提示用户现在正在做什么操作,需要耐心等待等等,这时一个进度对话框就可以解决。Android提供给我们一个很好的控件叫ProgressDialog,用来创建自定义信息以及一些相关操作,唯一不好的一点就是Android原生控件给我一种一如既往的单调和丑陋,下面是原生ProgressDialog的源码以及效果

大家好,又见面了,我是你们的朋友全栈君。

       转载请注明出处:http://blog.csdn.net/allen315410/article/details/41699063 

       我们在开发Android上应用程序时,有很多时候会遇到“延时”等待的情况,例如数据加载时,尤其是在联网的时候,请求网络会有个等待时间,在这个等待的时间里需要给用户一个友好的提示,提示用户现在正在做什么操作,需要耐心等待等等,这时一个进度对话框就可以解决。Android提供给我们一个很好的控件叫ProgressDialog,用来创建自定义信息以及一些相关操作,唯一不好的一点就是Android原生控件给我一种一如既往的单调和丑陋,下面是原生ProgressDialog的源码以及效果:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setMessage("登录中……");
		dialog.show();

	}
}

Android自定义ProgressDialog

       源码极其简单,效果也极其简陋,怎么看怎么不爽。那么怎样让这个ProgressDialog看起来爽点呢?其实不妨做一个自定义的ProgressDialog,先看一下自定义ProgressDialog的效果吧!

Android自定义ProgressDialog

好了,看上去不错吧!下面开始一步一步来实现!

1,自定义ProgressDialog的布局。

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

    <ImageView
        android:id="@+id/spinnerImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/spinner" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Message"
        android:textColor="#FFFFFF" />

</LinearLayout>

2,滚动条的背景设置。

上面的XML布局中可以看到滚动条是一个ImageView,需要给ImageView设置一个动态的背景,那这个动态的背景该怎么办呢?其实就是给ImageView一个动画背景,给出一定数量的图片,在动画中按一定时间匀速切换图片即可,图片资源如下:

Android自定义ProgressDialog

在res/anim文件夹建立这样一个动画集spinner.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/spinner_0"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_1"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_2"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_3"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_4"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_5"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_6"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_7"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_8"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_9"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_10"
        android:duration="60"/>
    <item
        android:drawable="@drawable/spinner_11"
        android:duration="60"/>
</animation-list>

然后给整个的ProgressDialog设置一个背景色progress_custom_bg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ff404040" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

然后还需要给自定义ProgressDialog设置一个风格,在res/value/style.xml下这样定义:

<!-- 自定义Dialog -->
<style name="Custom_Progress" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
</style>

3,接下来进入重点,我们通过代码来构建一个自定义的ProgressDialog,具体做法就是自定义类继承Dialog:

package com.example.myexample;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomProgress extends Dialog {
	public CustomProgress(Context context) {
		super(context);
	}

	public CustomProgress(Context context, int theme) {
		super(context, theme);
	}

	/**
	 * 当窗口焦点改变时调用
	 */
	public void onWindowFocusChanged(boolean hasFocus) {
		ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
		// 获取ImageView上的动画背景
		AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
		// 开始动画
		spinner.start();
	}

	/**
	 * 给Dialog设置提示信息
	 * 
	 * @param message
	 */
	public void setMessage(CharSequence message) {
		if (message != null && message.length() > 0) {
			findViewById(R.id.message).setVisibility(View.VISIBLE);
			TextView txt = (TextView) findViewById(R.id.message);
			txt.setText(message);
			txt.invalidate();
		}
	}

	/**
	 * 弹出自定义ProgressDialog
	 * 
	 * @param context
	 *            上下文
	 * @param message
	 *            提示
	 * @param cancelable
	 *            是否按返回键取消
	 * @param cancelListener
	 *            按下返回键监听
	 * @return
	 */
	public static CustomProgress show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {
		CustomProgress dialog = new CustomProgress(context, R.style.Custom_Progress);
		dialog.setTitle("");
		dialog.setContentView(R.layout.progress_custom);
		if (message == null || message.length() == 0) {
			dialog.findViewById(R.id.message).setVisibility(View.GONE);
		} else {
			TextView txt = (TextView) dialog.findViewById(R.id.message);
			txt.setText(message);
		}
		// 按返回键是否取消
		dialog.setCancelable(cancelable);
		// 监听返回键处理
		dialog.setOnCancelListener(cancelListener);
		// 设置居中
		dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		// 设置背景层透明度
		lp.dimAmount = 0.2f;
		dialog.getWindow().setAttributes(lp);
		// dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		dialog.show();
		return dialog;
	}
}

在Activity中引用自定义ProgressDialog:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	    CustomProgress.show(this, "登录中...", true, null);

	}
}

好了,整个流程结束,我们简单的自定义进度对话框就做好了,效果在上面已经截图了,这里不再贴出。里面的代码我就不解释了,都比较简单而且有一些注释。喜欢的朋友,请在下方点击下载源码。

源码请在这里下载

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159126.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何选择tomcat版本「建议收藏」

    如何选择tomcat版本「建议收藏」

    2022年8月30日
    0
  • 抓住每一次学习的机会

    抓住每一次学习的机会抓住每一次学习的机会

    2022年4月24日
    37
  • 闭包常见面试题_闭包的特点

    闭包常见面试题_闭包的特点理解:什么是闭包?1.密闭的容器,类似set/map容器,用来存储数据2.闭包是一个对象,存放数据的格式:key:value闭包形成的条件1.函数嵌套2.内部函数引用外部函数functionfun(){varcount=1;functionfun2(){//条件1:函数嵌套//形成条件2:内部函数引用外部函数console.log(count);}}fun();已经形成了闭包,以键值对的形式保存数据在外部.

    2022年8月30日
    0
  • 网上的腾讯php面试题 (有答案版本)

    网上的腾讯php面试题 (有答案版本)

    2021年11月5日
    40
  • 软件激活成功教程教程2[通俗易懂]

    软件激活成功教程教程2[通俗易懂]第四章–调试器及相关工具入门在写这章之前,我看了一下看雪以往的教程。本来想参考一下,可忽然发现,写这样的一章,是一件非常愚蠢的事情,因为我觉的关于这些工具的使用教程。看雪教程中已经写的够详细的了,我并不认为你会看不懂。所以我不想做浪费时间的人,本章就此搁浅。推荐看《CrackTutorial2001》,推荐看《看雪论坛精华一、二、三、四》,推荐看《加密与解密--软件保护技术及完全解决方案》,

    2022年6月18日
    36
  • MySQL索引原理及BTree(B-/+Tree)结构详解「建议收藏」

    MySQL索引原理及BTree(B-/+Tree)结构详解「建议收藏」目录摘要数据结构及算法基础索引的本质B-Tree和B+TreeB-TreeB+Tree带有顺序访问指针的B+Tree为什么使用B-Tree(B+Tree)主存存取原理磁盘存取原理局部性原理与磁盘预读B-/+Tree索引的性能分析MySQL索引实现MyISAM索引实现InnoDB索引实现索引使用策略及优化示例数据库最左前缀原理与…

    2022年6月24日
    25

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号