首先看下要实现的功能:

AAffA0nNPuCLAAAAAElFTkSuQmCC

需求:当加载数据的时候要出现加载数据中...和上部圆要做动画效果

1.写一个类继承Dialog;在Dialog做处理:package progressdialog.hanwei.com.dialog;

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 progressdialog.hanwei.com.R;

/**

* Created by 陈苗辉 on 2016/12/14.

*/

public class ProgressDialog extends Dialog{

private Context context;

public ProgressDialog(Context context) {

super(context);

this.context = context;

}

public ProgressDialog(Context context, int themeResId) {

super(context, themeResId);

}

protected ProgressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {

super(context, cancelable, cancelListener);

}

private static ProgressDialog progressDialog;

//创建dialog的样式

public static ProgressDialog createDialog(Context context){

progressDialog = new ProgressDialog(context, R.style.ProgressDialogStyle);

progressDialog.setCanceledOnTouchOutside(false);

progressDialog.setContentView(R.layout.progressdialog);

progressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

return progressDialog ;

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

if (progressDialog == null) {

return;

}

//添加控件  执行帧动画

ImageView imageView = (ImageView) progressDialog.findViewById(R.id.loadingImageView);

AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();

animationDrawable.start();

}

public ProgressDialog setTitle(String title) {

return progressDialog;

}

public ProgressDialog setMessage(String strMessage){

TextView tvMessage = (TextView)progressDialog.findViewById(R.id.id_tv_loadingmsg);

if (tvMessage != null){

tvMessage.setText(strMessage);

}

return progressDialog;

}

}

2.主要在自定义的Dialog中创建样式:public static ProgressDialog createDialog(Context context){

progressDialog = new ProgressDialog(context, R.style.ProgressDialogStyle);

progressDialog.setCanceledOnTouchOutside(false);

progressDialog.setContentView(R.layout.progressdialog);

progressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

return progressDialog ;

}

@android:color/transparent

true

@null

true

@null

@android:style/Animation.Dialog

stateUnspecified|adjustPan

3.要加载的样式布局:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/loadingImageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/progress_round"/>

android:id="@+id/id_tv_loadingmsg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:textSize="20dp"/>

4.红色部分添加的动画图片(特别提醒一下animation-list在Studio下的anim是找不到的要放在drawable下):

xmlns:android="http://schemas.android.com/apk/res/android"

android:oneshot="false">

5.在自定义的Dialog中执行onWindowFocusChanged方法来执行动画效果

6.在需要的地方添加ProgressDialog即可ProgressDialog dialog = ProgressDialog.createDialog(this);

dialog.setMessage("数据加载中...");

dialog.show();

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐