Android(安卓)自定义loading
16lz
2021-01-25
前段时间做项目要求添加一个好看的加载动画,找了很多,都没有实现功能,后来还是自己找到的方法,具体代码实现如下:
先看看效果图
自定义一个loading类:
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 com.ycsoft.android.moviebar.R;public class CustomeProgressDialog extends Dialog { private static CustomeProgressDialog customProgressDialog = null; public CustomeProgressDialog(Context context) { super(context); } public CustomeProgressDialog(Context context, int theme) { super(context, theme); } public static CustomeProgressDialog createDialog(Context context) { customProgressDialog = new CustomeProgressDialog(context, R.style.CustomProgressDialog); customProgressDialog.setContentView(R.layout.customprogressdialog); customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; customProgressDialog.setCanceledOnTouchOutside(false); return customProgressDialog; } public void onWindowFocusChanged(boolean hasFocus) { if (customProgressDialog == null) { return; } ImageView imageView = (ImageView) customProgressDialog .findViewById(R.id.loadingImageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView .getBackground(); animationDrawable.start(); } /** * * [Summary] setTitile 标题 * * @param strTitle * @return * */ public CustomeProgressDialog setTitile(String strTitle) { return customProgressDialog; } /** * * [Summary] setMessage 提示内容 * * @param strMessage * @return * */ public CustomeProgressDialog setMessage(String strMessage) { TextView tvMsg = (TextView) customProgressDialog .findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null) { tvMsg.setText(strMessage); } return customProgressDialog; }}
customprogressdialog.xml布局代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/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>
CustomProgressDialog style代码如下:
progress_round.xml 代码如下:
<?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/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="60"/>animation-list>
图片下载地址图片下载地址
CustomeProgressDialog的使用方法
private CustomeProgressDialog customeProgressDialog;customeProgressDialog = CustomeProgressDialog.createDialog(mContext);//显示加载动画customeProgressDialog.show();//加载动画消失if (customeProgressDialog != null && customeProgressDialog.isShowing()) { customeProgressDialog.dismiss(); }
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- webView头部加进度加载效果
- Android(安卓)SimpleAdapter显示ListView、GridView
- Android(安卓)Audio代码分析9 - AudioTrack::write函数
- Android(安卓)7.1.2(Android(安卓)N) SystemUI--Recents Task 加
- Android常用代码集合
- Android:控件anim动画效果xml文件属性值含义
- Android自定义Dialog没有按设置的宽度显示的问题
- Android(安卓)Activity之间跳转与传值