先看图
自定义Dialog
public class LoadingDialog extends Dialog {
private View loading_layout;
private ImageView iv_loading;
public LoadingDialog(Context context) {
this(context, R.style.LoadingDialog);
}
public LoadingDialog(Context context, int themeResId) {
super(context, themeResId);
init();
}
private void init() {
loading_layout = View.inflate(getContext(), R.layout.loading_layout, null);
iv_loading = (ImageView) loading_layout.findViewById(R.id.iv_loading);
this.setCancelable(false);
this.setContentView(loading_layout);
}
@Override
public void show() {
if (iv_loading != null) {
iv_loading.setBackgroundResource(R.drawable.load_list);
AnimationDrawable ad = (AnimationDrawable) iv_loading.getBackground();
if (ad != null && !ad.isRunning()) {
ad.start();
}
}
super.show();
}
/**
* 加载数据完成播放的动画
*/
public void success() {
if (iv_loading != null) {
iv_loading.setBackgroundResource(R.drawable.success_list);
AnimationDrawable ad = (AnimationDrawable) iv_loading.getBackground();
if (ad != null && !ad.isRunning()) {
ad.start();
iv_loading.postDelayed(new Runnable() {
@Override
public void run() {
dismiss();
}
}, 13 * 100);// 这里的时间是由animation-list计算出来的
} // 暂时不知道代码里面怎么去让动画播完后让dialog dismiss掉
}
}
}
- dialog布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="140dp"
android:layout_height="120dp"
android:gravity="center" >
<ImageView
android:id="@+id/iv_loading"
android:layout_width="60dp"
android:layout_height="60dp" />
</RelativeLayout>
</LinearLayout>
- dialog style 文件
<style name="LoadingDialog" parent="@android:Theme.Dialog">
<!-- Dialog的windowFrame框为无 -->
<item name="android:windowFrame">@null</item>
<!-- 是否无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 窗口是否悬浮 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否有覆盖 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 是否半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 窗口背景,默认0.5 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 设置dialog的背景 -->
<item name="android:background">@drawable/loading_bg</item>
<!-- 是否允许对话框外的背景变暗 -->
<item name="android:backgroundDimEnabled">true</item>
</style>
- 帧动画文件
- loading文件
<?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/loading_blue1"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue2"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue5"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue6"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue7"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue8"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue9"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue10"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue11"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue14"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue15"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue16"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue19"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue20"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue23"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue24"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue27"
android:duration="100">
</item>
<item
android:drawable="@drawable/loading_blue28"
android:duration="100">
</item>
</animation-list>
- loading_success文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/success_blue1"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue2"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue3"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue4"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue5"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue6"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue7"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue8"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue9"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue10"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue11"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue12"
android:duration="100">
</item>
<item
android:drawable="@drawable/success_blue13"
android:duration="100">
</item>
</animation-list>
- dialog背景shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="5dp" />
<solid android:color="#223D3D3D" />
</shape>
- MainActivity
public class MainActivity extends Activity {
private LoadingDialog dialog;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
if(msg.what == 0){
dialog.success();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new LoadingDialog(this);
dialog.show();
handler.sendEmptyMessageDelayed(0, 3000);
}
}
需要用到的图片样式