Android动态壁纸如何实现双击?

发布于 2024-12-07 20:55:20 字数 1089 浏览 0 评论 0原文

我想为 Android 动态壁纸实现双击事件。 遗憾的是,我找不到任何具体的代码来实现这一点。

目前,我找到了使用 Engine 类的 onTouchEvent 方法的解决方法:

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);
}

嗯,这不是一个优雅的解决方案。我知道有些壁纸实现了双击。但我不知道如何自己做。

因此,朝正确的方向“点击”会很好。如果有必要,我会接受“双击”。 :D

你好, 罗伯特

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.

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);
}

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.

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

Greetings,
Robert

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-12-14 20:55:20

使用 http://developer.android.com/reference/android/view/GestureDetector。例如

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;
            }
        });
    }
}

Use http://developer.android.com/reference/android/view/GestureDetector.html

for example:

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;
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文