我正在开发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没有。请帮我找一个解决方案。 提前致谢
答案 0 :(得分:0)
我遇到了同样的问题,这就是我所做的:
由于没有触发setOnClickListener或setOnLongClickListener,我创建了自己的类,扩展了VideoView
public class VideoViewCustom extends VideoView{
并将此类用作和xml对象
<com.your.proyect.VideoViewCustom
android:id="@+id/my_custom_videoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
在我的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。我刚刚实现了一个计时器,它将在1000ms后触发任务以模拟长按。您可以设置所需的延迟。
希望这有助于某人!
答案 1 :(得分:0)
更正确的方式: 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;
}});
计时器的任务:
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;
}
}}
};
享受!