我试图在我的Android应用中创建手势识别功能,以响应手势并相应地更改视图。一些背景信息:我的活动包括线性布局中的两个并排视图。
我遇到的问题是,尽管在设备的屏幕上执行了手势,仍然不会触发onFling()方法。
这是我的手势识别处理程序类, GestureListener.java :
package com.example.guitarsim;
//this class is designed to listen for touch events corresponding to the fling gesture
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
class GestureListener extends SimpleOnGestureListener implements OnTouchListener {
Context mContext;
GestureDetector gDetector;
static final double SWIPE_MIN_DISTANCE = PlayFrets.getActualHeight()*(0.05); //must swipe across at least 5% of screen height to register a gesture
static final int SWIPE_MAX_OFF_PATH = 150;
static final int SWIPE_THRESHOLD_VELOCITY = 100;
public GestureListener(Context context) {
this.mContext = context;
}
public GestureListener(Context context, GestureDetector gDetector) {
if (gDetector == null){
gDetector = new GestureDetector(context, this);
}
this.mContext = context;
this.gDetector = gDetector;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gDetector.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
Log.e("gesture", String.valueOf(SWIPE_MIN_DISTANCE));
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH || Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
return false;
}
else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
//float traveled = e1.getY() - e2.getY();
//Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
Toast.makeText(mContext, "Swiped Up",Toast.LENGTH_SHORT).show();
}
else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
//float traveled = e2.getY() - e1.getY();
//Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
Toast.makeText(mContext, "Swiped Down",Toast.LENGTH_SHORT).show();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
public GestureDetector getDetector() {
return gDetector;
}
}
编辑这是我的主要活动类,我在其中实例化我的GestureListener对象, PlayFrets.java
public class PlayFrets extends Activity {
LinearLayout gestureOverlay;
GestureListener gestureListener;
public void configGestureRecognition(){ //Toolbar gesture recognition
gestureOverlay = (LinearLayout) findViewById(R.id.toolbarGestureOverlay);
gestureListener = new GestureListener(PlayFrets.this);
if (gestureOverlay == null){
Toast.makeText(PlayFrets.this, "gestureOverlay object is null bro" ,Toast.LENGTH_SHORT).show();
}
gestureOverlay.setOnTouchListener(gestureListener);
}
}
编辑我还在运行应用程序启动时运行Asynctask的类中调用了运行我的GestureListener实例化代码的主活动中的方法并设置视图 LoadNoteFiles.java :
public class LoadNoteFiles extends AsyncTask<Void, Void, Void>{
private Context mContext;
Activity instance;
public LoadNoteFiles(Context context){ //constructor
mContext = context;
}
@Override
protected void onPreExecute(){
PlayFrets.viewSwitch = new ViewSwitcher(mContext);
PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.splash_screen, null));
instance = (Activity)mContext; //cast context from main activity into an activity to access setContentView method
instance.setContentView(PlayFrets.viewSwitch);
}
...//do in background code omitted
@Override
protected void onPostExecute(Void v){
PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.activity_play_frets, null));
PlayFrets.viewSwitch.showNext();
((PlayFrets)mContext).configFretboard();
((PlayFrets)mContext).configGestureRecognition(); //test gesture recognition
}
这是我的xml文件。 *请注意,我尝试检测手势的视图是ID为toolbarGestureOverlay的线性布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:keepScreenOn="true">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/gold_guitarsim_text"
android:orientation="vertical"
android:id="@+id/toolbarGestureOverlay" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="4">
<com.example.guitarsim.Multitouch
android:id="@+id/fretBoard"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/fret_board1" />
</LinearLayout>
</LinearLayout>
所以这是我的问题;是什么阻止了GestureListener的onFling()方法被触及?另外,我的xml文件是否正确设置?特别;我是否需要标记线性布局我尝试检测手势,就像我使用myother线性布局(com.example.guitarsim.Multitouch)一样,以便在我的程序中有效地控制它?