detect double tap (Double click) or long click in a videoview

*爱你&永不变心* 提交于 2019-12-12 19:19:17

问题


I am working on an Android application. In my app I have to show the video in the corner of the screen.Then If the user double clicked or longclicked I have to expand the video in to full screen. So i used the following code.

vd.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub




            if (!flag) {
                DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
                android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) vd.getLayoutParams();
                params.width =  metrics.widthPixels;
                params.height = metrics.heightPixels;
                params.leftMargin = 0;
                vd.setLayoutParams(params);
                flag=true;

            }
            else{

                DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
                android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) vd.getLayoutParams();
                params.width =  (int) (200);
                params.height = (int) (200);
                params.leftMargin = 30;
                vd.setLayoutParams(params);
                flag = false;

            }
            return true;
        }

    });

But nothing happeneds on the long click.Long click is working fine for button but not for Videoview. Please help me to find a solution. Thanks in advance


回答1:


I was having the same issue, this is what I did:

Since setOnClickListener or setOnLongClickListener is not been triggered, I created my own class which extends VideoView

public class VideoViewCustom extends VideoView{

and use this class as and xml object

   <com.your.proyect.VideoViewCustom
     android:id="@+id/my_custom_videoview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/> 

on my VideoViewCustom class, I overrided the onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if(ev.getAction() == MotionEvent.ACTION_DOWN && longClickTimer == null)
    {
        Log.d(TAG, "ACTION_DOWN");
        longClickTimer = new Timer();
        longClickTimer.schedule(new longClickTask(), DELAY_TIME,PERIOD_TIME);
    }
    else if(ev.getAction() == MotionEvent.ACTION_UP)
    {
        Log.d(TAG, "ACTION_UP");

        if(longClickTimer != null)
        {
            longClickTimer.cancel();
            longClickTimer.purge();
            longClickTimer = null;
        }
    }

    return true;
}

class longClickTask extends TimerTask {

    @Override
    public void run() {
        Log.d(TAG, "Long Click");
        longClickTimer.cancel();
        longClickTimer.purge();
        longClickTimer = null;

        //IMPLEMENT YOUR LONG CLICK TASK HERE
    }

};

now it can be know when the VideoView is been clicked. I just implemented a timer which will trigger a task after 1000ms to simulate a Long Click. You can set the delay you want.

Hope this helps someone!




回答2:


More right way: set listener:

videoView.setOnTouchListener(new OnTouchListener () {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN && longClickTimer == null)
        {
            if (mc.isShowing()) {mc.hide();} else {
                mc.show(10000);} 
            longClickTimer = new Timer();
            longClickTimer.schedule(new longClickTask(), 3000);
        }
        else 
        {
            if(longClickTimer != null)
            {
                longClickTimer.cancel();
                longClickTimer.purge();
                longClickTimer = null;
            }
        }
        return true;
    }});

task for timer:

class longClickTask extends TimerTask {
    @Override
    public void run() {

        if(longClickTimer != null)
        {
            longClickTimer.cancel();
            longClickTimer.purge();
            longClickTimer = null;
        }
        getActivity().runOnUiThread(Runnable1);
    }}

and runnable with full screen:

final Runnable Runnable1 = new Runnable() {
    public void run() {
        int orien = getResources().getConfiguration().orientation;
        if ((orien==Configuration.ORIENTATION_LANDSCAPE) && (frag==0)) {
        if (fullscr==false) { 
        wind.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        wind.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DisplayMetrics metrics = new DisplayMetrics(); wind.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoView.getLayoutParams();
        heightvid=params.height;
        params.width =  metrics.widthPixels;
        params.height = metrics.heightPixels;
        frame1.setBackgroundColor(Color.BLACK);

        videoView.setLayoutParams(params);
        fullscr=true;}
        else if ((orien==Configuration.ORIENTATION_LANDSCAPE) && (frag==0)) {
            wind.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            DisplayMetrics metrics = new DisplayMetrics(); wind.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoView.getLayoutParams();
            params.width =  WindowManager.LayoutParams.FILL_PARENT;
            params.height = heightvid;
            frame1.setBackgroundColor(Color.WHITE);

            videoView.setLayoutParams(params);
            fullscr=false;
        }
    }}
};

Enjoy!



来源:https://stackoverflow.com/questions/16457049/detect-double-tap-double-click-or-long-click-in-a-videoview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!