67 votes

Pourquoi setVisibility ne fonctionne-t-il pas après l'animation d'une vue ?

Pourquoi le textView ne devient-il pas invisible ?

Voici mon XML de mise en page :

 <?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>

..et voici mon activité :

 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);
    }
}

Mon objectif est de faire pivoter une vue, puis de pouvoir la masquer et l'afficher dans le code en définissant setVisibility. Ce qui suit fonctionne, mais setRotation n'est disponible que dans l'API Level 11. J'ai besoin d'un moyen de le faire dans l'API Level 10.

 tvRotate.setRotation(180);//instead of the RotateAnimation, only works in API Level 11
tvRotate.setVisibility(View.INVISIBLE);

171voto

jtietema Points 1403

Pour moi, appeler clearAnimation of the View a résolu le problème. Dans mon cas, je voulais remettre la vue à sa position d'origine après avoir effectué une traduction avec fillAfter défini sur true.

8voto

Chris Knight Points 7946

Une autre façon de contourner ce problème consiste à envelopper votre vue animée dans une autre vue et à définir la visibilité de cette vue d'enveloppe.

 <?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>

Et le code devient alors ceci :

 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);

1voto

ZippyFerguson Points 126

J'ai fini par exiger le niveau d'API 11 et utiliser setRotation pour y parvenir. Cela semble être une exigence assez simple qui ne peut pas être faite avant Honeycomb. Tout ce que je voulais faire était de faire pivoter un bouton, puis de le masquer/afficher.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X