一. Frame Animation(逐帧动画):
逐帧动画(Frame-by-frame Animations)从字面上理解就是一帧挨着一帧的播放图片,就像放电影一样。和补间动画一样可以通过xml实现也可以通过java代码实现。
由于没有逐帧图片,所以随便用了几张图片,名字为one1,one2,one3.明白逐帧动画的使用就行,不用太在意图片的缺陷。
(1)xml方式实现:
在drawable下新建frame_layout.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="@mipmap/one1" android:duration="300"/> <item android:drawable="@mipmap/one2" android:duration="300"/> <item android:drawable="@mipmap/one3" android:duration="300"/> </animation-list>
使用animation-list标签
oneshot : 逐帧动画播放的次数,false为循环播放,true为播放一次
item : 存放一个帧动画图片
drawable : 帧动画图片
duration : 这张帧动画播放的持续时间
将frame_layout.xml添加到view中:
<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:src="@drawable/frame_layout"/>
java代码中加载设置启动:
AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable(); drawable.start();
需要停止时调用:
AnimationDrawable drawable1 = (AnimationDrawable) imageView.getDrawable(); drawable1.stop();
(2)纯java代码实现:
AnimationDrawable animationDrawable = new AnimationDrawable(); //添加所需动画,并设置持续显示时间 animationDrawable.addFrame(ContextCompat.getDrawable(this, R.mipmap.one1), 300); animationDrawable.addFrame(ContextCompat.getDrawable(this, R.mipmap.one2), 300); animationDrawable.addFrame(ContextCompat.getDrawable(this, R.mipmap.one3), 300); animationDrawable.setOneShot(false); imageView.setImageDrawable(animationDrawable); animationDrawable.start();
需要停止时调用:
AnimationDrawable drawable1 = (AnimationDrawable) imageView.getDrawable(); drawable1.stop();
每天进步一点点!