Android, setVisbility to gone not working in Relat

2019-01-24 13:11发布

问题:

I am using a RelativeLayout to put a rotating spinner animation on top of a placeholder image while the real image is being loaded in a background thread.

When the real bitmap is ready, the code below is run with in the UI thread with Activity.runOnUiThread(Runnable)

The problem is, that all my attempts to hide the rotating image after the real image was loaded seem to fail. I got it working finally by bringing the underlying imageview to the front, but I'm just curious why setVisibility(View.GONE) and the other methods I tried are not working. I tried various methods trying to hide the rotating animation, but none of them worked.

The spinner image is declared simply in an ImageView as:

android:src="@drawable/spinner_black_20"

Then I start an rotation animation on it. Once the real image is loaded, this is where I try to hide it (in the UI thread as mentioned above)

View v = (View)imageView.getParent();
ImageView spinner = (ImageView) v.findViewById(R.id.loading_spinner);
if (spinner != null) {
    spinner.getAnimation().cancel(); // this works, the animation stops
    spinner.setVisibility(View.INVISIBLE); // doesn't work, spinner bitmap still there
    spinner.setVisibility(View.GONE); // doesn't work
    spinner.getDrawable().setVisible(false, false); // tried this, no joy
    spinner.refreshDrawableState(); // thought this might help, nope
    spinner.invalidate(); // nor this
    imageView.bringToFront(); // this works, in that spinner disappears to background
}
imageView.setImageBitmap(bitmap);

回答1:

Try calling spinner.clearAnimation(); before the setVisibility.

Which is what Rich alluded to...