Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom object click issue in android
    text
    copied!<p>I have created an custom view in android to display ball on screen. Now what I want is when I touch on that ball it should explode in four parts and every part should move different four directions ie up, down, left, right. I know I have to set touch listener to detect touch on ball but then how to create a explode effect?<sub>This issue is solved now</sub>. I'm displaying multiple balls on screen so that user can click on it and explode them.</p> <p>Here is my custom view:</p> <pre><code>public class BallView extends View { private float x; private float y; private final int r; public BallView(Context context, float x1, float y1, int r) { super(context); this.x = x1; this.y = y1; this.r = r; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(x, y, r, mPaint); } } </code></pre> <p>SmallBall with similar properties except one that is direction and an explode method to move it in direction and animation flag to stop it moving.</p> <pre><code>private final int direction; private boolean anim; public void explode() { // plus or minus x/y based on direction and stop animation if anim flag is false invalidate(); } </code></pre> <p>My layout xml is as follows:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout android:id="@+id/main_view" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF66FF33" /&gt; </code></pre> <p>I'm adding BallView and SmallBall to activity class as follows:</p> <pre><code>final FrameLayout mainFrameLayout = (FrameLayout) findViewById(R.id.main_frame_layout); SmallBall[] smallBalls = new SmallBall[4]; smallBalls[0] = new SmallBall(getApplicationContext(), 105, 100, 10, 1, false); smallBalls[0].setVisibility(View.GONE); mainFrameLayout .addView(smallBalls[0]); // create and add other 3 balls with different directions. BallView ball = new BallView(getApplicationContext(), 100, 100, 25, smallBalls); listener = new MyListener(ball); ball.setOnClickListener(listener); mainFrameLayout.addView(ball); </code></pre> <p>I'm adding multiple BallView and their relative SmallBall array at different positions. Now what happens no matter where I click on screen last added BallView starts to explode. After that second last, and so on. So here are 2 issues: </p> <ol> <li>Why it call onClick/onTouch event no matter where I click on screen? It should only call listener event when I click on particular BallView. </li> <li>And second is why BallView starts exploding in reverse manner of how they are added to layout?</li> </ol> <p>My listener class:</p> <pre><code>public void onClick(View v) { BallView ballView = (BallView) v; ballView.setVisibility(View.GONE); //get small balls associated with this ball. //loop through small ball and call their explode method. } </code></pre> <p><sub>I've trimmed code due to character limitation in question.</sub></p>
 

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