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 :

creer deux animations en appuyant sur deux boutons différents


Sujet :

Composants graphiques Android

  1. #1
    Futur Membre du Club
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 6
    Points
    6
    Par défaut creer deux animations en appuyant sur deux boutons différents
    Bonjour,

    J'ai un souci avec mon code je developpe un jeu sur android studio et je rencontre le souci suivant.

    Il y a deuw boutons sur mon appli, un bouton Punch et un Bouton Kick et une Imageview dans laquelle j'anime mes images.

    Quand je clique sur le bouton punch, je cree une animation drawable de deux images

    quand je clique sur le bouton kick, je cree une animation drawable de deux images egalement.

    Le probleme est que quand j'execute ce code avec deux animations drawable, l'application crashe!!

    j'ai pourtant utiliser deux imageview differentes pour les deux animations

    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 AppCompatActivity {
     
        private Button btnPunch;
        private Button btnKick;
        private ImageView imageView1 ;
        private ImageView imageView2 ;
     
        private AnimationDrawable[] animationDrawable = new AnimationDrawable[2];
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            btnPunch = (Button) findViewById(R.id.btnPunch);
     
     
            imageView1 = (ImageView) findViewById(R.id.imageView1);
            imageView1.setBackgroundResource(R.drawable.animationpunch);
            animationDrawable[0] = (AnimationDrawable) imageView1.getBackground();
     
            imageView2 = (ImageView) findViewById(R.id.imageView2);
            imageView2.setBackgroundResource(R.drawable.animationkick);
            animationDrawable[1] = (AnimationDrawable) imageView2.getBackground();
     
     
            btnPunch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    animationDrawable[1].stop();
                    animationDrawable[0].setOneShot(true);
                    animationDrawable[0].start();
                }
            });
     
     
     
            btnKick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    animationDrawable[0].stop();
                    animationDrawable[1].setOneShot(true);
                    animationDrawable[1].start();
                }
            });
     
     
     
     
     
     
     
     
     
        }
     
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            //animationDrawable.start();
        }
    }

    par contre lorsque j'enleve une animation comme ceci, ca fonctionne bien mais du coup je suis limité a utiliser le bouton punch, je voudrais que ca marche avec les deux boutons


    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
     
     
    public class MainActivity extends AppCompatActivity {
     
        private Button btnPunch;
        private ImageView imageView1 ;
     
        private AnimationDrawable[] animationDrawable = new AnimationDrawable[2];
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            btnPunch = (Button) findViewById(R.id.btnPunch);
     
     
            imageView1 = (ImageView) findViewById(R.id.imageView1);
            imageView1.setBackgroundResource(R.drawable.animationpunch);
            animationDrawable[0] = (AnimationDrawable) imageView1.getBackground();
     
     
            btnPunch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    animationDrawable[1].stop();
                    animationDrawable[0].setOneShot(true);
                    animationDrawable[0].start();
                }
            });
     
     
        }
     
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            //animationDrawable.start();
        }
    }

    je clique sur bouton punch, ca anime mon animation unch

    je clique sur bouton kick, ca anime mon animation kick

    une aide?

  2. #2
    Modérateur
    Avatar de grunk
    Homme Profil pro
    Lead dév - Architecte
    Inscrit en
    Août 2003
    Messages
    6 691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Lead dév - Architecte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2003
    Messages : 6 691
    Points : 20 222
    Points
    20 222
    Par défaut
    Bonjour,

    Le probleme est que quand j'execute ce code avec deux animations drawable, l'application crashe!!
    Sans le message d'erreur présent dans le logcat , difficile de deviner le problème ...
    Pry Framework php5 | N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Réponses: 1
    Dernier message: 05/04/2017, 12h01
  2. Comment mettre sur deux lignes l'intitulé d'un bouton ?
    Par lolymeupy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 4
    Dernier message: 03/04/2008, 19h51
  3. deux fonctions en cliquant sur un bouton
    Par paolo2002 dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 21/01/2008, 12h26
  4. animation en pointant sur un bouton
    Par ph_anrys dans le forum Flash
    Réponses: 1
    Dernier message: 17/01/2007, 08h30
  5. Comment Appuyer sur le bouton d'une page web...?
    Par Mayti4 dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 23/01/2005, 14h07

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