安卓开发加载中效果类似progressDialog效果加载动画

本人亲测可用,直接复制粘贴源码就可以用,我也是怕忘记了 留着看

方法如下:

1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景)。

如我要的效果:



2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView):

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/dialog_view"   
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent"  
  7.     android:minHeight="60dp"  
  8.     android:minWidth="180dp"  
  9.     android:gravity="center"  
  10.     android:padding="10dp"  
  11.     android:background="@drawable/loading_bg">  
  12.     <ImageView   
  13.         android:id="@+id/img"  
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"   
  16.         android:src="@drawable/publicloading"  
  17.         />  
  18.     <TextView   
  19.         android:id="@+id/tipTextView"  
  20.         android:layout_width="wrap_content"   
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginLeft="10dp"  
  23.         android:text="数据加载中……" />  
  24. </LinearLayout>  

3.定义一个loadingDialog中imageView转动的动画:loading_animation.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <rotate   
  4.         android:interpolator="@android:anim/linear_interpolator"  
  5.         android:pivotX="50%"  
  6.         android:pivotY="50%"  
  7.         android:fromDegrees="0"  
  8.         android:toDegrees="+360"  
  9.         android:duration="1500"  
  10.         android:startOffset="-1"  
  11.         android:repeatMode="restart"  
  12.         android:repeatCount="-1"/>  
  13. </set>  

4.定义dialog的style.

[java]  view plain  copy
  1. <!-- 自定义loading dialog -->  
  2.     <style name="loading_dialog" parent="android:style/Theme.Dialog">  
  3.         <item name="android:windowFrame">@null</item>  
  4.         <item name="android:windowNoTitle">true</item>   
  5.         <item name="android:windowBackground">@drawable/loading_bg</item>  
  6.         <item name="android:windowIsFloating">true</item>  
  7.         <item name="android:windowContentOverlay">@null</item>  
  8.     </style>  


5.写点创建Dialog的代码,你可以自己封装成一个方法。

[java]  view plain  copy
  1. /** 
  2.      * 得到自定义的progressDialog 
  3.      * @param context 
  4.      * @param msg 
  5.      * @return 
  6.      */  
  7.     public static Dialog createLoadingDialog(Context context, String msg) {  
  8.   
  9.         LayoutInflater inflater = LayoutInflater.from(context);  
  10.         View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view  
  11.         LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局  
  12.         // main.xml中的ImageView  
  13.         ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);  
  14.         TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字  
  15.         // 加载动画  
  16.         Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(  
  17.                 context, R.anim.load_animation);  
  18.         // 使用ImageView显示动画  
  19.         spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
  20.         tipTextView.setText(msg);// 设置加载信息  
  21.   
  22.         Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog  
  23.   
  24.         loadingDialog.setCancelable(false);// 不可以用“返回键”取消  
  25.         loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
  26.                 LinearLayout.LayoutParams.FILL_PARENT,  
  27.                 LinearLayout.LayoutParams.FILL_PARENT));// 设置布局  
  28.         return loadingDialog;  
  29.   
  30.     }  

最后来张整体的效果图:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在 Android 应用使用自定义加载动画,可以通过创建自定义的布局和使用 Animation 类来实现。以下是一个简单的示例,演示如何在 Android 应用自定义加载动画: 首先,在 res/layout 目录下创建一个新的布局文件,例如 custom_loading.xml,其包含自定义加载动画布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/custom_loading_animation" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="@android:color/white"/> </LinearLayout> ``` 其loading_image 是一个自定义加载动画的 ImageView,loading_text 是一个文本视图,用于显示加载消息。 接下来,在 res/drawable 目录下创建一个新的动画文件,例如 custom_loading_animation.xml,其包含自定义加载动画动画,例如: ```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/custom_loading_frame1" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame2" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame3" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame4" android:duration="100" /> </animation-list> ``` 其,custom_loading_frame1、custom_loading_frame2、custom_loading_frame3、custom_loading_frame4 是自定义加载动画的帧。 最后,在你的 Activity ,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 Animation 类将 custom_loading_animation.xml 动画文件加载到 ImageView ,例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_loading, null); ImageView imageView = (ImageView) view.findViewById(R.id.loading_image); Animation animation = AnimationUtils.loadAnimation(this, R.drawable.custom_loading_animation); imageView.startAnimation(animation); ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setCancelable(false); progressDialog.show(); progressDialog.setContentView(view); ``` 其,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 findViewById() 方法获取 loading_image ImageView 对象。然后,使用 AnimationUtils.loadAnimation() 方法将 custom_loading_animation.xml 动画文件加载到 ImageView ,并调用 startAnimation() 方法开始播放动画。最后,将 View 对象设置为 ProgressDialog 的内容视图,调用 show() 方法显示 ProgressDialog

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值