IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

Problème d'animation avec ActionBar, mais pas avec bouton


Sujet :

Composants graphiques Android

  1. #1
    Membre averti
    Avatar de Heavy Metal Hero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juin 2007
    Messages : 152
    Points : 333
    Points
    333
    Billets dans le blog
    13
    Par défaut Problème d'animation avec ActionBar, mais pas avec bouton
    Bonjour,
    J'ai implémenté une sorte de sliding menu sans bibliothèque. Il est possible de le déplier ou de le fermer à partir d'un bouton dans la vue et d'un bouton dans l'actionBar. Les deux boutons appellent la même méthode (optionsAction()) pour provoquer l'animation. Voici mon code :

    MainActivity.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    public class MainActivity extends Activity {
        private LinearLayout menuParams;
        private Button btnToggleMenuList;
        private int screenWidth;
        private boolean isExpanded;
        private DisplayMetrics metrics;
     
        @Override
        protected void onCreate(Bundle pSavedInstanceState) {
            super.onCreate(pSavedInstanceState);
            setContentView(R.layout.activity_main);
     
            menuParams = (LinearLayout) findViewById(R.id.linearLayout2);
            btnToggleMenuList = (Button) findViewById(R.id.button1);
            metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            screenWidth = metrics.widthPixels;
    //bouton appelant l'animation, marche parfaitement
            btnToggleMenuList.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    optionsAction();
                }
            });
     
        }
    //fonction provoquant l'animation
        private void optionsAction() {
            if (!isExpanded) {
                isExpanded = true;
                menuParams.startAnimation(new ExpandAnimation(menuParams, 0,
                        (int) (screenWidth * 0.2), 5));
                Toast.makeText(getApplicationContext(),
                        "isExpanded : " + isExpanded, Toast.LENGTH_SHORT).show();
            } else {
                isExpanded = false;
                menuParams.startAnimation(new CollapseAnimation(menuParams, 0,
                        (int) (screenWidth * 0.2), 5));
                Toast.makeText(getApplicationContext(),
                        "isExpanded : " + isExpanded, Toast.LENGTH_SHORT).show();
            }
        }
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.main, menu);
          return true;
        }
        //Listener du bouton de l'action bar.
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case R.id.action_options:
              optionsAction();
            break;
          default:
            break;
          }
     
          return true;
        }
     
    }
    CollapseAnimation.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    public class CollapseAnimation extends Animation implements Animation.AnimationListener {
     
        private LinearLayout container;
        private static int ANIMATION_DURATION;
        private static int REPEAT_COUNT;
        private int LastWidth;
        private int FromWidth;
        private int ToWidth;
        private static int STEP_SIZE=30;
        public CollapseAnimation(LinearLayout pContainer, int pFromWidth, int pToWidth, int pDuration) {
     
            this.container = pContainer;
            LayoutParams lyp =  container.getLayoutParams();
            ANIMATION_DURATION = 1;
            REPEAT_COUNT = pDuration;
            this.FromWidth = lyp.width;
            this.ToWidth = lyp.width;
            setDuration(ANIMATION_DURATION);
            setRepeatCount(REPEAT_COUNT);
            setFillAfter(false);
            setInterpolator(new AccelerateInterpolator());
            setAnimationListener(this);
        }
     
        @Override
        public void onAnimationEnd(Animation animation) {
            LayoutParams lyp =  container.getLayoutParams();
            lyp.width = 0;
            container.setLayoutParams(lyp);
            LastWidth = 0;
        }
     
        @Override
        public void onAnimationRepeat(Animation animation) {
            LayoutParams lyp =  container.getLayoutParams();
            lyp.width = lyp.width - ToWidth/REPEAT_COUNT;
            container.setLayoutParams(lyp);
        }
     
        @Override
        public void onAnimationStart(Animation animation) {
            LayoutParams lyp =  container.getLayoutParams();
            LastWidth = lyp.width;
            container.setLayoutParams(lyp);
        }
     
    }
    ExpandAnimation.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    public class ExpandAnimation extends Animation implements Animation.AnimationListener {
     
        private LinearLayout container;
        private static int ANIMATION_DURATION;
        private static int REPEAT_COUNT;
        private int LastWidth;
        private int FromWidth;
        private int ToWidth;
        private static int STEP_SIZE=30;
        public ExpandAnimation(LinearLayout pContainer, int pFromWidth, int pToWidth, int pDuration) {
     
            this.container = pContainer;
            ANIMATION_DURATION = 1;
            REPEAT_COUNT = pDuration;
            this.FromWidth = pFromWidth;
            this.ToWidth = pToWidth;
            setDuration(ANIMATION_DURATION);
            setRepeatCount(REPEAT_COUNT);
            setFillAfter(false);
            setInterpolator(new AccelerateInterpolator());
            setAnimationListener(this);
        }
     
        @Override
        public void onAnimationEnd(Animation animation) {
            // Nothing
        }
     
        @Override
        public void onAnimationRepeat(Animation animation) {
            LayoutParams lyp =  container.getLayoutParams();
            lyp.width = LastWidth +=ToWidth/REPEAT_COUNT;
            container.setLayoutParams(lyp);
        }
        @Override
        public void onAnimationStart(Animation animation) {
            LayoutParams lyp =  container.getLayoutParams();
            lyp.width = FromWidth;
            container.setLayoutParams(lyp);
            LastWidth = FromWidth;
     
        }
     
    }
    Et le layout de l'activité, activity_main.xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
     
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
     
           <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="0.01"
                android:background="@color/MallowRCMS"
                android:orientation="vertical" >
     
            </LinearLayout>
     
            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="0.99"
                android:orientation="vertical" >
     
                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
     
                    <LinearLayout
                        android:id="@+id/linearLayout5"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >
    "
                        <Button
                            android:id="@+id/button1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="2dp"
                            android:text="ToggleMenu" />
     
    <ListView
                            android:id="@+id/listMenu"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >
     
                    </LinearLayout>
                </TableRow>
            </LinearLayout>
        </LinearLayout>
     
    </FrameLayout>
    Quand on clique sur le bouton, le menu se déroule. On reclique dessus, ils s'enroule et ainsi de suite, le bouton fonctionne normalement et appelle optionAction() qui gère les appels startAnimation avec les deux autres classes en paramètre.

    Cependant, ce n'est pas le cas de l'actionButton de l'action bar. La première fois, il déroule et enroule, puis c'est fini, plus rien. Il continue d'afficher les Toast normalement, donc il appelle toujours optionAction() mais ne lance plus les animations (pourtant les toast sont après les startAnimation) ! Il marche cependant quand le menu est déjà déroulé : il le renroule mais ne peut pas le dérouler, alors que le bouton peut...

    Comment est-ce possible alors qu'il fait strictement la même chose que l'autre listener ? A savoir appeler une fonction qui ne prends pas de paramètre (et qui est donc censée être indépendante de l'appelant) ?

    Merci d'avance

  2. #2
    Membre averti
    Avatar de Heavy Metal Hero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juin 2007
    Messages : 152
    Points : 333
    Points
    333
    Billets dans le blog
    13
    Par défaut
    J'ai trouvé une piste. Le problème vient de activity_main.xml. Quand j'enlève la liste d'ID "listMenu" (en bas du fichier), le comportement est bon. J'ai donc un problème d'agencement des layout. Après j'ai du mal à résoudre ce problème, toute bonne idée est la bienvenue

  3. #3
    Membre régulier
    Femme Profil pro
    Développeur Java
    Inscrit en
    Août 2013
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Août 2013
    Messages : 61
    Points : 105
    Points
    105
    Par défaut
    Ta balise ListView n'est pas fermée?

    Sinon, juste pour chipoter, c'est quoi l'apostrophe ligne 39?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ibatis] "No suitable driver" avec ibatis mais pas avec jdbc ?!
    Par vingtcent dans le forum Persistance des données
    Réponses: 6
    Dernier message: 13/12/2006, 17h00
  2. Sa marche avec Firefox mais pas avec IE ?
    Par boolat dans le forum Flash
    Réponses: 1
    Dernier message: 30/10/2006, 14h02
  3. Réponses: 13
    Dernier message: 06/11/2005, 10h45
  4. Fonction JS qui fonctionne avec Mozilla mais pas avec IE.
    Par etiennegaloup dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 31/10/2005, 13h58
  5. Erreur avec IE mais pas avec Netscape
    Par Oluha dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 16/02/2005, 15h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo