我有一個videoView中的片段,我需要檢測它的手勢。我嘗試用下面的代碼沒有成功。我如何正確實現這一點?需要幫助。提前致謝。videoView內的Android手勢檢測器片段
CustomVideoView.java
public class CustomVideoView extends VideoView {
private GestureDetector gestureDetector;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
public void setGestureDetector(GestureDetector gestureDetector) {
this.gestureDetector = gestureDetector;
}
}
MyFragment.java
public class MyFragment extends Fragment{
private Context context;
private CustomVideoView videoView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity();
View tmp_view = inflater.inflate(R.layout.my_fragment, container, false);
assert tmp_view != null;
videoView = (CustomVideoView) tmp_view.findViewById(R.id.videoView);
MediaController mc = new MediaController(context);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse("myUrl");
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
videoView.setGestureDetector(new GestureDetector(new CustomeGestureDetector()));
return tmp_view;
}
private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//NEVER CALLED
if(e1 == null || e2 == null) return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
else {
try { // right to left swipe .. go to next page
if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) {
//do your stuff
Log.i("MyApp", "right to left");
return true;
} //left to right swipe .. go to prev page
else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
//do your stuff
Log.i("MyApp", "left to right");
return true;
} //bottom to top, go to next document
else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800
&& videoView.getScrollY() >= videoView.getHeight()) {
//do your stuff
return true;
} //top to bottom, go to prev document
else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800) {
//do your stuff
return true;
}
} catch (Exception e) { // nothing
}
return false;
}
}
@Override
public boolean onDown(MotionEvent event) {
Log.i("MyApp", "onDown");
//THIS IS CALLED WHEN TOUCH
return super.onDown(event);
}
}
當我觸摸我的VideView只響應它的控制(播放,暫停......),但姿態探測器從未打過電話也許這些控件擾亂我的手勢檢測器?
觸摸時會調用onDown事件,但永遠不會觸發onFling。是否有可能通過onDown事件實現onFling函數?
我剛剛用onFling方法創建了一個監聽器類。我的問題是,從來沒有被稱爲。當我觸摸videoview時,只響應其控件(播放,停止...),但手勢檢測器從未呼叫過。 – user3065901 2014-10-22 10:35:06
我正在嘗試調試您的代碼。只要我找到一些東西,會立即回覆給你。我認爲'GestureDetector'沒有獲得'videoView'的上下文。 – Siddharth2092 2014-10-22 10:47:20
我不明白你使用'gestureDetector.onTouchEvent(ev)||的邏輯。 super.onTouchEvent(ev)'。你爲什麼這樣做? – Siddharth2092 2014-10-22 10:58:22