Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting touches on Views drawn on SurfaceView
    primarykey
    data
    text
    <p>I'm working on a game and ran into a bit of trouble, I might not be doing this correctly as I'm new to the graphic side of Android.</p> <p>I have a <code>SurfaceView</code> and an <code>ArrayList</code> of my own <code>Card</code> object which extends <code>View</code>. I override the <code>onDraw</code> method for the <code>Card</code> object, and then in the <code>SurfaceView's onDraw</code> I draw everything. The drawing part works as it should.</p> <p>I now try to detect when an individual card is touched using the <code>onTouchListener</code>, I set the listener for each card, but it detects the touch as if the view that being touched is the <code>SurfaceView</code>. It's possible that my whole way of thinking of this is wrong, so I'm asking for your advice.</p> <p>Some code:</p> <pre><code>public GameSurfaceView(Context context) { super(context); GAME_STATE = GameState.LOADING; this.context = context; holder = getHolder(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void surfaceCreated(SurfaceHolder holder) { Canvas c = holder.lockCanvas(null); onDraw(c); holder.unlockCanvasAndPost(c); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }); } </code></pre> <p>SurfaceView onDraw():</p> <pre><code>@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRGB(30, 180, 30); for (Card card : user.getTableCards()) { card.draw(canvas); } } </code></pre> <p>Card's onDraw():</p> <pre><code>@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(getBitmap(), x, y, null); } </code></pre> <p>and onTouch</p> <pre><code>@Override public boolean onTouch(View v, MotionEvent event) { Log.d("TOUCH", "Class: " + v.getClass().toString()); if (v.getClass() == Card.class) { Log.d("CARD", "Touched: " + ((Card) v).getValue()); } return true; } </code></pre> <p>I'm always getting the <code>SurfaceView</code> class being logged. Is this even the right way of doing something like that? I need to detect touch on individual objects of the same class..</p> <p>I currently do this by adding a member of type <code>Rect</code> to each card and then looping over and checking if the x and y of the touch are contained in that rect, but it seems like an expensive way, iterating over each card (even with max of 52) and checking it..</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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