1

I have problems doing a small game which adds a lot of views in a ConstraintLayout (I already tested with FrameLayout, RelativeLayout and LinearLayout. First two with same result and Linear with very rare behaviour) and changes the size and position of the views.

Each game loop (33ms) some of the views are changing it's size and position, so I do this on the LayoutParams variable applied on the View on each loop with the new size.

params.width = realWidth;
params.height = realHeight;

and I do this for the position:

view.setX(realX);
view.setY(realY);

The problem is that the change is not reflected if I didn't call view.requestLayout() and that is a huge problem because requestLayout() is repaiting the parent layout and all its childrens, slowing down the game.

How can the size changes of a view be reflected without calling requestLayout? I read that you can achieve that with view.layout(). So I changed my code with this:

view.layout((int)realX, (int)realY, (int)realX+realWidth, (int)realY+realHeight);

The problem is that it works without calling requestLayout() but the view has a very rare behaviour using layout(), and when I add more views, the views become some milliseconds invisible and appear on the left top corner, after that they appear in the correct position, but suddenly they become invisible again and again and again etc in a very rare behaviour loop.

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

ConstraintLayout is slow, particularly if it contains many Views and much slower if it contains nested ConstraintLayouts.

Because of you are manually setting the position for your Views, you can use a different ViewGroup than ConstraintLayouts and set the absolute position with setX(), setY(), setTop(), etc.

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • I already tested with FrameLayout, RelativeLayout and LinearLayout. First two with same result and Linear with very rare behaviour – NullPointerException Oct 07 '19 at 07:35
  • Since you are developing a game and you are facing those performance issues, I suggest you to try using a game engine or programmatically draw these views without using an Android Layout but with pure Java code. – user2342558 Oct 07 '19 at 07:50