方式一:
效果图
package com.example.dialog;
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateBehavior="repeat"
android:indeterminateDrawable="@drawable/progressbar_bg"
android:indeterminateOnly="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
drawable下资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress_bar"
android:pivotX="50%"
android:pivotY="50%"
/>
方式二:
效果图:
代码 :
package com.example.dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
public class ShowProgressDialog extends Dialog {
private Context ctt=null;
private static ShowProgressDialog dialog;
public static ShowProgressDialog createDialog(Context context){
if (dialog == null) {
//dialog = new ShowProgressDialog(context);
dialog = new ShowProgressDialog(context,R.style.CustomProgressDialog);//这里 你可以自己定义一个
dialog.setContentView(R.layout.showprogressdialog);
dialog.setCancelable(false);//设置按back键不会取消
dialog.setCanceledOnTouchOutside(false);//设置窗体外部 不可触摸
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;//显示在中间
dialog.onWindowFocusChanged(false);//关键
dialog.show();
return dialog;
}else {
return dialog;
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (dialog==null) {
return;
}else {
ImageView imageView = (ImageView) dialog.findViewById(R.id.loadingImageView);
AnimationDrawable ad= (AnimationDrawable) imageView.getDrawable(); //关键
ad.start();
}
}
//系统提供的 一个参数的
public ShowProgressDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供的 二个参数的
public ShowProgressDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.ctt = context;
}
//系统提供 带三个参数的
protected ShowProgressDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
this.ctt = context;
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:src="@drawable/progress_round" />
</RelativeLayout>
drawable下 资源文件:
<?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_01" android:duration="100"></item>
<item android:drawable="@drawable/loading_02" android:duration="100"></item>
<item android:drawable="@drawable/loading_03" android:duration="100"></item>
<item android:drawable="@drawable/loading_04" android:duration="100"></item>
<item android:drawable="@drawable/loading_05" android:duration="100"></item>
<item android:drawable="@drawable/loading_06" android:duration="100"></item>
<item android:drawable="@drawable/loading_07" android:duration="100"></item>
<item android:drawable="@drawable/loading_08" android:duration="100"></item>
</animation-list>
调用示例:
package com.example.dialog;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends Activity implements GetContent{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hell=(TextView) findViewById(R.id.hell);
show_qishu=(TextView) findViewById(R.id.show_qishu);
show_beishu=(TextView) findViewById(R.id.show_beishu);
hell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Mydialog.createDialog(MainActivity.this).show();
//自定义dialog示例1
//ShowProgressDialog dialog=ShowProgressDialog.createDialog(MainActivity.this);
//dialog.show(); //注意里面我是 封装了show()方法的
//自定义dialog示例 2
ProgressBarUtils.showProgressBar(MainActivity.this);
}
});
}
}