使点击贴在工作中

时间:2014-08-08 09:00:51

标签: android overlay

我在其中创建了一个带有listView的透明叠加层。它占据了屏幕的一半。叠加层后面的点击不起作用我不想在叠加层上进行任何点击操作,但希望后台应用程序完全正常工作。基本上我想在屏幕上显示文字,与屏幕后面的内容无关。以下是我的叠加代码。

      package de.mobilej.overlay;

import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {

private View topLeftView;

private Button overlayedButton;

private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

overlayedButton = new Button(this);
overlayedButton.setText("Overlay button");
overlayedButton.setOnTouchListener(this);
overlayedButton.setAlpha(0.0f);
overlayedButton.setBackgroundColor(0x55fe4444);
overlayedButton.setOnClickListener(this);

ListView listView;
ArrayAdapter<String> aR;
String[] str = new String[] { "item1", "item2", "item3", "item4", "item5",
        "item6" };

listView = new ListView(this);
//listView.setOnTouchListener(this);
listView.setAlpha(0.0f);
listView.setBackgroundColor(0x55fe4444);
//listView.setOnClickListener(this);
aR = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, str);
listView.setAdapter(aR);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
//wm.addView(overlayedButton, params);
wm.addView(listView, params);

topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);

}

@Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
    wm.removeView(overlayedButton);
    wm.removeView(topLeftView);
    overlayedButton = null;
    topLeftView = null;
}
}

@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    float x = event.getRawX();
    float y = event.getRawY();

    moving = false;

    int[] location = new int[2];
    overlayedButton.getLocationOnScreen(location);

    originalXPos = location[0];
    originalYPos = location[1];

    offsetX = originalXPos - x;
    offsetY = originalYPos - y;

} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
    int[] topLeftLocationOnScreen = new int[2];
    topLeftView.getLocationOnScreen(topLeftLocationOnScreen);

    System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
    System.out.println("originalY="+originalYPos);

    float x = event.getRawX();
    float y = event.getRawY();

    WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

    int newX = (int) (offsetX + x);
    int newY = (int) (offsetY + y);

    if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
    return false;
    }

    params.x = newX - (topLeftLocationOnScreen[0]);
    params.y = newY - (topLeftLocationOnScreen[1]);

    wm.updateViewLayout(overlayedButton, params);
    moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
    if (moving) {
    return true;
    }
}

return false;
}

@Override
public void onClick(View v) {
Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:0)

添加WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE标志:

int curFlags = topLeftarams.flags;
topLeftarams.flags = curFlags | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

然后给WindowManager这个参数:

wm.addView(topLeftView, topLeftarams);