我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在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.my_progressdialog.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>
上面的动画是一个逐帧动画,通常采用xml文件定义。如上面代码,定义逐帧动画非常简单,只需要定义动画内容和时间即可。oneshot="false"代表让动画循环播放。
4.MyProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。
package com.maso.wuye.widget;
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;
import com.maso.wuye.R;
public class MyProgressDialog extends Dialog {
private Context context = null;
private static MyProgressDialog customProgressDialog = null;
public MyProgressDialog(Context context) {
super(context);
this.context = context;
}
public MyProgressDialog(Context context, int theme) {
super(context, theme);
}
public static MyProgressDialog createDialog(Context context) {
customProgressDialog = new MyProgressDialog(context,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.my_progressdialog);
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();
}
public MyProgressDialog setTitile(String strTitle) {
return customProgressDialog;
}
public MyProgressDialog setMessage(String strMessage) {
TextView tvMsg = (TextView) customProgressDialog
.findViewById(R.id.id_tv_loadingmsg);
if (tvMsg != null) {
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
可以看到上面重写了onWindowFocusChanged()方法,Activity生命周期中,onStart, onResume, onCreate都不是真正visible的时间点,真正的visible时间点是onWindowFocusChanged()函数被执行时。onWindowFocusChanged指的是这个Activity得到或者失去焦点的时候 就会call。也就是说 如果你想要做一个Activity一加载完毕,就触发什么的话 完全可以用这个。我们在这个方法中启动了动画。
5.接下来就是写一个测试activity调用我们的progressDialog了。
/**
* 开启进度对话框
*/
private void startProgressDialog() {
if (myProgressDialog == null) {
myProgressDialog = MyProgressDialog.createDialog(this);
myProgressDialog.setMessage("");
}
myProgressDialog.show();
}
/**
* 停止进度对话框
*/
private void stopProgressDialog() {
if (myProgressDialog != null) {
myProgressDialog.dismiss();
myProgressDialog = null;
}
}
/**
* 创建异步任务
*
* @author 李小强
*
*/
public class LoginFrameTask extends AsyncTask<Integer, String, Integer> {
/**
* 构造函数
*/
public LoginFrameTask() {
}
/**
* 调用取消时
*/
@Override
protected void onCancelled() {
stopProgressDialog();
super.onCancelled();
}
/**
* 后台线程查询数据
*/
@Override
protected Integer doInBackground(Integer... params) {
try {
Thread.sleep(100 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
/**
* 该方法将在执行后台耗时操作前被调用
*/
@Override
protected void onPreExecute() {
startProgressDialog();
}
/**
* 将doInBackground()的返回值传给该方法
*/
@Override
protected void onPostExecute(Integer result) {
stopProgressDialog();
}
}
上面用到了异步任务,有关异步任务请参阅:http://blog.csdn.net/dawanganban/article/details/19398725
这样我们需要的progressDialog效果就出来了