Good day. This method implements the creation of an ImageView array, takes the animations from the class and plays them. How can I remove unnecessary ImageView after use? Now they just remain in the starting positions.

public void ExplosiveInCurPos(int x, int y) { MassAnimDead = null; MassAnimDead = new ImageView[r.nextInt(20)+2]; deadAnim = null; deadAnim = new Animdead(MassAnimDead.length); for (int i = 0; i<MassAnimDead.length; i++) { MassAnimDead[i] = RandLoadImage(1); FrameLayout.LayoutParams Anim = new FrameLayout.LayoutParams(sizeReturn(1)/50,sizeReturn(1)/50); MassAnimDead[i].setAnimation(deadAnim.getAnim(i)); Anim.leftMargin = x; Anim.topMargin = y; if ((i==1)||(i==2)) { Anim.leftMargin += sizeReturn(1)/16; Anim.topMargin += sizeReturn(1)/16; } MassAnimDead[i].setLayoutParams(Anim); StarScreen.addView(MassAnimDead[i]); } new CountDownTimer(500,500) { @Override public void onTick(long arg0) { } @Override public void onFinish() { for (int i=0; i<MassAnimDead.length; i++) { StarScreen.removeViewAt(MassAnimDead[i].getId()); } } }.start(); } 

    1 answer 1

    Any View can be deleted using this code.

     View v; ... ((ViewGroup)v.getParent()).removeView(v); 

    That is, in your case, you should delete them as follows:

     for (int i=0; i<MassAnimDead.length; i++) { ((ViewGroup)MassAnimDead[i].getParent()).removeView(MassAnimDead[i]); } 

    A couple of notes to the code:
    variable names must begin with lowercase letters
    function names must begin with lowercase letters
    In my opinion, this code is the wildest bike, invented for frame-by-frame animation using ImageView (I can be very wrong). If so, describe what you want to achieve with this code, and help you find the right solution ...

    • Thanks for the comments, I will continue to adhere to them. This thing makes some kind of explosion. massAnimDead displays a random number of fragments that fly in random directions. - Kota1921
    • StarScreen class object is your StarScreen ? I considered that the heir to ViewGroup - Vladyslav Matviienko
    • And here's another [link] [1] to the documentation on how to properly make an animation ... [1]: developer.android.com/guide/topics/graphics ... - Vladyslav Matviienko
    • StarScreen is the FrameLayout on which everything is drawn. Good, but how bad is this approach? Animation is generated separately in the class and transferred to the drawing. This code does not create an animation; it simply displays it. - Kota1921
    • If it is FrameLayout, then you can use the following code: StarScreen.removeView (MassAnimDead [i]); - Vladyslav Matviienko