如何判断X和Y坐标是否在我的按钮内? [英] How to tell if an X and Y coordinate are inside my button?

查看:108
本文介绍了如何判断X和Y坐标是否在我的按钮内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经非常困难地设法在屏幕上叠加位图。我也可以获得触摸输入,但是它会在屏幕上为每个地方提供触摸输入。

I have managed, with great difficulty, to make a bitmap overlay the screen. I can also get touch input, however it gets touch input for EVERYWHERE on the screen.

我想知道我怎么能检查触摸是否在我身上位图,在屏幕上可见。

I want to know how I would be able to check if the touch was on my bitmap, which is visible on the screen.

服务和视图类如下所示。我已经思考过,但我想不出办法:(

The service and view class is below. I have thought and thought, but I couldn't think of a way to do it :(

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 方法不正确,这会导致显示子视图时出现问题。

Consider using FrameLayout (or any other subclass of ViewGroup) instead of ViewGroup directly. Because your current implementation of onLayout method is not correct, which will lead you to problems with displaying of child views.

现在,更接近你的问题。您应该初始化 Rect 并只存储位图的左,上,右和下位置。我可以看到,目前你初始化了 r 变量,但没有在任何地方使用它。

Now, closer to your question. You should ininitialize Rect and just store left, top, right and bottom position of your Bitmap. As I can see, currently you're initialized r variable, but not using it anywhere.

所以,你可以像这样初始化它:

So, you can initialize it like this:

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

现在在 onTouchEvent ,你可以检查:

Now in onTouchEvent you can just check:

r.contains((int) event.getX(), (int) event.getY());

这篇关于如何判断X和Y坐标是否在我的按钮内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆