Why does the setVisibility function not work after watching an animated view?

Why doesn't textView become invisible?

Here is my xml layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvRotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotate Me" /> </LinearLayout> 

.. and here is my activity:

 public class RotateMeActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tvRotate = (TextView) findViewById(R.id.tvRotate); RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); r.setDuration(0); r.setFillAfter(true); tvRotate.startAnimation(r); tvRotate.setVisibility(View.INVISIBLE); } } 

My goal is to rotate the view and then hide and show it in code by setting setVisibility. The following steps, but setRotation are only available in API Level 11. I need a way to do this at API level 10.

 tvRotate.setRotation(180);//instead of the RotateAnimation, only works in API Level 11 tvRotate.setVisibility(View.INVISIBLE); 
+55
android visibility rotation animation rotateanimation
Dec 31 '11 at 19:12
source share
6 answers

For me, calling clearAnimation in View fixed the problem. In my case, I wanted to set the view back to its original position after completing the translation with fillAfter set to true.

+149
Mar 27 '12 at 9:07
source share

All animations (prior to android 3.0) are actually applied to the bitmap, which is a snapshot of your view instead of the original one. When you set the fill value after true, this means that the bitmap will be displayed on the screen instead of your view. That is why visibility will not change when using setVisibility , as well as the reason why your view will not receive touch events in its new (rotated) boundaries. (but since you rotate 180 degrees, this is not a problem).

+18
Dec 31 '11 at 19:19
source share

Another way around this is to wrap the animated view in a different view and set the visibility of this shell view.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/animationHoldingFrame" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tvRotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotate Me" /> </FrameLayout> </LinearLayout> 

And then the code will be as follows:

 TextView tvRotate = (TextView) findViewById(R.id.tvRotate); FrameLayout animationContainer = (FrameLayout)findViewById(R.id.animationHoldingFrame) RotateAnimation r = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); r.setDuration(0); r.setFillAfter(true); tvRotate.startAnimation(r); animationContainer.setVisibility(View.INVISIBLE); 
+7
Feb 25 2018-12-25T00:
source share

Use this before setVisibility after the animation finishes:

 anim.reverse(); anim.removeAllListeners(); anim.end(); anim.cancel(); 

where anim is your ObjectAnimator

but if you are using the Animation class, just do:

 view.clearAnimation(); 

in the view on which the animation was performed

+5
May 20 '16 at 10:00
source share

In the end, I needed API level 11 and using setRotation to do this. This seems like a fairly simple requirement, which, however, cannot be made in advance. All I wanted to do was rotate the button and then hide / show it.

+1
Jan 02 2018-12-12T00:
source share

I came up with a workaround for this: basically right before you call setVisibility (View.GONE), do an animation with a duration = 0 setFillAfter (false) and set the angle from / to the current rotation angle.

This will clear the setFillAfter bitmap and allow you to view the view.

+1
Jan 25 '12 at 1:30
source share



All Articles