【Android】帧动画(86/100)

最近几年新的动画方式增加了许多,笔者回顾复习整理一下,以后方便取用
帧动画
Android中的动画 分为三大类,逐帧动画(Frame)以及补间动画(Tween),还有Android 3.0以后引入的属性动画 (Property), 我们本篇文章说一下帧动画,逐帧(Frame)动画是最容易理解的,它要求把动画的过程的每张静态图片都搜集起来,然后由Android来控制依次显示这些静态图片,再利用人眼"视觉暂留"的原理,给用户造成"动画"的错觉。逐帧动画的动画原理和放电影的原理是一样的。
实现上有两种方式:一种是xml,一种是代码。笔者使用代码方式进行。xml方式有兴趣童鞋自己搞搞蛤!
主要代码如下:
private void showPicsAnim() {
animationDrawable=new AnimationDrawable();
int time=300;
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img01),time);
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img02),time);
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img03),time);
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img04),time);
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img05),time);
animationDrawable.addFrame(getDrawable(R.mipmap.ic_img06),time);
animationDrawable.setOneShot(false);//false:循环播放;true:播放一次就停止
imageView.setImageDrawable(animationDrawable);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(animationDrawable.isRunning()){
animationDrawable.stop();
}else{
animationDrawable.start();
}
}
});
animationDrawable.start();
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.anim.FrameAnimActivity">
<ImageView
android:layout_margin="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@mipmap/ic_img01"
android:scaleType="fitXY"
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="300dp"/>
</android.support.constraint.ConstraintLayout>
就到这里啦!
## SmartApi开发工具
全新版本
极小,极快,极限性能的开发调试工具
官网地址:[http://www.smartapi.site/](http://www.smartapi.site/)


## 旧版本不在维护更新

浙公网安备 33010602011771号