帧动画,原理是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。因为每一帧的内容都不一样,增加了负荷,输出的文件量也大。
优点:灵活,变现细腻。
缺点:增加了负荷。
首先在网上随便找到一张GIF图片,下载下来,然后打开后,如下
将每一张图片都拖拽到桌面上,然后重命名,在拷贝到工程中的res下得mipmap下。
在drawable下创建drawable resource file,
然后,
然后就可以使用,
MainActivity中:
package com.example.mac.framedemo;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.frameBtn);
btn.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.iv);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.frameBtn:
//播放帧动画
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
break;
}
}
}
MainActivity的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mac.framedemo.MainActivity">
<Button
android:id="@+id/frameBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放帧动画" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="@drawable/frame_my" />
<!--
若使用android:background="@drawable/frame_my",则获取使用
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
-->
</LinearLayout>
drawable下资源文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/my1"
android:duration="80" />
<item
android:drawable="@mipmap/my2"
android:duration="80" />
<item
android:drawable="@mipmap/my3"
android:duration="80" />
<item
android:drawable="@mipmap/my4"
android:duration="80" />
<item
android:drawable="@mipmap/my5"
android:duration="80" />
<item
android:drawable="@mipmap/my6"
android:duration="80" />
<item
android:drawable="@mipmap/my7"
android:duration="80" />
<item
android:drawable="@mipmap/my8"
android:duration="80" />
</animation-list>
本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!