如何实现为Android动态壁纸枪王? [英] How to implement double tap for Android live wallpapers?

查看:232
本文介绍了如何实现为Android动态壁纸枪王?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施的是Android动态壁纸双击事件。
可悲的是,我发现couldn't任何具体code如何做到这一点。

I want to implement a double tap event for a Android live wallpaper. Sadly, I couldn´t find any specific code how to do that.

目前发现从来就使用引擎类的onTouchEvent-方法workarround:

At the moment I´ve found a workarround using the onTouchEvent-method of the Engine-class:

public void onTouchEvent(MotionEvent event) {
  long time = android.os.SystemClock.currentThreadTimeMillis();

  if(((time - mLastTouchTime) < 500) && ((time - mLastTouchTime) > 100))
  {
    if(!mIsPlayed && mSound)
      {
        mIsPlayed = true;
        int sound = R.raw.hell;
        if(mTheme.equals("rose"))
          sound = R.raw.rose;
        if(mTheme.equals("greed"))
          sound = R.raw.greed;

        MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);   
          mp.start();
          mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        mp.release();
        mIsPlayed = false;
      }
    });
      }
  }

  mLastTouchTime = time;
  super.onTouchEvent(event);
}

好吧,that's不是一个完美的解决方案。我知道有哪些实施枪王壁纸。但我不知道,怎么做我自己。

Well, that´s not an elegant solution. I know there are wallpapers which implemented the double tap. But I have no idea, how to do it on my own.

因此​​,一个龙头在正确的方向将是很好。如果nescessary,我会接受一个枪王。 :D

So a "tap" in the right direction would be nice. If nescessary, I will accept a "double tap". :D

问候,
罗伯特

Greetings, Robert

推荐答案

使用<一个href=\"http://developer.android.com/reference/android/view/GestureDetector.html\">http://developer.android.com/reference/android/view/GestureDetector.html

例如:

public class AndroidTestActivity extends Activity {

    private GestureDetector gestureDetector;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.e("onDoubleTap", e.toString());
                //handle double tap
                return true;
            }
        });
    }
}

这篇关于如何实现为Android动态壁纸枪王?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆