Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't setVisibility work after a view is animated?

Why doesn't the textView become invisible?

Here is my layout xml:

<?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 a view and then be able to hide and show it in code by setting setVisibility. The following works, but setRotation is available only in API Level 11. I need a way to do it in API Level 10.

tvRotate.setRotation(180);//instead of the RotateAnimation, only works in API Level 11 tvRotate.setVisibility(View.INVISIBLE); 
like image 820
ZippyFerguson Avatar asked Dec 31 '11 19:12

ZippyFerguson


People also ask

How do I make animations visible on Android?

The easiest way to animate Visibility changes is use Transition API which available in support (androidx) package. Just call TransitionManager. beginDelayedTransition method then change visibility of the view. There are several default transitions like Fade , Slide .

How do you animate a view?

Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.

What is Animatelayoutchanges?

Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.


2 Answers

For me calling clearAnimation of the View fixed the problem. In my case I wanted to set the View back to its original position after doing a translation with fillAfter set to true.

like image 169
Jeroen Avatar answered Oct 15 '22 20:10

Jeroen


All the animations (before android 3.0) are actually applied to a bitmap which is a snapshot of your view instead of on your original view. When you are setting the fill after to true this actually means that the bitmap will continue to be displayed on the screen instead of your view. This is the reason why the visibility won't change upon using setVisibility and also the reason why your view will not be receiving touch events in its new (rotated) bounds. (but since you're rotating on 180 degrees that's not an issue).

like image 22
asenovm Avatar answered Oct 15 '22 21:10

asenovm