使用Animation-list实现等待旋转圆圈动画
2016-02-26 10:30
471 查看
我们在做网络交互或是从服务器提取数据的时候,常常会给出一个等待的动画,就是一个小圆圈转啊转的。它的使用很简单,我们只要使用Animation-list用三,五行代码,就可以搞定了。
首先,我们要找几个转圈的图片,注意这几个图片是有讲究的,就是它们正好可以拼接成一个转圈的动画,如下图
这个网上有很多,大家可以自行去找一下
下面看代码,首先,建立anim文件夹,然后在里面建:imgloading.xml
[html] view
plain copy
<?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
[html] view
plain copy
<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
[java] view
plain copy
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();
}
}
效果如下图:
首先,我们要找几个转圈的图片,注意这几个图片是有讲究的,就是它们正好可以拼接成一个转圈的动画,如下图
这个网上有很多,大家可以自行去找一下
下面看代码,首先,建立anim文件夹,然后在里面建:imgloading.xml
[html] view
plain copy
<?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
[html] view
plain copy
<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
[java] view
plain copy
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();
}
}
效果如下图:
相关文章推荐
- 几何着色器
- UIApplicationMain
- iOS 网络
- 小黑小波比.coding的使用
- Qt 5.4错误"qt.network.ssl: QSslSocket: cannot call"
- flume 报File Channel transaction capacity cannot be greater than the capacity of the channel capacity错误
- 陕南是哪里?陕南是哪几个市?
- arm9+linux fl2440之按键驱动 plat_button.c
- linux修改密码
- 如何向外行解释产品经理频繁更改需求为什么会令程序员烦恼?
- elgnairT.120
- [linux学习]centos下mysql的简单使用教程
- android开发之MVC、MVP、MVVM
- 变换反馈
- iOS :笔试题
- mysql子查询
- Java中Return和Finally运行顺序的实现
- 微信IOS的SDK:isWXAppInstalled总是返回NO
- 前期开发心得总结
- 通过url中的hash(#)来记录页面状态,用以返回、跳转到特定状态