我目前正在处理我的第一个Android应用程序,并且想知道如何实现onFling方法,使位于屏幕底部的Drawable Shape被抛到屏幕顶部。首先是用于实现游戏屏幕的.java文件。
Game1.java
package com.example.mdm71.flyingbird;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
public class Game1 extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
private ImageView gestureImage;
private GestureDetectorCompat gDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamescreen);
gestureImage = (ImageView)findViewById(R.id.thrown_ball);
this.gDetector = new GestureDetectorCompat(this, this);
gDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event){
this.gDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent event){
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
return;
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY){
Toast.makeText(this, "Fling Event performed", Toast.LENGTH_LONG).show();
return true;
}
@Override
public void onLongPress(MotionEvent event){
return;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
return false;
}
}
这是定义形状的文件:
Shapes.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/thrown_ball"
android:shape="oval">
<corners
android:radius="20px"
android:topLeftRadius="20px"
android:topRightRadius="20px"
android:bottomLeftRadius="20px"
android:bottomRightRadius="20px" />
<size
android:width="100px"
android:height="100px" />
<solid
android:color="#000000" />
</shape>