使用Animation-list实现等待旋转圆圈动画

  我们在做网络交互或是从服务器提取数据的时候,常常会给出一个等待的动画,就是一个小圆圈转啊转的。它的使用很简单,我们只要使用Animation-list用三,五行代码,就可以搞定了。

         首先,我们要找几个转圈的图片,注意这几个图片是有讲究的,就是它们正好可以拼接成一个转圈的动画,如下图

         

    这个网上有很多,大家可以自行去找一下

    下面看代码,首先,建立anim文件夹,然后在里面建:imgloading.xml

   

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.     根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画  
  4.     根标签下,通过item标签对动画中的每一个图片进行声明  
  5.     android:duration 表示展示所用的该图片的时间长度  
  6.  -->  
  7. <animation-list  
  8.     xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     android:oneshot="false"  
  10.     >  
  11.     <item android:drawable="@drawable/black_01" android:duration="150"/>  
  12.     <item android:drawable="@drawable/black_02" android:duration="150"/>  
  13.     <item android:drawable="@drawable/black_03" android:duration="150"/>  
  14.     <item android:drawable="@drawable/black_04" android:duration="150"/>  
  15.     <item android:drawable="@drawable/black_05" android:duration="150"/>  
  16.     <item android:drawable="@drawable/black_06" android:duration="150"/>  
  17.     <item android:drawable="@drawable/black_07" android:duration="150"/>  
  18.     <item android:drawable="@drawable/black_08" android:duration="150"/>  
  19.     <item android:drawable="@drawable/black_09" android:duration="150"/>  
  20.     <item android:drawable="@drawable/black_10" android:duration="150"/>  
  21.     <item android:drawable="@drawable/black_11" android:duration="150"/>  
  22.     <item android:drawable="@drawable/black_12" android:duration="150"/>  
  23. </animation-list>  

activity_main.xml

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     tools:context=".MainActivity"  
  5.     android:gravity="center">  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/img_loading"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>  

程序代码:mainActivity.java

[java]  view plain  copy
  1. import android.graphics.drawable.AnimationDrawable;  
  2. import android.os.Bundle;  
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.widget.ImageView;  
  5.   
  6. public class MainActivity extends AppCompatActivity {  
  7.   
  8.     private ImageView img_loading;  
  9.   
  10.     private AnimationDrawable AniDraw;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         img_loading = (ImageView)findViewById(R.id.img_loading);  
  18.         img_loading.setBackgroundResource(R.anim.imgloading);  
  19.         AniDraw = (AnimationDrawable)img_loading.getBackground();  
  20.         AniDraw.start();  
  21.     }  
  22.   
  23.   
  24. }  
效果如下图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值