How to implement double tap for Android live wallpapers?
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开发者_JAVA技巧 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
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;
}
});
}
}
精彩评论