这篇文章主要介绍“Android如何实现加载圈”,在日常操作中,相信很多人在Android如何实现加载圈问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android如何实现加载圈”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
首先,直接上菊花效果图,这是我直接从项目里面截取下来的。
核心代码
import android.app.Dialog; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class CircularLoading { /** * 显示Dialog * @param context 上下文对象 * @param msg 提示内容 * @param isCancelable 是否可以点击取消 * @return */ public static Dialog showLoadDialog(Context context, String msg, boolean isCancelable) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.circular_loading, null); RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.dialog_bg); // main.xml中的ImageView ImageView loadImage = (ImageView) v.findViewById(R.id.load_iv); TextView pointTextView = (TextView) v.findViewById(R.id.point_tv); // 加载动画 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.rotating_animation); // 使用ImageView显示动画 loadImage.startAnimation(hyperspaceJumpAnimation); pointTextView.setText(msg); Dialog loadingDialog = new Dialog(context, R.style.TransDialogStyle); loadingDialog.setContentView(layout); loadingDialog.setCancelable(isCancelable); loadingDialog.setCanceledOnTouchOutside(false); Window window = loadingDialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.WRAP_CONTENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setGravity(Gravity.CENTER); window.setAttributes(lp); window.setWindowAnimations(R.style.PopWindowAnimStyle); loadingDialog.show(); return loadingDialog; } /** * 关闭dialog */ public static void closeDialog(Dialog mCircularLoading) { if (mCircularLoading != null && mCircularLoading.isShowing()) { mCircularLoading.dismiss(); } } }
布局文件
circular_loading.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" android:minHeight="60dp" android:minWidth="150dp" android:orientation="horizontal" > <ImageView android:id="@+id/load_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:src="@drawable/dialog_loading_img" /> <TextView android:id="@+id/point_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginTop="0dp" android:layout_toRightOf="@+id/load_iv" android:ellipsize="middle" android:singleLine="true" android:textSize="16sp" /> </RelativeLayout>
动画
rotating_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <rotate android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:repeatMode="restart" android:toDegrees="360" />
dialog_show.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0" android:toAlpha="1.0"/> </set>
dialog_hide.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0"/> </set>
Style
</style> <style name="TransDialogStyle" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:backgroundDimAmount">0.5</item> <item name="android:windowFullscreen">true</item> </style> </style> <style name="PopWindowAnimStyle"> <item name="android:windowShowAnimation">@anim/dialog_show</item> <item name="android:windowHideAnimation">@anim/dialog_hide</item> </style>
使用方法
//显示 mCircularLoading = CircularLoading.showLoadDialog(Dvr_Activity_Main.this, "加载中...", true); //关闭 CircularLoading.closeDialog(mCircularLoading);
到此,关于“Android如何实现加载圈”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。