先来两张效果图
自定义对话框:
public class LoadingProgressDialog extends ProgressDialog { private AnimationDrawable mAnimation; private Context mContext; private ImageView mImageView; private String mLoadingTitle; private TextView mLoadingTv; private int mResid; public LoadingProgressDialog(Context context, String content, int id) { super(context); this.mContext = context; this.mLoadingTitle = content; this.mResid = id; setCanceledOnTouchOutside(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initData(); } private void initData() { mImageView.setBackgroundResource(mResid); mAnimation = (AnimationDrawable) mImageView.getBackground(); mImageView.post(new Runnable() { @Override public void run() { mAnimation.start(); } }); mLoadingTv.setText(mLoadingTitle); } public void setContent(String str) { mLoadingTv.setText(str); } private void initView() { setContentView(R.layout.progress_dialog); mLoadingTv = (TextView) findViewById(R.id.loadingTv); mImageView = (ImageView) findViewById(R.id.loadingIv); } }
在layout文件夹下建立progress_dialog.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:gravity="center" android:layout_gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/loadingIv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/loadingTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20sp" /> </LinearLayout>
在res文件夹下建立anim文件夹,然后在里面建立frame.xml
动画是由一张一张的图片逐帧播放的,里面每一个item就是一张图片,动画有多少帧就有多少张图片,有多少图片就有多少item。
<?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 android:drawable="@drawable/loading_02" android:duration="100" /> <item android:drawable="@drawable/loading_03" android:duration="100" /> <item android:drawable="@drawable/loading_04" android:duration="100" /> </animation-list>
用法:
LoadingProgressDialog dialog =new LoadingProgressDialog(MainActivity.this, "正在加载中...",R.anim.frame); //打开 dialog.show(); //隐藏 dialog.dismiss();
Demo地址:https://github.com/yangjie10930/LoadingAnim