有两种实现效果,一种是系统默认效果,一种是自定义效果;默认效果比较简单但效果也比较单调,自定义可以做出比较好看的效果来;

第一种:通过创建一个ProgressDialog对象并设置属性来显示

public class Util {

private static ProgressDialog processDia;

/**

* 显示加载中对话框

*

* @param context

*/

public static void showLoadingDialog(Context context,String message,boolean isCancelable) {

if (processDia == null) {

processDia= new ProgressDialog(context,R.style.dialog);

//点击提示框外面是否取消提示框

processDia.setCanceledOnTouchOutside(false);

//点击返回键是否取消提示框

processDia.setCancelable(isCancelable);

processDia.setIndeterminate(true);

processDia.setMessage(message);

processDia.show();

}

}

/**

* 关闭加载对话框

*/

public static void closeLoadingDialog() {

if (processDia != null) {

if (processDia.isShowing()) {

processDia.cancel();

}

processDia = null;

}

}

}

其中style.xml中需要设置dialog的背景、字体等属性

@null

true

true

true

@android:color/transparent

true

@null

25sp

第二种:首先要准备至少两张图片,图片在切换的过程可以形成动画效果即可;

定义动画切换特效,anim/loading.xml

自定义dialog页面progress_dialog.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="vertical" >

android:id="@+id/loadingIv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@anim/loading"/>

android:id="@+id/loadingTv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/loadingIv"

android:layout_centerHorizontal="true"

android:textSize="20sp"

android:text="正在加载中.." />

创建diglog类MyProgressDialog

public class MyProgressDialog extends ProgressDialog {

private AnimationDrawable mAnimation;

private ImageView mImageView;

private TextView mTextView;

private String loadingTip;

private int resid;

/**

*

* @param context 上下文对象

* @param content 显示文字提示信息内容

* @param id 动画id

*/

public MyProgressDialog(Context context, String content, int resid) {

super(context);

this.loadingTip = content;

this.resid = resid;

//点击提示框外面是否取消提示框

setCanceledOnTouchOutside(false);

//点击返回键是否取消提示框

setCancelable(false);

setIndeterminate(true);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.progress_dialog);

mTextView = (TextView) findViewById(R.id.loadingTv);

mImageView = (ImageView) findViewById(R.id.loadingIv);

mImageView.setBackgroundResource(resid);

// 通过ImageView对象拿到背景显示的AnimationDrawable

mAnimation = (AnimationDrawable) mImageView.getBackground();

mImageView.post(new Runnable() {

@Override

public void run() {

mAnimation.start();

}

});

mTextView.setText(loadingTip);

}

}

最后在activity中进行调用,这样就完成了一个自定义的dialog提示框

public class MainActivity extends Activity {

private MyProgressDialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.btn_start);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

showMyDialog(view);

}

});

}

/**

* 显示对话框

* @param v

*/

public void showMyDialog(View v){

dialog =new MyProgressDialog(this, "正在加载中",R.anim.loading);

dialog.show();

Handler handler =new Handler();

handler.postDelayed(new Runnable() {

@Override

public void run() {

dialog.dismiss();

}

}, 3000);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐