建业科技,花茶的搭配,现在的国家主席是谁
前言
在android中,动画animation的实现有两种方式:tween animation(补间动画)和frame animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。
下面我们就来学习下android中逐帧动画的基础知识。
原理 : 人眼的"视觉暂留"
方式 :
1.在java代码中 ( new animationdrawable().addframe(getdrawable(r.drawable.a),200);)
sdk好像要求最低版本必须>=21
2.在xml文件中定义动画资源
效果图如下
代码
1.准备图片资源
将图片资源放在drawable-hdpi目录下
2.在drawable目录下新建animation-list类型文件
anim_frame.xml
<?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/a" android:duration="100"/> <item android:drawable="@drawable/b" android:duration="100"/> <item android:drawable="@drawable/c" android:duration="100"/> </animation-list>
3.在布局文件中,添加imageview
,将其background
属性设置为动画资源xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <button android:id="@+id/btn_start" android:text="开始跳舞" android:textsize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <button android:id="@+id/btn_stop" android:text="结束跳舞" android:textsize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <imageview android:id="@+id/image" android:background="@drawable/anim_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout>
4.在java中,获取动画资源,调用start( )
开启动画,stop( )
停止动画
package com.lyp.frameanim; import android.graphics.drawable.animationdrawable; import android.graphics.drawable.drawable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview; public class mainactivity extends appcompatactivity implements view.onclicklistener{ private button mbtnstart; private button mbtnstop; private imageview mimage; private animationdrawable manim; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // new animationdrawable().addframe(getdrawable(r.drawable.a),200); initview(); manim = (animationdrawable) mimage.getbackground(); } private void initview() { mbtnstart= (button) findviewbyid(r.id.btn_start); mbtnstop= (button) findviewbyid(r.id.btn_stop); mimage= (imageview) findviewbyid(r.id.image); mbtnstart.setonclicklistener(this); mbtnstop.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()){ case r.id.btn_start: manim.start(); break; case r.id.btn_stop: manim.stop(); break; } } }
总结
以上就是这篇文章的全部内容了,希望能对各位android开发者们有所帮助,如果有疑问大家可以留言交流。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论