Android Frame动画demo
Android动画介绍:Android为我们提供了两种动画实现,Frame和Tween。
两者之间的区别:
1.Frame动画:就像放电影一样,是通过预先做好的图片进行连续播放从而形成动画效果
2.Tween动画:通过对图片设置平移、缩放、旋转、改变透明度等方式来显示动画效果
本节仅讲Frame动画,
Frame动画是通过AnimationDrawable来实现的,它提供了start()和stop()两个方法,对播放的动画进行控制,一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。
废话就不说了,下面将贴出该例子的完整代码,供大家测试使用:
一、FrameActivity
package com.yw.myapiupdate.frame; import com.yw.myapiupdate.R; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; /** * * 实现动画轮询播放 * @author yw-tony * */ public class FrameActivity extends Activity implements OnClickListener{ private Button btn_start; private Button btn_end; private ImageView iv; private AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame_layout); initViews(); } private void initViews(){ btn_start = (Button)findViewById(R.id.frame_btn_start); btn_end = (Button)findViewById(R.id.frame_btn_end); iv = (ImageView)findViewById(R.id.frame_iv); btn_start.setOnClickListener(this); btn_end.setOnClickListener(this); this.ad = (AnimationDrawable)iv.getBackground(); } private void startAnimation(){ this.ad.start(); } private void stopAnimation(){ this.ad.stop(); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.frame_btn_start: startAnimation(); break; case R.id.frame_btn_end: stopAnimation(); break; } } }
与之对应的xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/frame_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/framedrawable"/> <Button android:id="@+id/frame_btn_start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="start"/> <Button android:id="@+id/frame_btn_end" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="end"/> </LinearLayout>
二、设置动画的xml文件
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false"> <item apk:drawable="@drawable/anim_1" apk:duration="200" /> <item apk:drawable="@drawable/anim_2" apk:duration="200" /> <item apk:drawable="@drawable/anim_3" apk:duration="200" /> <item apk:drawable="@drawable/anim_4" apk:duration="200" /> <item apk:drawable="@drawable/anim_5" apk:duration="200" /> <item apk:drawable="@drawable/anim_6" apk:duration="200" /> </animation-list>
下面是源代码以及资源文件的下载地址:
https://files.cnblogs.com/tony-yang-flutter/anni.zip
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探