首先看下要实现的功能:
需求:当加载数据的时候要出现加载数据中...和上部圆要做动画效果
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 ; }
<style name="ProgressDialogStyle" parent="@style/ProgressDialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> </style>
<style name="ProgressDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> </style>
3.要加载的样式布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/progress_round"/> <TextView android:id="@+id/id_tv_loadingmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20dp"/> </LinearLayout>
4.红色部分添加的动画图片(特别提醒一下animation-list在Studio下的anim是找不到的要放在drawable下):
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/progress_1" android:duration="200"/> <item android:drawable="@drawable/progress_2" android:duration="200"/> <item android:drawable="@drawable/progress_3" android:duration="200"/> <item android:drawable="@drawable/progress_4" android:duration="200"/> <item android:drawable="@drawable/progress_5" android:duration="200"/> <item android:drawable="@drawable/progress_6" android:duration="200"/> <item android:drawable="@drawable/progress_7" android:duration="200"/> <item android:drawable="@drawable/progress_8" android:duration="200"/> </animation-list>
5.在自定义的Dialog中执行onWindowFocusChanged方法来执行动画效果
6.在需要的地方添加ProgressDialog即可
ProgressDialog dialog = ProgressDialog.createDialog(this); dialog.setMessage("数据加载中..."); dialog.show();
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦