Android custom object error

I created a custom view in Android to display the ball on the screen. Now I want, when I touch this ball, it should explode in four parts, and each part should move different four directions, i.e. up, down, left, right. I know that I have to install a touch listener to detect a touch on the ball, but then how to create an explosion effect? This issue has been resolved now . I show several balls on the screen so that the user can click on it and detonate them.

Here is my custom view:

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); } } 

SmallBall with similar properties except one, which is a direction and a break method, to move it in the direction and an animation flag to stop it moving.

 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(); } 

My xml layout is as follows:

 <?xml version="1.0" encoding="utf-8"?> <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" /> 

I add BallView and SmallBall to the activity class as follows:

 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); 

I add a few BallViews and their relative SmallBall array at different positions. Now, what happens no matter where I click on the screen, the latter added that BallView is starting to explode. After this second and so on. So here are 2 questions:

  • Why does it trigger the onClick / onTouch event no matter where I click on the screen? It should only trigger a listener event when I click on a specific BallView.
  • And secondly, why does BallView begin to explode in the opposite way, how are they added to the layout?

My listener class:

 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. } 

I cut the code due to the character restriction in question.

+6
source share
3 answers

I don’t think you need to hard code it all on canvas. You can call ball.setVisibility(View.GONE) in the Touch Listener and show 4 extra balls using smallBall.setVisibility(View.Visible) for each small ball. This way you can hide a large ball and show small balls. Now for the motion effect, there may be a method in each smallBall where you need to transfer the direction, and you can name it like this smallBall.explode (direction). Method implementation may be

 explode(int direction){//can be String if(direction=NORTH) y--; //other condition checks } 

The blast method will begin to change the x and y coordinates based on the direction traveled. Hope this gives you a hint on how to implement it.

+1
source

OK, the problem is creating BallView and SmalBall as View . I think I can fully explain this quite well, so bear with me:

A good place to start is a layered cake. Think about standing in the kitchen and making a cake like this: adding one layer at a time. First a layer of chocolate, and then a vanilla layer, and then another layer of chocolate. That sounds good? Good.

So, on Canvas a lot of circles are drawn, and separately, Ball objects are added as View s. Note that Canvas is simply a rectangle that the user can interact with, and is associated with View - the FrameLayout in your code. For all purposes and purposes, Canvas fully fills FrameLayout . In your application, Canvas is the bottom layer of the cake that fills the width and height of the ContentView (or pan).

Each call to m.addView(View v) essentially puts a new View v on top of the fact that many View objects are already contained in m . In terms of cake, each addView call adds another layer to our layer.

In your project, you want to add a layer to the pie, but do not fill it. You want to put a couple of strawberries on top, you don't need a full layer of strawberries.

So, by default, your BallView objects are full screen. Each of them overlays on top of the next, filling the entire canvas. When you click, it deletes the last one - because the last one is on top and occupies the width and height of the entire canvas - and in fact it is the only ball that the user can click on.

So, you should tell your BallView View objects the same value as the circle that you draw on the Canvas object.

So here are a couple of questions:

You can only perform absolute positioning using RelativeLayout . If you want the BallView user to be able to click on the circle drawn on the canvas, you need to tell the BallView where it is and dynamically change this position. You are currently using FrameLayout , which is necessary for Canvas , so in your XML, add the full screen <RelativeLayout> . And then in your code add BallView objects to this RelativeLayout .

and then, FINALLY:

Go here:

Set absolute view position

Good luck

+1
source

This all happens because you do not specify the LayoutParameters of the Ball object. What happens here when you add a Ball object to a Layout, it takes up the space of the entire layout and therefore responds to a click anywhere on the screen.

Now for the second problem, the reason is the same. Suppose the situation with the plates, it always appears in the LIFO style. Here, all Ball objects cover the entire screen, so treat them like plates, so what you do is put the plates on top of each other. And so it responds to the onClick method of the last ball, and then the second, etc.

Thus, your problem will be solved after setting the correct layout parameters for Ball objects.

0
source

Source: https://habr.com/ru/post/909876/


All Articles