DrawerLayout + SurfaceView + TouchListener
DrawerLayout + SurfaceView + TouchListener
我想将 DrawerLayout 与 SurfaceView(MetaioSDK 增强现实视图)一起使用。除了 SurfaceView 的 TouchListener 之外,一切正常。
看起来 DrawerlLayout 内的 FrameLayout 阻止了我的 SurfaceView 的触摸事件。是否可以将 Event 直接委托给 SurfaceView?非常感谢专业人士的帮助。
下面是我的 xml-布局
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>
<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/tV_Attributes"
android:layout_width="240dp"
android:layout_height="match_parent"
android:text="Hier sollen Attribute angezeigt werden." />
</LinearLayout>
您需要扩展 FrameLayout 以忽略触摸事件(如果它落在 SurfaceView 上)。
在您的框架布局中:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isHitToView(ev, view)) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
public static boolean isHitToView(MotionEvent event, View view) {
if (view == null || event == null || view.getVisibility() != View.VISIBLE)
return false;
Rect r = new Rect();
//r.left и r.top - всегда нули
view.getLocalVisibleRect(r);
int[] coordinates = new int[2];
view.getLocationOnScreen(coordinates);
r.left += coordinates[0];
r.right += coordinates[0];
r.top += coordinates[1];
r.bottom += coordinates[1];
float x = event.getRawX();
float y = event.getRawY();
if (r.right >= x && r.top <= y && r.left <= x && r.bottom >= y)
return true;
return false;
}
我想将 DrawerLayout 与 SurfaceView(MetaioSDK 增强现实视图)一起使用。除了 SurfaceView 的 TouchListener 之外,一切正常。 看起来 DrawerlLayout 内的 FrameLayout 阻止了我的 SurfaceView 的触摸事件。是否可以将 Event 直接委托给 SurfaceView?非常感谢专业人士的帮助。
下面是我的 xml-布局
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>
<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/tV_Attributes"
android:layout_width="240dp"
android:layout_height="match_parent"
android:text="Hier sollen Attribute angezeigt werden." />
</LinearLayout>
您需要扩展 FrameLayout 以忽略触摸事件(如果它落在 SurfaceView 上)。
在您的框架布局中:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isHitToView(ev, view)) {
return false;
} else {
return super.onInterceptTouchEvent(ev);
}
}
public static boolean isHitToView(MotionEvent event, View view) {
if (view == null || event == null || view.getVisibility() != View.VISIBLE)
return false;
Rect r = new Rect();
//r.left и r.top - всегда нули
view.getLocalVisibleRect(r);
int[] coordinates = new int[2];
view.getLocationOnScreen(coordinates);
r.left += coordinates[0];
r.right += coordinates[0];
r.top += coordinates[1];
r.bottom += coordinates[1];
float x = event.getRawX();
float y = event.getRawY();
if (r.right >= x && r.top <= y && r.left <= x && r.bottom >= y)
return true;
return false;
}