Android视频内部片段的Android手势探测器

时间:2014-10-22 10:17:13

标签: android fragment android-videoview gesture detection

我在片段中有一个videoView,我需要检测它上面的手势。我尝试使用以下代码但没有成功。我怎样才能正确实现这一目标?需要帮助。提前谢谢。

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功能?

2 个答案:

答案 0 :(得分:1)

我不知道为什么但是将onDown方法添加到我的CustomeGestureDetector会使onFling方法被调用。

private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    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) {
          return true;
    }

}

答案 1 :(得分:0)

在android中以简单的方式使用GestureDetector创建一个扩展SimpleOnGestureListner的侦听器类,然后覆盖onScrollonFling方法。我认为除了onTouch之外,这些应该是用户在视频上遇到的唯一手势。

示例源代码:

class VideoGestureDetector extends SimpleOnGestureListener {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

      // implementation when the user scrolls
      return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

      // implementation when the user flings
      return false;
    }
  }