在视频视图中检测双击(双击)或长按
Posted
技术标签:
【中文标题】在视频视图中检测双击(双击)或长按【英文标题】:detect double tap (Double click) or long click in a videoview 【发布时间】:2013-05-03 15:35:58 【问题描述】:我正在开发一个 android 应用程序。在我的应用程序中,我必须在屏幕一角显示视频。然后,如果用户双击或长按,我必须将视频展开到全屏。 所以我使用了以下代码。
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;
);
但是长按没有任何反应。长按对按钮工作正常,但对 Videoview 却没有。请帮助我找到解决方案。 提前致谢
【问题讨论】:
【参考方案1】:我遇到了同样的问题,这就是我所做的:
由于未触发 setOnClickListener 或 setOnLongClickListener,我创建了自己的扩展 VideoView 的类
public class VideoViewCustom extends VideoView
并将此类用作和 xml 对象
<com.your.proyect.VideoViewCustom
android:id="@+id/my_custom_videoview"
android:layout_
android:layout_/>
在我的 VideoViewCustom 类中,我覆盖了 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
;
现在可以知道 VideoView 何时被点击。我刚刚实现了一个计时器,它将在 1000 毫秒后触发一个任务来模拟长按。你可以设置你想要的延迟。
希望这对某人有所帮助!
【讨论】:
【参考方案2】:更正确的方法: 设置监听器:
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;
);
定时器任务:
class longClickTask extends TimerTask
@Override
public void run()
if(longClickTimer != null)
longClickTimer.cancel();
longClickTimer.purge();
longClickTimer = null;
getActivity().runOnUiThread(Runnable1);
并且可以全屏运行:
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;
;
享受吧!
【讨论】:
以上是关于在视频视图中检测双击(双击)或长按的主要内容,如果未能解决你的问题,请参考以下文章