您的位置:首页 > 移动开发 > Android开发

安卓 使用animation-list实现图片的逐帧变换(加载框,刷新框)(19)

2018-03-19 16:37 423 查看
我们在很多地方都会见到,在加载数据的时候,会有个转动的图片,下面配上一行文字:正在载入中,这个转动的图片其实是通过多张图片的每帧逐帧变换得到的,可以使用animation-list实现该功能。
最开始在布局文件中写一个animation-list的xml文件,其中的item为需要加入变换的图片,android:duration代表每个图片显示的时间:<?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/bottom_1"
android:duration="1000"></item>

<item
android:drawable="@drawable/bottom_2"
android:duration="1000"></item>

<item
android:drawable="@drawable/bottom_3"
android:duration="1000"></item>

<item
android:drawable="@drawable/ic_launcher_foreground"
android:duration="1000"></item>
</animation-list>之后我们使用一个layout装入一个ImageView作为显示加载图的界面:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
android:id="@+id/animation1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/loading_dialog_animation"/>

<TextView
android:id="@+id/animation_text"
android:layout_below="@id/animation1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载中"
android:layout_centerHorizontal="true"/>

</RelativeLayout>创建一个Activity实现功能:public class AnimationTestActivity extends AppCompatActivity {

private ImageView animation_image;
private AnimationDrawable animationDrawable;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_layout);

animation_image = findViewById(R.id.animation1);
animationDrawable = (AnimationDrawable) animation_image.getDrawable();
animationDrawable.start();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android