Android动画之帧动画

2.利用 xml 实现帧动画(开发中通常使用这种方法实现帧动画)

1.利用 Java 代码实现帧动画

在这里插入图片描述

源代码如下:

activity_main.xml

<LinearLayout 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”

android:orientation=“vertical”

tools:context=“.MainActivity”>

<Button

android:id=“@+id/btn_start”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“开启” />

<Button

android:id=“@+id/btn_stop”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“停止” />

<ImageView

android:id=“@+id/iv_animation”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content” />

MainActivity.java文件如下

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

AnimationDrawable animationDrawable;

private Button btn_start;

private Button btn_stop;

private ImageView iv_animation;//java的形式实现

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

btn_start = (Button) findViewById(R.id.btn_start);

btn_stop = (Button) findViewById(R.id.btn_stop);

iv_animation = (ImageView) findViewById(R.id.iv_animation);

btn_start.setOnClickListener(this);

btn_stop.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_start:

//1.实现帧动画的类(相当于一本空白的小人书)

animationDrawable = new AnimationDrawable();

//2.为帧动画添加内容(在小人书里添加内容)

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim1)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim2)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim3)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim4)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim5)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim6)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim7)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim8)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim9)), 100);

animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim10)), 100);

//3.将帧动画设置给View做背景

iv_animation.setBackground(animationDrawable);

//其他操作,如,设置只执行一次

animationDrawable.setOneShot(true);

//4.开启动画(相当于翻书)

animationDrawable.start();

break;

case R.id.btn_stop:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值