Android动画:Tom猫
1.导入所有的图片和音频
—图片在drawable目录下,音频在raw目录下,所有的命名都需要小写
2.创建布局
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/breath_relative" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/breath_list" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/list_relative" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <Button android:id="@+id/fart_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="150dp" android:background="@null" android:text="嘿嘿" /> <Button android:id="@+id/poke_belly_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/fart_btn" android:layout_alignParentLeft="true" android:layout_marginBottom="52dp" android:text="左抓" /> <Button android:id="@+id/swipe_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/fart_btn" android:layout_alignParentLeft="true" android:text="打左脸" /> <Button android:id="@+id/swipe_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/fart_btn" android:layout_alignParentRight="true" android:text="打右脸" /> <Button android:id="@+id/poke_belly_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/poke_belly_left" android:layout_alignBottom="@+id/poke_belly_left" android:layout_alignParentRight="true" android:text="右抓" /> <Button android:id="@+id/look_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_toRightOf="@+id/swipe_left" android:text="看" /> <Button android:id="@+id/say_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/look_btn" android:text="说" /> </RelativeLayout> </RelativeLayout>
MainActivity.java
package com.example.qf2; import android.os.Bundle; import android.os.Handler; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.media.AudioManager; import android.media.SoundPool; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener { private RelativeLayout breath_Tom, list_relative; private Button fartbtn, lookbtn, saybtn, poke_belly_left, swipe_left, poke_belly_right, swipe_right; private SoundPool pool; private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取播放动画屏幕 breath_Tom = (RelativeLayout) findViewById(R.id.breath_relative); list_relative = (RelativeLayout) findViewById(R.id.list_relative); // 获取按钮 fartbtn = (Button) findViewById(R.id.fart_btn); lookbtn = (Button) findViewById(R.id.look_btn); saybtn = (Button) findViewById(R.id.say_btn); poke_belly_left = (Button) findViewById(R.id.poke_belly_left); poke_belly_right = (Button) findViewById(R.id.poke_belly_right); swipe_left = (Button) findViewById(R.id.swipe_left); swipe_right = (Button) findViewById(R.id.swipe_right); // 获取胶卷 AnimationDrawable drawable = (AnimationDrawable) breath_Tom.getBackground(); // 播放 drawable.start(); // 实例化音乐播放对象 pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 1); map.put(1, pool.load(MainActivity.this, R.raw.fart003_11025, 1)); map.put(2, pool.load(MainActivity.this, R.raw.cymbal, 1)); map.put(3, pool.load(MainActivity.this, R.raw.p_poke_foot3, 1)); fartbtn.setOnClickListener(this); lookbtn.setOnClickListener(this); poke_belly_left.setOnClickListener(this); poke_belly_right.setOnClickListener(this); swipe_left.setOnClickListener(this); saybtn.setOnClickListener(this); swipe_right.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.fart_btn: // 添加背景 list_relative.setBackgroundResource(R.drawable.fart_list); // 获取胶卷 AnimationDrawable drawable = (AnimationDrawable) list_relative.getBackground(); // 设置执行一次 drawable.setOneShot(true); // 播放 drawable.start(); int time = 0; // 计算动画播放时间 for (int i = 0; i < drawable.getNumberOfFrames(); i++) { time = time + drawable.getDuration(i); } // 延迟time毫秒后执行,将背景设置透明 new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time); pool.play(map.get(1), 1, 1, 1, 0, 1); break; case R.id.look_btn: list_relative.setBackgroundResource(R.drawable.look_list); AnimationDrawable drawable1 = (AnimationDrawable) list_relative.getBackground(); drawable1.setOneShot(true); drawable1.start(); int time1 = 0; for (int i = 0; i < drawable1.getNumberOfFrames(); i++) { time1 = time1 + drawable1.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time1); pool.play(map.get(2), 1, 1, 1, 0, 1); break; case R.id.say_btn: list_relative.setBackgroundResource(R.drawable.talk_list); AnimationDrawable drawable2 = (AnimationDrawable) list_relative.getBackground(); drawable2.setOneShot(true); drawable2.start(); int time2 = 0; for (int i = 0; i < drawable2.getNumberOfFrames(); i++) { time2 = time2 + drawable2.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time2); pool.play(map.get(3), 1, 1, 1, 0, 1); break; case R.id.swipe_left: list_relative.setBackgroundResource(R.drawable.swipe_left_list); AnimationDrawable drawable3 = (AnimationDrawable) list_relative.getBackground(); drawable3.setOneShot(true); drawable3.start(); int time3 = 0; for (int i = 0; i < drawable3.getNumberOfFrames(); i++) { time3 = time3 + drawable3.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time3); // pool.play(map.get(3), 1, 1, 1, 0, 1); break; case R.id.swipe_right: list_relative.setBackgroundResource(R.drawable.swipe_right_list); AnimationDrawable drawable4 = (AnimationDrawable) list_relative.getBackground(); drawable4.setOneShot(true); drawable4.start(); int time4 = 0; for (int i = 0; i < drawable4.getNumberOfFrames(); i++) { time4 = time4 + drawable4.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time4); // pool.play(map.get(3), 1, 1, 1, 0, 1); break; case R.id.poke_belly_right: list_relative.setBackgroundResource(R.drawable.poke_belly_right_list); AnimationDrawable drawable5 = (AnimationDrawable) list_relative.getBackground(); drawable5.setOneShot(true); drawable5.start(); int time5 = 0; for (int i = 0; i < drawable5.getNumberOfFrames(); i++) { time5 = time5 + drawable5.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time5); // pool.play(map.get(2), 1, 1, 1, 0, 1); break; case R.id.poke_belly_left: list_relative.setBackgroundResource(R.drawable.poke_belly_left_list); AnimationDrawable drawable6 = (AnimationDrawable) list_relative.getBackground(); drawable6.setOneShot(true); drawable6.start(); int time6 = 0; for (int i = 0; i < drawable6.getNumberOfFrames(); i++) { time6 = time6 + drawable6.getDuration(i); } new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub list_relative.setBackgroundColor(0x000000); } }, time6); // pool.play(map.get(3), 1, 1, 1, 0, 1); break; default: break; } } }
breath_list.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/breath_0001" android:duration="100"/> <item android:drawable="@drawable/breath_0002" android:duration="100"/> <item android:drawable="@drawable/breath_0003" android:duration="100"/> <item android:drawable="@drawable/breath_0004" android:duration="100"/> <item android:drawable="@drawable/breath_0005" android:duration="100"/> <item android:drawable="@drawable/breath_0006" android:duration="100"/> <item android:drawable="@drawable/breath_0007" android:duration="100"/> <item android:drawable="@drawable/breath_0008" android:duration="100"/> <item android:drawable="@drawable/breath_0009" android:duration="100"/> <item android:drawable="@drawable/breath_0010" android:duration="100"/> <item android:drawable="@drawable/breath_0011" android:duration="100"/> <item android:drawable="@drawable/breath_0012" android:duration="100"/> <item android:drawable="@drawable/breath_0013" android:duration="100"/> <item android:drawable="@drawable/breath_0014" android:duration="100"/> <item android:drawable="@drawable/breath_0015" android:duration="100"/> <item android:drawable="@drawable/breath_0016" android:duration="100"/> <item android:drawable="@drawable/breath_0017" android:duration="100"/> <item android:drawable="@drawable/breath_0018" android:duration="100"/> <item android:drawable="@drawable/breath_0019" android:duration="100"/> <item android:drawable="@drawable/breath_0020" android:duration="100"/> <item android:drawable="@drawable/breath_0021" android:duration="100"/> <item android:drawable="@drawable/breath_0022" android:duration="100"/> <item android:drawable="@drawable/breath_0023" android:duration="100"/> <item android:drawable="@drawable/breath_0024" android:duration="100"/> <item android:drawable="@drawable/breath_0025" android:duration="100"/> <item android:drawable="@drawable/breath_0026" android:duration="100"/> </animation-list>
其他的类似。