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

来源:互联网 发布:php代理跨域 编辑:程序博客网 时间:2024/06/04 18:59

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

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

        

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

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

   

<?xml version="1.0" encoding="utf-8"?><!--    根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画    根标签下,通过item标签对动画中的每一个图片进行声明    android:duration 表示展示所用的该图片的时间长度 --><animation-list    xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false"    >    <item android:drawable="@drawable/black_01" android:duration="150"/>    <item android:drawable="@drawable/black_02" android:duration="150"/>    <item android:drawable="@drawable/black_03" android:duration="150"/>    <item android:drawable="@drawable/black_04" android:duration="150"/>    <item android:drawable="@drawable/black_05" android:duration="150"/>    <item android:drawable="@drawable/black_06" android:duration="150"/>    <item android:drawable="@drawable/black_07" android:duration="150"/>    <item android:drawable="@drawable/black_08" android:duration="150"/>    <item android:drawable="@drawable/black_09" android:duration="150"/>    <item android:drawable="@drawable/black_10" android:duration="150"/>    <item android:drawable="@drawable/black_11" android:duration="150"/>    <item android:drawable="@drawable/black_12" android:duration="150"/></animation-list>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:gravity="center">    <ImageView        android:id="@+id/img_loading"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

程序代码:mainActivity.java

import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView img_loading;    private AnimationDrawable AniDraw;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        img_loading = (ImageView)findViewById(R.id.img_loading);        img_loading.setBackgroundResource(R.anim.imgloading);        AniDraw = (AnimationDrawable)img_loading.getBackground();        AniDraw.start();    }}
效果如下图:


0 2
原创粉丝点击