package xena.act;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout linearLayout;
private AnimationDrawable anim;
private Button start_btn, stop_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.start_btn = (Button) this.findViewById(R.id.start_btn);
this.stop_btn = (Button) this.findViewById(R.id.stop_btn);
this.start_btn.setOnClickListener(this);
this.stop_btn.setOnClickListener(this);
this.linearLayout = (LinearLayout) this.findViewById(R.id.linearLayout);
//获取动画 3
this.anim = (AnimationDrawable) this.linearLayout.getBackground();
//设置播次数,不设置默认为false 4
this.anim.setOneShot(false);//设置播放次数 false为循环播放, true为只播放一次
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.start_btn) {//开始播放动画
this.anim.start();
//得到动画播放一次的时间=每张图片的停留时间之和+代码执行时间
//定时执行complete方法
}else if(v.getId() == R.id.stop_btn) {//停止播放动画
this.anim.stop();
}
}
public void complete() {
Toast.makeText(this, "播放完毕", Toast.LENGTH_LONG).show();
}
}
<pre name="code" class="html"><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"
android:id="@+id/linearLayout"
android:background="@drawable/breath_anim"
>
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放" />
<Button
android:id="@+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放" />
</LinearLayout>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- android:drawable 引入系统资源图片 -->
<!-- android:duration 每张图片的显示时间 单位毫秒 -->
<item
android:drawable="@drawable/breath_0001"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0002"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0003"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0004"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0005"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0006"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0007"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0008"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0009"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0010"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0011"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0012"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0013"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0014"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0015"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0016"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0017"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0018"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0019"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0020"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0021"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0022"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0023"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0024"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0025"
android:duration="60"/>
<item
android:drawable="@drawable/breath_0026"
android:duration="60"/>
</animation-list>
/***************************************************开辟子线程设置监听***************************************************************************************************************/
package xena.act;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout linearLayout;
private AnimationDrawable anim;
private Button start_btn, stop_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.start_btn = (Button) this.findViewById(R.id.start_btn);
this.stop_btn = (Button) this.findViewById(R.id.stop_btn);
this.start_btn.setOnClickListener(this);
this.stop_btn.setOnClickListener(this);
this.linearLayout = (LinearLayout) this.findViewById(R.id.linearLayout);
//设置动画背景
this.linearLayout.setBackgroundResource(R.drawable.breath_anim);
//获取动画 3
this.anim = (AnimationDrawable) this.linearLayout.getBackground();
//设置播次数,不设置默认为false 4
this.anim.setOneShot(true);//设置播放次数 false为循环播放, true为只播放一次
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.start_btn) {//开始播放动画
this.anim.start(); //5
int time_totle = 150;//表示动画播放时间
//得到动画播放一次的时间=每张图片的停留时间之和+代码执行时间
int item_totle = this.anim.getNumberOfFrames();
for(int index=0; index<item_totle; index++) {
time_totle += this.anim.getDuration(index);
}
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {//子线程
complete();
timer.cancel();
}
}, time_totle);
//定时执行complete方法
}else if(v.getId() == R.id.stop_btn) {//停止播放动画
this.anim.stop(); //5
}
}
public void complete() {//子线程
Looper.prepare();
Toast.makeText(this, "播放完毕", Toast.LENGTH_LONG).show();
this.anim.stop();
Looper.loop();
}
}
汤姆猫例子
汤姆猫实例图片以及XML
package xena.act;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
/**
this.anim.getNumberOfFrames();//返回帧动画的帧数,即图片数量
this.anim.getDuration(5);//返回参数表示的下标对应的帧的停止时间
*/
public class MainActivity extends Activity {
private RelativeLayout relativeLayout;
private AnimationDrawable anim;//帧动画对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.relativeLayout = (RelativeLayout) this.findViewById(R.id.myRelativeLayout);
this.relativeLayout.setBackgroundResource(R.drawable.breath);//设置帧动画
this.anim = (AnimationDrawable) this.relativeLayout.getBackground();//取出帧动画
this.anim.setOneShot(false);//设置为循环播放
this.anim.start();//开始播放
}
public void eye(View v) {//铵钮监听方法
this.play(R.drawable.fart);
}
public void dz(View v) {//铵钮监听方法
this.play(R.drawable.poke_left);
}
//放放
public void play(int resID) {
this.anim.stop();
this.relativeLayout.setBackgroundResource(resID);//设置帧动画
this.anim = (AnimationDrawable) this.relativeLayout.getBackground();//取出帧动画
this.anim.setOneShot(true);
this.anim.start();
int time = 150;
for(int i=0; i<this.anim.getNumberOfFrames(); i++) {
time = time + this.anim.getDuration(i);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {//延迟time毫秒之后触发run方法的执行
@Override
public void run() {//主线程
MainActivity.this.anim.stop();
MainActivity.this.relativeLayout.setBackgroundResource(R.drawable.breath);//设置帧动画
MainActivity.this.anim
= (AnimationDrawable) MainActivity.this.relativeLayout.getBackground();//取出帧动画
MainActivity.this.anim.setOneShot(false);//设置为循环播放
MainActivity. this.anim.start();//开始播放
}
}, time);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/myRelativeLayout"
>
<Button
android:onClick="eye"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="眼睛"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#ffff0000"
android:id="@+id/button" />
<Button
android:onClick="dz"
android:background="#ffff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="肚子"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/button"
android:layout_marginBottom="117dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- android:duration 每张图片的显示时间 单位毫秒 -->
<item
android:drawable="@drawable/breath_0001"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0002"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0003"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0004"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0005"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0006"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0007"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0008"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0009"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0010"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0011"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0012"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0013"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0014"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0015"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0016"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0017"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0018"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0019"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0020"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0021"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0022"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0023"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0024"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0025"
android:duration="30"/>
<item
android:drawable="@drawable/breath_0026"
android:duration="30"/>
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- android:drawable 引入系统资源图片 -->
<!-- android:duration 每张图片的显示时间 单位毫秒 -->
<item
android:drawable="@drawable/fart_0001"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0002"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0003"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0004"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0005"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0006"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0007"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0008"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0009"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0010"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0011"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0012"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0013"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0014"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0015"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0016"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0017"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0018"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0019"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0020"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0021"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0022"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0023"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0024"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0025"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0026"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0027"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0028"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0029"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0030"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0031"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0032"
android:duration="30"/>
<item
android:drawable="@drawable/fart_0033"
android:duration="30"/>
<item
android:drawable="@drawable/fart_end_0034"
android:duration="30"/>
<item
android:drawable="@drawable/fart_end_0035"
android:duration="30"/>
<item
android:drawable="@drawable/fart_end_0036"
android:duration="30"/>
<item
android:drawable="@drawable/fart_end_0037"
android:duration="30"/>
<item
android:drawable="@drawable/fart_end_0038"
android:duration="30"/>
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- android:drawable 引入系统资源图片 -->
<!-- android:duration 每张图片的显示时间 单位毫秒 -->
<item
android:drawable="@drawable/poke_belly_left_0001"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0002"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0003"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0004"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0005"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0006"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0007"
android:duration="30"/>
<item
android:drawable="@drawable/poke_belly_left_0008"
android:duration="30"/>
</animation-list>