Android Studio部分事件

1.Android MotionEvent

public boolean onTouchEvent (MotionEvent event){
            menu.onTouch(event);
            celect.onTouch(event);
            myPlane.onTouch(event);
            return true;
        }
public void onTouch(MotionEvent event){
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            float ex = event.getX();
            float ey = event.getY();
            if (ex>x&&ex<x+bitmap3.getWidth()&&ey>y&&ey<y+bitmap3.getHeight()){
                    MySurfaceView.Game_STATE = 0;
                }
        }
    }

关于MotionEvent的一个重要概念,事件类型。事件类型就是指MotionEvent对象所代表的动作。有的事件代表你手指按下这个动作,有的事件代表你手指在屏幕上滑动,还有的事件代表你手指离开屏幕。这些事件的事件类型就分别为ACTION_DOWN,ACTION_MOVE,和ACTION_UP。上述这个动作所产生的一系列事件,被称为一个事件流,它包括一个ACTION_DOWN事件,很多个ACTION_MOVE事件,和一个ACTION_UP事件。

2.Vector

Vector mybulletVector = new Vector<>();
这个创建一个动态数组的普通对象,是线性安全,而Arraylist是非线性安全

 if (count % 10 == 0) {
       gameSoundPool.playSound(1);
       Mybullet mybullet = new Mybullet(BitmapFactory.decodeResource(this.getResources(), R.mipmap.mybullet),myPlane.getX()+(myPlane.getWidth()/2)-15,myPlane.getY(),0);
      mybulletVector.add(mybullet);}                 //添加数据
 for (int i=0;i<mybulletVector.size();i++) {
     if (mybulletVector.elementAt(i).isDead()) {     //判断
       mybulletVector.remove(i);                     //删除数据
   }
 }
 for (int i=0;i<mybulletVector.size();i++){
   mybulletVector.elementAt(i).Appear(canvas,paint); //for循环打印
 if (bossPlane.isCollision(mybulletVector.elementAt(i))){
     gameSoundPool.playSound(2);
            mybulletVector.remove(i);
                            }
                        }

3.soundpool

SoundPool主要用于播放一些较短的声音片段,与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、 播放比率等参数

SoundPool(int maxStreams, int streamType, int srcQuality):第一个参数指定支持多少个声音;第二个参数指定声音类型:第三个参数指定声音品质。

Soundpool的方法其一:

int load(Context context, int resld, int priority):从 resld 所对应的资源加载声音。

int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate):该方法的第一个参数指定播放哪个声音;leftVolume、rightVolume指定左、右的音量:priority指定播放声音的优先级,数值越大,优先级越高;loop指定是否循环,0为不循环,-1为循环;rate指定播放的比率,数值可从0.5到2, 1为正常比率。

使用SoundPool播放声音的步骤如下:

1) 调用SoundPool的构造器创建SoundPool的对象。

2) 调用SoundPool对象的load()方法从指定资源、文件中加载声音。最好使用HashMap< Integer, Integer>来管理所加载的声音。

3) 调用SoundPool的play方法播放声音。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值