当我点击屏幕时,我正试图让控制按钮弹出开/关画面。他们将覆盖视频。我遇到的问题是如何为已经膨胀的布局创建触摸事件
例程如下:
1)为视频设置初始内容;
2)在视频顶部扩展包含控件的布局(XML);
3)(需要做)为新膨胀的布局创建onTouch事件,当用户点击屏幕时隐藏/显示按钮。
包含要显示/隐藏的按钮的布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dpad_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/tiltUp"
android:background="@drawable/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:id="@+id/panRight"
android:background="@drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:id="@+id/tiltDown"
android:background="@drawable/arrow_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="8dp"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:id="@+id/panLeft"
android:background="@drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
<Button
android:id="@+id/zoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/zoom_in"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="@+id/zoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/zoom_out"
android:layout_marginRight="0dp"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/zoomIn"/>
<Button
android:id="@+id/irisDarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@drawable/iris_darker"/>
<Button
android:id="@+id/irisBrighter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/irisDarker"
android:background="@drawable/iris_brighter"/>
<Button
android:id="@+id/focusFar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/dpad_grid"
android:layout_marginRight="20dp"
android:layout_alignParentStart="true"
android:background="@drawable/focus_far"/>
<Button
android:id="@+id/focusNear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/focus_near"
android:layout_alignTop="@+id/focusFar"
android:layout_alignStart="@+id/irisBrighter"/>
<Button
android:id="@+id/snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/camera"
android:layout_alignEnd="@+id/focusFar"
android:layout_alignParentStart="true"/>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/gear"/>
</RelativeLayout>
添加视频和扩充上述布局的代码:
@Override
public void onResume() {
super.onResume();
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mv = new MjpegView(this);
setContentView(mv);
LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.screen2, null), relativeLayoutParams);
感谢您的帮助!
编辑:这是我目前正在使用的内容。当我触摸屏幕时,为什么我的代码没有进入触摸事件?LayoutInflater inflater = getLayoutInflater();
control_buttons = inflater.inflate(R.layout.screen2, null);
control_buttons.setOnTouchListener(new View.OnTouchListener() {
boolean gone = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("In OnTouch");
if ( (event.getAction() == MotionEvent.ACTION_DOWN) && (gone == true) ){
getWindow().addContentView(control_buttons, relativeLayoutParams);
gone = false;
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == false)) {
v.setVisibility(View.GONE);
gone = true;
return true;
}
return false;
}
});
答案 0 :(得分:0)
这可能对您更有效。只有在RelativeLayout中的子项是包含按钮的Buttons或GridLayouts时,这才有效。
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
for(int i = 0; i < relativeLayout.getChildCount(); i++) {
if(relativeLayout.getChildAt(i) instanceof Button) {
Button temp = (Button) relativeLayout.getChildAt(i);
if(temp.getVisibility() == View.INVISIBLE) {
temp.setVisibility(View.VISIBLE);
} else {
temp.setVisibility(View.INVISIBLE);
}
} else if(relativeLayout.getChildAt(i) instanceof GridLayout) {
GridLayout temp = (GridLayout) relativeLayout.getChildAt(i);
for(int g = 0; g < temp.getChildCount(); g++) {
Button tempButton = (Button) temp.getChildAt(g);
if(tempButton.getVisibility() == View.INVISIBLE) {
tempButton.setVisibility(View.VISIBLE);
} else {
temp.setVisibility(View.INVISIBLE);
}
}
}
}
}
return false;
}
});