• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Duplicate View after animation

I am working on an app that involves some simple translation animation. The animation works as expected, but when it ends, a second copy of the animated ImageView shows up. I suspect I am missing something really simple, Can anyone help me out?? I have a simplistic version of my app that displays the problem. Here is the code:
Code:
public class MainActivity extends Activity {
    LinearLayout verticalPane;
    RelativeLayout animPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_android_play);
        verticalPane = (LinearLayout) findViewById(R.id.ovc0);
        animPane = (RelativeLayout) findViewById(R.id.animLayout);
        Button button = (Button) findViewById(R.id.askPartnerButton);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                doAnimation();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void doAnimation() {
        
        // create a View to be animated
        final ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.c1);
        iv.setAdjustViewBounds(true);
        iv.setScaleType(ImageView.ScaleType.FIT_START);
        iv.setMaxHeight(64);
        iv.setMaxWidth(48);
        LayoutParams params = new LayoutParams(48, 64);
        iv.setLayoutParams(params);
        iv.setScaleX(1.0f);
        iv.setScaleY(1.0f);

        // start location is off screen to the right
        int startx = animPane.getWidth() + 48;
        int starty = animPane.getHeight() / 2;
        
        // end location is in a vertical LinearLayout
        int endx = verticalPane.getLeft();
        int endy = verticalPane.getTop();
        
        // set up layout params for the Relative Layout
        android.widget.RelativeLayout.LayoutParams p2 = new android.widget.RelativeLayout.LayoutParams(startx, starty);
        p2.setMargins(startx, starty, 0, 0);
        p2.height = 64;
        p2.width = 48;
        animPane.addView(iv, p2);
        
        // set up the animation, just a translation from offscreen to a vertical LinearLayout
        TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, endx - startx,
                Animation.ABSOLUTE, 0, Animation.ABSOLUTE, endy - starty);
        anim.setDuration(2000);
        iv.setAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            
            @Override
            public void onAnimationStart(Animation animation) {
            }
            
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
            
            @Override
            public void onAnimationEnd(Animation animation) {
                // move animated View to its final location in the vertical LinearLayout
                animPane.removeView(iv);
                verticalPane.addView(iv);
            }
        });
        anim.start();
    }

}

In the onAnimationEnd() method, I remove the animated view from my "animPane" (a RelativeLayout), and move it into "verticalPane" (a LinearLayout). The view is displayed as expected in the LinearLayout, but another copy of it appears in the upper left corner of my screen. Can't figure out how to get rid of it. I am using a RelativeLayout that contains just a GridLayout (which contains the rest of my layout). The only reason for the RelativeLayout is to allow animations to start offscreen. My layout is a bit lengthy (lots of LinearLayouts). If you need to see the actual layout, I will post it.
 
I guess I'm the only one seeing this problem. In case anyone else stumbles upon it, the only way I found to eliminate the unwanted copy of my translated view was to destroy the translated view, and create a new copy of it to add to the destination LinearLayout.
 
I was just going to suggest that you destroy the previous view before it switches to the LinearLayout. But, hey you figured it out! :)
 
Back
Top Bottom