4

I have this following code and everything work as follow. except that the remove view is not working. what is wrong with my code? does remove view do not work inside animation?

on my oncreate

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_edit_profile);

    mWinManager = (WindowManager) ManageProfileActivity.this
        .getSystemService(Context.WINDOW_SERVICE);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);

    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;

    mParentView = new FrameLayout(ManageProfileActivity.this);
    mParentViewlayout = new FrameLayout.LayoutParams(100, 100);

    mWinManager.addView(mParentView, params);

on my gridView Item Click

@Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

              ImageView original = (ImageView) view.findViewById(R.id.avatar_imageview);
              final ImageView img = new ImageView(ManageProfileActivity.this);
              img.setVisibility(View.VISIBLE);


              img.setImageDrawable(original.getDrawable());
              mParentView.addView(img, mParentViewlayout);

              int[] img_coordinates = new int[2];
              mGroupAvatarImageView.getLocationOnScreen(img_coordinates);
              float mX = (float) ((img_coordinates[0])); 
              float mY = (float) ((img_coordinates[1])); 

              TranslateAnimation anim = new TranslateAnimation(x, mX, y, mY);
              anim.setDuration(300);
              anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                   //THIS IS NOT WORKING
                  mParentView.removeView(img);
                  mParentView.invalidate();
                   }
      });

      img.startAnimation(anim);
user3487657
  • 553
  • 2
  • 8
  • 17

2 Answers2

1

You can't change the view hierarchy in onAnimationEnd. The workaround is to postpone deleting of the child view. Try to use smth like:

 @Override
 public void onAnimationEnd(Animation animation) {
    mParentView.post(new Runnable() {

        @Override
        public void run() {
            mParentView.removeView(img);
        }
    });
 }
Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
1

As stated by some other SO-questions (1, 2), you can't change the view hierarchy in onAnimationEnd(). However, there is no official source which states this odd behaviour...

To fix this problem, you have to use a Handler, which will execute your code on the UI-thread.

@Override
public void onAnimationEnd(Animation animation) {
    mParentView.post(new Runnable() {

        @Override
        public void run() {
            //this works now
            mParentView.removeView(img);
            mParentView.invalidate();
        }
    });
}
Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76