0

Thanks in advance :)

Context

I have a game that is running at ~120 FPS and it dynamically creates multiple imageviews that move around the screen. These imageviews last for 5 seconds and then are made invisible and the code stops interacting with those objects. Then new Imageviews are created to replace them that last 5 seconds so on and so forth. In total I am making 4 objects every 5 seconds that create an imageView.

Problem

At a completely random time during the program the dynamic imageviews start to have 0 width and height so I can not see them on screen. I am not changing how the imageview object is being created at all and I can not find any correlation to when this starts to happen. Sometimes it takes 2 seconds to happen, sometimes 2 minutes. Also the other imageViews that are not dynamically created are completely fine. All the imageViews are on the same constraint layout.

What I Have Tried

I know for sure that the imageview's Visibility is VISIBLE and it is located at an x and y on screen. I also checked that the alpha is at 1F, that the problem wasn't the image itself by adding a background color and I checked that the imageView it is in front of all other views. I also tried cleaning the project both ways. I have also tried using requestLayout() and invalidate() on the imageviews. It also does not allow me to change the width and height once it disappears/becomes 0. For example in the code below it prints 0.

body.layoutParams.width = 20
body.layoutParams.height = 20
println(body.width)
//prints out 0

This is how I originally set up the imageView dynamically. Which works until it doesn't

body.layoutParams = LinearLayout.LayoutParams(size, size)
body.setImageResource(R.drawable.enemy_two)
body.x = ran.nextInt(screenWidth).toFloat()
body.y = ran.nextInt(screenHeight).toFloat()

main.runOnUiThread {
            layout.addView(body)
            body.visibility = View.VISIBLE}

Please let me know if you have an idea because I don't even know what to try anymore. This is for my first app so I might be missing something stupid. Thanks guys

Seth
  • 1
  • 1

1 Answers1

0

After modifying LayoutParams' properties try to set them once again onto target view. In other case the changes won't be applied.