在指定地点播放动画,下面是一个爆炸过程的动画,鼠标点击处播放该动画


定义动画资源文件blast.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/bom_f01" android:duration="80" /><item android:drawable="@drawable/bom_f02" android:duration="80" /><item android:drawable="@drawable/bom_f03" android:duration="80" /><item android:drawable="@drawable/bom_f04" android:duration="80" /><item android:drawable="@drawable/bom_f05" android:duration="80" /><item android:drawable="@drawable/bom_f06" android:duration="80" /><item android:drawable="@drawable/bom_f07" android:duration="80" /><item android:drawable="@drawable/bom_f08" android:duration="80" /><item android:drawable="@drawable/bom_f09" android:duration="80" /><item android:drawable="@drawable/bom_f10" android:duration="80" /><item android:drawable="@drawable/bom_f11" android:duration="80" /><item android:drawable="@drawable/bom_f12" android:duration="80" /><item android:drawable="@drawable/bom_f13" android:duration="80" /><item android:drawable="@drawable/bom_f14" android:duration="80" /><item android:drawable="@drawable/bom_f15" android:duration="80" /><item android:drawable="@drawable/bom_f16" android:duration="80" /><item android:drawable="@drawable/bom_f16" android:duration="80" /><item android:drawable="@drawable/bom_f17" android:duration="80" /><item android:drawable="@drawable/bom_f18" android:duration="80" /><item android:drawable="@drawable/bom_f19" android:duration="80" /><item android:drawable="@drawable/bom_f20" android:duration="80" /><item android:drawable="@drawable/bom_f21" android:duration="80" /><item android:drawable="@drawable/bom_f22" android:duration="80" /><item android:drawable="@drawable/bom_f23" android:duration="80" /><item android:drawable="@drawable/bom_f24" android:duration="80" /><item android:drawable="@drawable/bom_f25" android:duration="80" /><item android:drawable="@drawable/bom_f26" android:duration="80" /><item android:drawable="@drawable/bom_f27" android:duration="80" /></animation-list>
动画控制代码

package WangLi.Graphics.Blast;import java.lang.reflect.Field;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.drawable.AnimationDrawable;import android.media.MediaPlayer;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.FrameLayout;import android.widget.ImageView;public class Blast extends Activity {    /** Called when the activity is first created. */private MyView myView;private AnimationDrawable anim;private MediaPlayer bomb;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //使用FramLayout布局管理器,它允许组件自己控制位置        FrameLayout frame = new FrameLayout(this);        setContentView(frame);        //设置作用背景        frame.setBackgroundResource(R.drawable.back);        //加载音效        bomb = MediaPlayer.create(this, R.raw.bomb);        myView = new MyView(this);        //设置myView用于显示blast动画        myView.setBackgroundResource(R.anim.blast);        //设置myView为默认隐藏        myView.setVisibility(View.INVISIBLE);        //获取动画对象        anim = (AnimationDrawable)myView.getBackground();        frame.addView(myView);        frame.setOnTouchListener(new OnTouchListener(){        @Override        public boolean onTouch(View source,MotionEvent event)        {        //只处理按下事件(避免每次产生两个动画效果)        if(event.getAction() == MotionEvent.ACTION_DOWN)        {        //先停止动画播放        anim.stop();        float x = event.getX();        float y = event.getY();        //控制myView的显示位置        myView.setLocation((int)y - 40, (int)x - 20);        //setVisibility已经播放了动画,不需要再写anim.start();        myView.setVisibility(View.VISIBLE);        //播放音效        bomb.start();        }        return false;        }        });    }        //定义一个自定义View,该自定义View用于播放"爆炸效果"    class MyView extends ImageView    {public MyView(Context context) {super(context);// TODO Auto-generated constructor stub}//定义一个方法,该方法用于控制MyView的显示位置public void setLocation(int top, int left){this.setFrame(left, top, left + 40, top + 40);}//重写该方法,控制如果动画播放到最后一帧时,隐藏该View@Overrideprotected void onDraw(Canvas canvas){try{Field field = AnimationDrawable.class.getDeclaredField("mCurFrame");field.setAccessible(true);//获取anim动画当前帧int curFrame = field.getInt(anim);//如果已经到了最后一帧if(curFrame == anim.getNumberOfFrames() - 1){//让该View隐藏setVisibility(View.INVISIBLE);}}catch(Exception e){}super.onDraw(canvas);}    }}



更多相关文章

  1. 【Android】让你的对话框炫起来!漂亮动画效果的Dialog
  2. 一个Android下拉刷新样式
  3. Tutorials ---- Hello, World
  4. Android通过手势实现图片按照指定路径移动
  5. viewpager切换添加动画效果viewpager切换添加动画效果--IT蓝豹
  6. Android(安卓)自定义SeekBar 实现分段显示不同背景颜色
  7. android中系统时间
  8. animation动画
  9. android assert 自定义格式被压缩

随机推荐

  1. 翻译Android(安卓)API Guides: App Manif
  2. FFmpeg学习—android 编译libfaac 音频库
  3. 最近下载了ADT Version 22发现里面多了个
  4. android studio 新建class文件报错Unable
  5. Android(安卓)结束进程的方法
  6. Android(安卓)多次点击事件的触发方法
  7. android 常用第三方插件收藏
  8. Android的冷启动优化
  9. android存取数据(基础)
  10. 从屏幕底部弹出PopupWindow