本帖最后由 liu 于 2018-12-24 14:07 编辑
帧动画
逐帧动画:Frame Animation 将多张图片按照顺序依次显示
用法:
一、使用xml实现
1、在res/drawable下新建文件,如anim.xml:
根标签是animation-list,里面有一个或者多个item节点组成,oneshot属性表示是否只播放一次,true表示只会播放一次,false表示一直循环播放anim.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:drawable="@drawable/img1"
android:duration="100" />
<item
android:drawable="@drawable/img2"
android:duration="100" />
<item
android:drawable="@drawable/img3"
android:duration="100" />
<item
android:drawable="@drawable/img4"
android:duration="100" />
<item
android:drawable="@drawable/img5"
android:duration="100" />
<item
android:drawable="@drawable/img6"
android:duration="100" />
<item
android:drawable="@drawable/img7"
android:duration="100" />
<item
android:drawable="@drawable/img8"
android:duration="100" />
<item
android:drawable="@drawable/img9"
android:duration="100" />
<item
android:drawable="@drawable/img10"
android:duration="100" />
<item
android:drawable="@drawable/img11"
android:duration="100" />
<item
android:drawable="@drawable/img12"
android:duration="100" />
<item
android:drawable="@drawable/img13"
android:duration="100" />
<item
android:drawable="@drawable/img14"
android:duration="100" />
<item
android:drawable="@drawable/img15"
android:duration="100" />
<item
android:drawable="@drawable/img16"
android:duration="100" />
<item
android:drawable="@drawable/img17"
android:duration="100" />
<item
android:drawable="@drawable/img18"
android:duration="100" />
<item
android:drawable="@drawable/img19"
android:duration="100" />
<item
android:drawable="@drawable/img20"
android:duration="100" />
</animation-list>
activity.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/anim" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_begin"
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:layout_marginLeft="50dp"
android:text="结束" />
</LinearLayout>
</LinearLayout>
MainAcitity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = (ImageView) findViewById(R.id.iv);
Button btn_begin = (Button) findViewById(R.id.btn_begin);
final Button btn_stop = (Button) findViewById(R.id.btn_stop);
final AnimationDrawable drawable = (AnimationDrawable) iv.getBackground();
btn_begin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!drawable.isRunning()) {
//开启
drawable.start();
}
}
});
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawable.isRunning()) {
//停止
drawable.stop();
}
}
});
}
}
二、java代码实现:
布局和上面一样
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
final ImageView iv = (ImageView) findViewById(R.id.iv);
Button btn_begin = (Button) findViewById(R.id.btn_begin);
final Button btn_stop = (Button) findViewById(R.id.btn_stop);
final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
for (int i = 1; i < 21; i++) {
int id = getResources().getIdentifier("img" + i, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
anim.addFrame(drawable, 200);
}
anim.setOneShot(false);
iv.setImageDrawable(anim);
btn_begin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!anim.isRunning()) {
//开启
anim.start();
}
}
});
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (anim.isRunning()) {
//停止
anim.stop();
}
}
});
}
}
效果图:
|