如何判断X和Y坐标是否在我的button内?

我已经非常困难地pipe理一个位图覆盖屏幕。 我也可以获得触摸input,但是屏幕上的每一个地方都可以获得触摸input。

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

服务和视图类如下。 我有想法和想法,但我想不出办法做到:(

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

现在,更接近你的问题。 您应该初始化Rect并存储Bitmap左,上,右和底部位置。 正如我所看到的,目前你已经初始化了rvariables,但没有在任何地方使用它。

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

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

现在在onTouchEvent你可以检查:

 r.contains((int) event.getX(), (int) event.getY()); 
 Rect rect = new Rect(); getHitRect(rect); if (rect.contains((int) event.getX(), (int) event.getY())) {} 

你可以使用getHitRect(Rect)。 它在父坐标中返回Hit矩形。 这里的文件

这适用于任何视图

 @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (inViewInBounds(myButtonView, (int) event.getRawX(), (int) event.getRawY())) { // User moved outside bounds Log.e("dispatchTouchEvent", "you touched inside button"); } else { Log.e("dispatchTouchEvent", "you touched outside button"); } } return super.dispatchTouchEvent(event); } Rect outRect = new Rect(); int[] location = new int[2]; private boolean inViewInBounds(View view, int x, int y) { view.getDrawingRect(outRect); view.getLocationOnScreen(location); outRect.offset(location[0], location[1]); return outRect.contains(x, y); } 

在你想检查的button上使用if语句和方法if(r.contains(x, y)) 。 当x和y点位于矩形r内时,此方法将返回true。 您也可以在该类中创build公共方法,以便您可以使用button对象引用在ButtonView类之外访问它。

当“触摸事件”发生时,它将通过所有视图的树。 如果你有线性布局和ImageView,用户在ImageView上触摸屏幕,然后触摸事件拦截,首先将在LinearLayour,然后在ImageView处理。

如果要阻止位图上的事件fe,则应该覆盖onTouchEvent的Bitmap并返回true值。 这将意味着你处理了这个事件,并且它将不可用于LinearLayout。

 image.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true; // attentively read documentation for onTouc interface } });