3

I have a view that I inflate when a use clicks a button, and I want to animate the view to the screen when the button is clicked, I have tried the code below but the view just snaps to the screen instead of animates on to the screen. I am not sure why the view will not animate into position. The view I want to animate is previewMemberPanel

 previewMemberPanel = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.main_member_listing_imagepreview, null, false);

 //button click body
    Animation anim = new TranslateAnimation(new DisplayMetrics().widthPixels / 2, 0, 0, 0);
    anim.setFillAfter(true);
    anim.setDuration(5000);
    anim.setStartOffset(500);
    if( !previewMemberPanel.isShown() ){
        ((LinearLayout) findViewById(R.id.rootMembers)).addView(previewMemberPanel);          
      previewMemberPanel.startAnimation(anim);
    }
kabuto178
  • 3,129
  • 3
  • 40
  • 61

1 Answers1

1

Use a handler to post the animation:

((LinearLayout) findViewById(R.id.rootMembers)).addView(previewMemberPanel);

new Handler().post(new Runnable() {
  public void run() {
    previewMemberPanel.startAnimation(anim);
  }
});
eski
  • 7,917
  • 1
  • 23
  • 34
  • This did the trick for me. My guess is after you inflate a view, it still isn't displayed, so you need to start the animation only after the view has completely finished the inflation. – Bip901 Jan 18 '20 at 14:36