我已经非常困难地设法使位图覆盖屏幕。我也可以获取触摸输入,但是它可以在屏幕上的任何地方获取触摸输入。

我想知道如何检查触摸是否在屏幕上可见的位图上。

服务和 View 类如下。我曾想过,但我想不出一种方法:(

package <package>;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

public class MyService extends Service {
    ButtonView mView;
    Bitmap bit;

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

    @Override
    public void onCreate() {
        super.onCreate();

        bit = BitmapFactory.decodeResource(getResources(), R.drawable.button);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this);
        builder.setContentTitle("Ingress Tools Running");
        builder.setContentText("Click to stop Ingress Tools");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(
                this, StopActivity.class), 0));
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1, builder.build());

        mView = new ButtonView(this, bit);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(), "onDestroy", Toast.LENGTH_LONG).show();
        if (mView != null) {
            ((WindowManager) getSystemService(WINDOW_SERVICE))
                    .removeView(mView);
            mView = null;
        }
    }
}

class ButtonView extends ViewGroup {
    private Paint mLoadPaint;
    private Rect r;
    private Bitmap bit;

    public ButtonView(Context context, Bitmap bit) {
        super(context);
        Toast.makeText(context, "HUDView", Toast.LENGTH_LONG).show();

        mLoadPaint = new Paint();
        mLoadPaint.setAntiAlias(true);
        mLoadPaint.setTextSize(10);
        mLoadPaint.setARGB(255, 255, 0, 0);
        r = new Rect();
        r.set(380, 134, 468, 213);
        this.bit = bit;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawColor(Color.BLACK);

        canvas.drawBitmap(bit, 100, 100, null);
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int area = bit.getWidth() * bit.getHeight();


        //if (event.getY() <= maxY && event.getX() <= maxX) {
            Toast.makeText(getContext(), "Open tools: ", Toast.LENGTH_LONG)
                    .show();

        //}
        return true;

    }
}

最佳答案

考虑直接使用FrameLayout(或ViewGroup的任何其他子类)而不是ViewGroup。因为您当前的onLayout方法实现不正确,这将导致您在显示 subview 时遇到问题。

现在,更接近您的问题。您应该初始化Rect并只存储Bitmap的左,上,右和下位置。如我所见,当前您已初始化r变量,但未在任何地方使用它。

因此,您可以像这样初始化它:

r = new Rect(100, 100, 100 + bit.getWidth(), 100 + bit.getHeight());

现在,在onTouchEvent中,您可以检查:
r.contains((int) event.getX(), (int) event.getY());

07-25 21:08