终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~
在Android中常用AnimationDrawable来实现简单的帧动画。可以通过两种方式来使用它,一种是xml方式,一种是代码方式。推荐使用xml方式。下面分别进行介绍。
一.使用xml方式实现AnimationDrawable
首先在res/drawable/目录下新建一个load_animation.xml文件,文件的根元素是animation-list,表示这是一个animation的列表,根元素下有几个属性,其中android:oneshot表示是否只执行一次,true表示只执行一次。根元素内的item表示帧动画的各个帧,用android:drawable来关联,其中android:duration表示该帧停留的时间。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<item android:drawable="@drawable/loading_1" android:duration="100"/>
<item android:drawable="@drawable/loading_2" android:duration="100"/>
<item android:drawable="@drawable/loading_3" android:duration="100"/>
<item android:drawable="@drawable/loading_4" android:duration="100"/>
</animation-list>
由于AnimationDrawable也是一个Drawable资源,可以通过xml中指定(
android:background="@drawable/load_animation"),也可以在代码中设置:View. setBackgroundResource(resID).然后使用animation.start().开启,使用
animation.stop()停止。
其中对应的Activity代码为:
package com.scu.ad;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageView iv;
private boolean flag;
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.button1);bt.setOnClickListener(this);
iv = (ImageView)findViewById(R.id.imageview);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
if (flag){
flag = false;
ad.stop();
bt.setText("开始");
}else {
flag = true;
ad.start();
bt.setText("暂停");
}
break;
}
}
}
二.通过Java代码方式
package com.scu.ad;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class CodeActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt;
private AnimationDrawable animationDrawable;
private ImageView iv;
private boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code);
bt = (Button)findViewById(R.id.button2);
bt.setOnClickListener(this);
animationDrawable = new AnimationDrawable();
loadImages();
animationDrawable.setOneShot(false);
iv = (ImageView)findViewById(R.id.imageview1);
iv.setBackground(animationDrawable);
}
private void loadImages() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.loading_1);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),R.drawable.loading_2);
Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(),R.drawable.loading_3);
Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(),R.drawable.loading_4);
animationDrawable.addFrame(new BitmapDrawable(bitmap),200);
animationDrawable.addFrame(new BitmapDrawable(bitmap2),200);
animationDrawable.addFrame(new BitmapDrawable(bitmap3),200);
animationDrawable.addFrame(new BitmapDrawable(bitmap4),200);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button2:
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
if (flag){
flag = false;
ad.stop();
bt.setText("开始");
}else {
flag = true;
ad.start();
bt.setText("暂停");
}
break;
}
}
}
其中对应的工程地址是:AnimationDrawable的实例工程