0
我的應用程序在屏幕的左邊緣放置了不可見的覆蓋圖,並且如果用戶從下半部分划動,我想捕獲此事件並對其執行操作。問題在於,不僅僅是下半部分,邊緣上半部分(8dp寬)上的所有觸摸事件也會被消耗,因此不會傳遞到其下面的應用程序。覆蓋消耗所有觸摸事件
版式文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/llLauncher"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/transparentblack80"
android:visibility="gone">
<be.robinj.ubuntu.unity.launcher.AppLauncher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:icon="@drawable/launcher_bfb"
custom:special="true"
android:onClick="lalBfb_clicked"
android:id="@+id/lalBfb"
android:layout_marginTop="2dp"
android:tag="partOfLauncher" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrLauncherAppsContainer"
android:scrollbars="none"
android:layout_weight="1"
android:fillViewport="false"
android:tag="partOfLauncher">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llLauncherPinnedApps"
android:layout_gravity="top"
android:tag="partOfLauncher">
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/llListenerContainer"
android:orientation="vertical"
android:layout_width="8dp"
android:layout_height="match_parent"
android:weightSum="2"
android:gravity="bottom">
<LinearLayout
android:id="@+id/llListener"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF0000">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llShadow"
android:orientation="vertical"
android:layout_width="160dp"
android:layout_height="match_parent"
android:background="@drawable/launcherservice_shadow"
android:visibility="gone"
android:tag="shadow">
</LinearLayout>
</LinearLayout>
llListenerContainer
及其子元素llListener
是重要的東西在這裏。另外兩個元素默認爲隱藏狀態,當檢測到屏幕左下角的觸摸時會顯示。
在下面的截圖中,我給llListener
添加了一個紅色的背景顏色;
所以倒是應該由我的服務可以處理的紅色區域內,但倒是左邊緣的上半部分應該只是由它下面的應用程序進行處理。
相關的Java代碼;
public class LauncherService extends Service
{
private WindowManager wm;
private TouchListener touchListener;
private LinearLayout layout;
private LinearLayout llListenerContainer;
private LinearLayout llListener;
private LinearLayout llLauncher;
private LinearLayout llShadow;
@Override
public IBinder onBind (Intent intent)
{ return null; }
@Override
public void onCreate()
{
super.onCreate();
this.wm = (WindowManager) this.getSystemService (WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService (Service.LAYOUT_INFLATER_SERVICE);
this.layout = (LinearLayout) inflater.inflate (R.layout.service_launcher, null, false);
this.llLauncher = (LinearLayout) this.layout.findViewById (R.id.llLauncher);
this.llListenerContainer = (LinearLayout) this.layout.findViewById (R.id.llListenerContainer);
this.llListener = (LinearLayout) this.layout.findViewById (R.id.llListener);
this.llShadow = (LinearLayout) this.layout.findViewById (R.id.llShadow);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
this.touchListener = new TouchListener (this);
lalBfb.setOnTouchListener (this.touchListener);
this.llListener.setOnTouchListener (this.touchListener);
this.llShadow.setOnTouchListener (this.touchListener);
this.wm.addView (this.layout, params);
}
@Override
public int onStartCommand (Intent intent, int flags, int id)
{
this.layout.setVisibility (intent.getBooleanExtra ("show", true) ? View.VISIBLE : View.GONE);
return super.onStartCommand (intent, flags, id);
}
@Override
public void onDestroy()
{
super.onDestroy();
this.wm.removeView (this.layout);
}
//# Event handlers #//
public void swipeRight()
{
this.llLauncher.setVisibility (View.VISIBLE);
this.llShadow.setVisibility (View.VISIBLE);
this.llListenerContainer.setVisibility (View.GONE);
}
public void swipeLeft()
{
this.llLauncher.setVisibility (View.GONE);
this.llShadow.setVisibility (View.GONE);
this.llListenerContainer.setVisibility (View.VISIBLE);
}
}
public class TouchListener implements View.OnTouchListener
{
private LauncherService parent;
public TouchListener (LauncherService parent)
{ this.parent = parent; }
@Override
public boolean onTouch (View view, MotionEvent event)
{
int id = view.getId();
if (id == R.id.llListener)
{
this.parent.swipeRight();
return true;
}
else if (id == R.id.llShadow)
{
this.parent.swipeLeft();
return true;
}
return false;
}
}