鉴于android自带的progressDialog很丑,这是我们就有必要按照自己需要的去定义一些美观的ProgressDialog。今天教大家写的这个自定义ProgressDialog基本上能满足大多数app了,除非你想做的更漂亮,更炫。不过,原理都差不多。需要大家举一反三。废话不多说,先上效果图:
自定义控件的代码不是很多,代码如下:
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by likai on 2015/1/14.
*/
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;
}
/**
* 当dilog显示在界面上时,执行该方法。。启动动画。
* @param hasFocus
*/
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);
tvMsg.setTypeface(Typeface.SERIF);
if (tvMsg != null){
tvMsg.setTypeface(Typeface.SERIF);
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
然后,我再把ProgressDialog布局文件写出来。customprogressdialog.xml
<?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:orientation="horizontal"
android:gravity="center_vertical"
android:background="@drawable/dialog_bg"
>
<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_marginLeft="@dimen/all_marginLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textSize="@dimen/size"/>
</LinearLayout>
这里的dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9000" />
<corners
<span style="white-space:pre"> </span>android:radius="10dp"
<span style="white-space:pre"> </span>/>
<padding android:left="40dp"
<span style="white-space:pre"> </span>android:top="40dp"
<span style="white-space:pre"> </span>android:right="40dp"
<span style="white-space:pre"> </span>android:bottom="40dp" />
</shape>
ImageView使用的一个帧动画 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>
接着我们设置dialog的style
<style name="CustomProgressDialog" 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>
<span style="white-space:pre"> </span><item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<span style="white-space:pre"> </span><item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
/style>
这样一个简单的自定义dialog就完成了!
在activity使用我们的自定义控件:
public class MyActivity extends Activity {
CustomProgressDialog customProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findViewById(R.id.btn_customerdialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customProgressDialog = CustomProgressDialog.createDialog(MyActivity.this);
customProgressDialog.setMessage("自定义ProgressDialog").show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
customProgressDialog.dismiss();
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:gravity="center"
android:layout_marginTop="30dip"
android:textColor="@android:color/black"
android:text="@string/demo" />
<Button
android:id="@+id/btn_customerdialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="30dip"
android:textColor="@android:color/black"
android:text="Customerdialog" />
</LinearLayout>
大功告成!赶紧运行试试吧。