如何使用ontouchlistener识别x和y坐标是否在矩形中?

问题描述

|
public void hatdraw(Canvas canvas,float x,float y) {
     mBitmaps = BitmapFactory.decodeResource(this.getResources(),R.drawable.hat);
     srcRect=new Rect(0,60,60);
     xrect=(int)x;
     yrect=(int)y;
    Log.d(\"hatdraw\",\"xrect,yrect\"+xrect    +yrect);
    desRect=new Rect(xrect,yrect,xrect+ (srcRect.right - srcRect.left),yrect + (srcRect.bottom -srcRect.top));
    canvas.drawBitmap(mBitmaps,srcRect,desRect,null);


}
    

解决方法

我不知道所发布的代码与该问题的标题有什么关系,但是请看一下该测试的
Rect.contains(int x,int y)
(或
RectF
中的等效方法)。     ,接触位图...
if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth()) 
    && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight()))
{
    // if this is true,you\'ve started your click inside your bitmap
}