Android用Animation-list实现逐帧动画
先看看效果图
下面是2个动画的xml文件
animation1.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="true" > <item android:drawable="@drawable/icon1" android:duration="250"> </item> <item android:drawable="@drawable/icon2" android:duration="250"> </item> <item android:drawable="@drawable/icon3" android:duration="250"> </item> <item android:drawable="@drawable/icon4" android:duration="250"> </item> <item android:drawable="@drawable/icon5" android:duration="250"> </item> <item android:drawable="@drawable/icon6" android:duration="250"> </item> </animation-list>
animation2.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="true" > <item android:drawable="@drawable/icon6" android:duration="250"> </item> <item android:drawable="@drawable/icon5" android:duration="250"> </item> <item android:drawable="@drawable/icon4" android:duration="250"> </item> <item android:drawable="@drawable/icon3" android:duration="250"> </item> <item android:drawable="@drawable/icon2" android:duration="250"> </item> <item android:drawable="@drawable/icon1" android:duration="250"> </item> </animation-list>
xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/animationIV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:src="@anim/animation1" android:scaleType="center"/> <Button android:id="@+id/buttonA" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" android:onClick="onClick" android:text="顺序显示" /> <Button android:id="@+id/buttonB" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" android:onClick="onClick" android:text="停止" /> <Button android:id="@+id/buttonC" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" android:onClick="onClick" android:text="倒序显示" /> </LinearLayout>
java代码:
package com.example.animationdemo; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.Window; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView animationIV; private AnimationDrawable animationDrawable; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); animationIV = (ImageView) findViewById(R.id.animationIV); } public void onClick(View v) { switch (v.getId()) { case R.id.buttonA: start_animation(R.anim.animation1); break; case R.id.buttonB: stop_animation(); break; case R.id.buttonC: start_animation(R.anim.animation2); break; default: break; } } private void start_animation(int id){ animationIV.setImageResource(id); animationDrawable = (AnimationDrawable) animationIV .getDrawable(); animationDrawable.start(); } private void stop_animation(){ animationDrawable.stop(); } }
csdn下载地址:http://download.csdn.net/detail/wenwei19861106/4856995
分类:
android_动画效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?