android 自定义progressDialog实现
我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在android中让 大家最容易想到的就是progressbar或者progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希 望用户能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要progressDialog的黑色框框,感觉 这样跟应用的整体风格不协调,这个时候就考虑了写一个自定义的progressDialog。
在网上搜过很多自定义progressDialog的例子,对着写了下,但是没有任何效果,不知道是自己使用的方法不对还是什么地方出错了。通过不断的查找资料,写了一个简单的自定义progressDialog。先上图看下效果:

1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色。
- <style name="CustomDialog" 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>
- </style>
- <style name="CustomProgressDialog" parent="@style/CustomDialog">
- <item name="android:windowBackground">@android:color/transparent</item>
- <item name="android:windowNoTitle">true</item>
- </style>
2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较接单
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/loadingImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@anim/progress_round"/>
- <TextView
- android:id="@+id/id_tv_loadingmsg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:textSize="20dp"/>
- </LinearLayout>
3.progress_round.xml文件.这个文件为了实现转动的效果,循环显示这些图片。
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/progress_1" android:duration="200"/>
- <item android:drawable="@drawable/progress_2" android:duration="200"/>
- <item android:drawable="@drawable/progress_3" android:duration="200"/>
- <item android:drawable="@drawable/progress_4" android:duration="200"/>
- <item android:drawable="@drawable/progress_5" android:duration="200"/>
- <item android:drawable="@drawable/progress_6" android:duration="200"/>
- <item android:drawable="@drawable/progress_7" android:duration="200"/>
- <item android:drawable="@drawable/progress_8" android:duration="200"/>
- </animation-list>
4.CustomProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。
- /**************************************************************************************
- * [Project]
- * MyProgressDialog
- * [Package]
- * com.lxd.widgets
- * [FileName]
- * CustomProgressDialog.java
- * [Copyright]
- * Copyright 2012 LXD All Rights Reserved.
- * [History]
- * Version Date Author Record
- *--------------------------------------------------------------------------------------
- * 1.0.0 2012-4-27 lxd (rohsuton@gmail.com) Create
- **************************************************************************************/
- package com.lxd.widgets;
- import com.lxd.activity.R;
- import android.app.Dialog;
- import android.content.Context;
- import android.graphics.drawable.AnimationDrawable;
- import android.view.Gravity;
- import android.widget.ImageView;
- import android.widget.TextView;
- /********************************************************************
- * [Summary]
- * TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要
- * [Remarks]
- * TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系.
- *******************************************************************/
- public class CustomProgressDialog extends Dialog {
- private Context context = null;
- private static CustomProgressDialog customProgressDialog = null;
- public CustomProgressDialog(Context context){
- super(context);
- this.context = context;
- }
- public CustomProgressDialog(Context context, int theme) {
- super(context, theme);
- }
- public static CustomProgressDialog createDialog(Context context){
- customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);
- customProgressDialog.setContentView(R.layout.customprogressdialog);
- customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
- return customProgressDialog;
- }
- public void onWindowFocusChanged(boolean hasFocus){
- if (customProgressDialog == null){
- return;
- }
- ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
- AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
- animationDrawable.start();
- }
- /**
- *
- * [Summary]
- * setTitile 标题
- * @param strTitle
- * @return
- *
- */
- public CustomProgressDialog setTitile(String strTitle){
- return customProgressDialog;
- }
- /**
- *
- * [Summary]
- * setMessage 提示内容
- * @param strMessage
- * @return
- *
- */
- public CustomProgressDialog setMessage(String strMessage){
- TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);
- if (tvMsg != null){
- tvMsg.setText(strMessage);
- }
- return customProgressDialog;
- }
- }
5.接下来就是写一个测试activity调用我们的progressDialog了。
- package com.lxd.activity;
- import com.lxd.widgets.CustomProgressDialog;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- public class MainFrame extends Activity {
- private MainFrameTask mMainFrameTask = null;
- private CustomProgressDialog progressDialog = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- mMainFrameTask = new MainFrameTask(this);
- mMainFrameTask.execute();
- }
- @Override
- protected void onDestroy() {
- stopProgressDialog();
- if (mMainFrameTask != null && !mMainFrameTask.isCancelled()){
- mMainFrameTask.cancel(true);
- }
- super.onDestroy();
- }
- private void startProgressDialog(){
- if (progressDialog == null){
- progressDialog = CustomProgressDialog.createDialog(this);
- progressDialog.setMessage("正在加载中...");
- }
- progressDialog.show();
- }
- private void stopProgressDialog(){
- if (progressDialog != null){
- progressDialog.dismiss();
- progressDialog = null;
- }
- }
- public class MainFrameTask extends AsyncTask<Integer, String, Integer>{
- private MainFrame mainFrame = null;
- public MainFrameTask(MainFrame mainFrame){
- this.mainFrame = mainFrame;
- }
- @Override
- protected void onCancelled() {
- stopProgressDialog();
- super.onCancelled();
- }
- @Override
- protected Integer doInBackground(Integer... params) {
- try {
- Thread.sleep(10 * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- startProgressDialog();
- }
- @Override
- protected void onPostExecute(Integer result) {
- stopProgressDialog();
- }
- }
- }
这样我们需要的progressDialog效果就出来了,希望对遇到跟我类似问题的朋友有所帮助,里面有什么写的不对的地方请大家指教。
转载请指明出处:http://blog.csdn.net/rohsuton/article/details/7518031
最后贴上完整代码:自定义ProgressDialog
android 自定义progressDialog实现的更多相关文章
- (转载)Android自定义ProgressDialog进度等待框
Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...
- Android 自定义ProgressDialog
Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框等待网络数据. 不过,既然是为了提高用户体验,我们肯定希望该 ...
- Android自定义类似ProgressDialog效果的Dialog
Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loadin ...
- android 自定义动画
android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...
- Android 之 ProgressDialog用法介绍
布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- Android自定义View 画弧形,文字,并增加动画效果
一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类 B ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
随机推荐
- ubuntu 14.04.02 LTS 启动项误写入 /dev/sda1 (win 7 loader) 修复
问题描述: 在win7下安装Ubuntu14.04,由于启动项 /boot loader 安装位置错误(/dev/sda1 (win 7 loader) )导致无法进入Windows(在GRUB界面能 ...
- Django socketio 安装
如果你还没有安装过 gevent,首先需要安装 libevent, 编译安装 libevent 需要安装 Pyhton 开发库. 在Debain上可以运行如下指令: $ sudo apt-get in ...
- 请教 WINDOWSPHONE 有个人录了个传感器等硬件的视频,并且项目是完全开源的,大家有知道地址的吗?或者叫什么。
请教 WINDOWSPHONE 有个人录了个传感器等硬件的视频,并且项目是完全开源的,大家有知道地址的吗?或者叫什么.
- 同步异步GET和POST请求
1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...
- 『安全科普』WEB安全之渗透测试流程
熟悉渗透流程,攻击会像摆积木一样简单! 0x 01:信息收集 收集网站信息对渗透测试非常重要,收集到的信息往往会让你在渗透中获得意外惊喜. 1. 网站结构 可以使用扫描工具扫描目录,主要扫出网站管理员 ...
- Agri-Net
poj1258:http://poj.org/problem?id=1258 题意:生成树的模板题.简单题. #include<iostream> #include<cstring& ...
- 关于用POI和EXCEL交互的问题
废话不多说,直接通过例子来说明POI的使用: 1.一个创建excel并写入数据的小例子,参照网上的一个例子: public class CreateXL { /** * @param args */ ...
- 关于如何在BCB中使用CodeGuard
作者:深圳虫 来自:深圳虫网本文来自http://www.szbug.com/disparticle.aspID=4 一. 为什么写这篇东西自己在使用BCB5写一些程序时需要检查很多东西,例如内存泄漏 ...
- 2.JQuery AJAX
new ActiveXObject("Microsoft XMLHTTP")是IE中创建XMLHTTPRequest对象的方法.非IE浏览器创建方法是new XmlHttpRequ ...
- Linux Shell脚本Ldd命令原理及使用方法
1.首先ldd不是一个可执行程序,而只是一个shell脚本2.ldd能够显示可执行模块的dependency,其原理是通过设置一系列的环境变量如下:LD_TRACE_LOADED_OBJECTS.LD ...