s.d
s.d

Reputation: 29436

Why setting visibility fails for Views with animation?

An ImageView is animated with a rotate animation :

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class Test extends Activity implements View.OnClickListener {
    private ImageView mIcon;
    private Animation mRotate;
    private boolean mShown = true;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mIcon = new ImageView(this);
        mIcon.setImageDrawable(
              getResources().getDrawable(android.R.drawable.ic_dialog_alert)
              );
        mIcon.setOnClickListener(this);

        setContentView(mIcon);


        mRotate = new RotateAnimation(
                  0f,360f,
                  getWindowManager().getDefaultDisplay().getWidth()/2,
                  getWindowManager().getDefaultDisplay().getHeight()/2
                  );
        mRotate.setDuration(2000);
        mRotate.setRepeatMode(Animation.RESTART);
        mRotate.setRepeatCount(Animation.INFINITE);
        mRotate.setInterpolator(new LinearInterpolator());

        mIcon.setAnimation(mRotate);
    }

    @Override
    public void onClick(View view) {
        if(mShown){
            mRotate.cancel();
            mIcon.setVisibility(View.GONE);
        }else {
            mIcon.setVisibility(View.VISIBLE);
            mRotate.reset();
            mRotate.start();
        }
        mShown = !mShown;
    }
}

The part mProgress.setVisibility(GONE); doesn't work. The ImageView doesn't hides at all. If I don't set the Animation for it, it works well.

My question is: why the animated view's visibility is not changing ?

UPDATE: Wrapping the View in a FrameLayout and setting FrameLayout's visibilty works. But still, this is an ugly workaround.

Upvotes: 3

Views: 723

Answers (4)

Akhil
Akhil

Reputation: 829

Try doing mIcon.clearAnimation() before you set the visibility to GONE.

Upvotes: 0

Ercan
Ercan

Reputation: 3705

Try to set mRotate.setFillAfter(false). your views visibility is really gone but the final state of your animation keeps its position.

Upvotes: 0

alejandrocordon
alejandrocordon

Reputation: 356

try

mProgress.setVisibility(View.GONE);

instead of

mProgress.setVisibility(GONE);

Upvotes: 0

slezadav
slezadav

Reputation: 6141

I think it works as it should. Let me explain. Important thing here is how your ImageView is animated. If you use for example TranslateAnimation (I guess this is your case) then the view you animate doesn't in fact change position, it is only moved elsewhere on the screen, so for example if you animate button this way and want to click it, you still have to click on its previous location. So in this case I think your ImageView is GONE, but you see only drawing made by animation. Using ObjectAnimator should solve this problem, because contrary to other animations it actually moves the view.

Upvotes: 2

Related Questions