其他分享
首页 > 其他分享> > Android动画之帧动画,74道高级Android面试合集

Android动画之帧动画,74道高级Android面试合集

作者:互联网

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 {

Animati

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

onDrawable 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:

//停止动画

animationDrawable.stop();

break;

}

}

}

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

以前还在学校的时候写过一个例子Android中帧动画的简单实现

下面再来一次

(1).帧动画通常在XML 资源进行定义,在 <animation-list …/> 标签下使用 <item …/> 子元素标签定义动画的全部帧,并指定各帧的持续时间。

在这里插入图片描述

标签:动画,animationDrawable,decodeResource,addFrame,74,new,Android,getResources
来源: https://blog.csdn.net/m0_65145219/article/details/122141922