Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By setting an OnClickListener on this view, all of it will be clickable (though not limited to your bitmap). To check whether or not the user clicked only the bitmap itself you have to override onTouchEvent(MotionEvent event) and check if the touch coordinates are the same as the bitmap.</p> <pre><code>@Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: //Check if the x and y position of the touch is inside the bitmap if( x &gt; bitmapXPosition &amp;&amp; x &lt; bitmapXPosition + bitmapWidth &amp;&amp; y &gt; bitmapYPosition &amp;&amp; y &lt; bitmapYPosition + bitmapHeight ) { //Bitmap touched } return true; } return false; } </code></pre> <p>Just replace bitmapXPosition and bitmapYPosition with the coordinates you use to draw the bitmap, and bitmapWidth and bitmapHeight with the width and height you use to draw it.</p> <p>Also, try not to allocate memory (create objects) inside the onDraw() method of any view. It is bad for perfomance.</p> <p>EDIT</p> <pre><code>private Rect r; private Paint paint; Bitmap bitmap; public TestRect(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); paint.setColor(Color.BLUE); r = new Rect(); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); } public TestRect(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); paint.setColor(Color.BLUE); r = new Rect(); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); } public TestRect(Context context) { super(context); paint = new Paint(); paint.setColor(Color.BLUE); r = new Rect(); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); } @Override public void onDraw(Canvas c) { r.set(getWidth()/2, getHeight()/2, getWidth()/2 + 200, getHeight()/2 + 200); //c.drawRect(r, paint); c.drawBitmap(bitmap, null, r, paint); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: //Check if the x and y position of the touch is inside the bitmap if( x &gt; getWidth()/2 &amp;&amp; x &lt; getWidth()/2 + 200 &amp;&amp; y &gt; getHeight()/2 &amp;&amp; y &lt; getHeight()/2 + 200 ) { Log.e("TOUCHED", "X: " + x + " Y: " + y); //Bitmap touched } return true; } return false; } </code></pre> <p>By drawing the bitmap using a Rect as your coordinates you can check if the touch is inside the bitmap or not. Also, instead of that big and ugly "if" statement, you can use</p> <pre><code>if(r.contains(x, y)) { //BITMAP TOUCHED } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload