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

Android Discussion :

Problème d'animation sous Android


Sujet :

Android

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 7
    Points : 5
    Points
    5
    Par défaut Problème d'animation sous Android
    Bonjour à tous !

    Voila, depuis quelques temps je me suis mis à developper sous Android.
    En ce moment, je suis sur la découverte des animations et j'ai quelques soucis...

    J'ai créé une petite appli qui affiche deux images boutons représentant une roue à quatre choix.
    Sur ma première roue, j'utilise une rotation qui se fait au centre de l'image et ce, en chargement mon animation via un fichier XML.
    Cela fonctionne très bien, à la fois sous Eclipse ainsi que sur mon téléphone, à savoir c'est un Samsung ACE.
    Voici le code:
    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
    imageButton = (ImageButton) findViewById(R.id.imageButton1);
    imageButton.setOnClickListener(new View.OnClickListener() {
        		@Override
        		public void onClick(View view) {
        			// On crée un utilitaire de configuration pour cette animation
        	        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
        	        // On l'affecte au widget désiré, et on démarre l'animation
        	        imageButton.startAnimation(animation);  
        	        animation.setAnimationListener(new Animation.AnimationListener() {
        	        	@Override
        	            public void onAnimationEnd(Animation animation)
        	            {
        	            	Toast.makeText(MainActivity.this, "Premier test qui marche.", Toast.LENGTH_SHORT).show();
        	            }
     
        	            @Override
        	            public void onAnimationRepeat(Animation animation) { }
     
        	            @Override
        	            public void onAnimationStart(Animation animation) { }
        	        });
        		}
        	});
    Et voici le XML:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/animTest"
         android:fillAfter="true"
    	 android:fillEnabled="true">
    	<rotate
    	    android:interpolator="@android:anim/decelerate_interpolator"
    	    android:fromDegrees="0"
    	    android:pivotX="50%"
    	    android:pivotY="50%"
    	    android:toDegrees="1080"
    	    android:duration="5000" />
    </set>
    Maintenant je souhaiterais pouvoir paramétrer mon animation afin de "setter" aléatoirement le nombre de tour que va faire ma roue.
    Pour cela, j'ai opté pour une option trouvée sur un autre forum, recoder la classe RotateAnimation.java.
    Voici ma nouvelle classe:
    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    package com.test.testanimation;
     
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.animation.Animation;
    import android.view.animation.Transformation;
     
    public class MyRotateAnimation extends Animation {
    	private float mFromDegrees;
        private float mToDegrees;
        private int mPivotXType = ABSOLUTE;
        private int mPivotYType = ABSOLUTE;
        private float mPivotXValue = 0.0f;
        private float mPivotYValue = 0.0f;
        private float mPivotX;
        private float mPivotY;
     
     
        public MyRotateAnimation(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
     
        public MyRotateAnimation(float fromDegrees, float toDegrees) {
            mFromDegrees = fromDegrees;
            mToDegrees = toDegrees;
            mPivotX = 0.0f;
            mPivotY = 0.0f;
        }
     
        public MyRotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
            mFromDegrees = fromDegrees;
            mToDegrees = toDegrees;
     
            mPivotXType = ABSOLUTE;
            mPivotYType = ABSOLUTE;
            mPivotXValue = pivotX;
            mPivotYValue = pivotY;
        }
     
        public MyRotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
                int pivotYType, float pivotYValue) {
            mFromDegrees = fromDegrees;
            mToDegrees = toDegrees;
     
            mPivotXValue = pivotXValue;
            mPivotXType = pivotXType;
            mPivotYValue = pivotYValue;
            mPivotYType = pivotYType;
        }
     
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime);
     
            if (mPivotX == 0.0f && mPivotY == 0.0f) {
                t.getMatrix().setRotate(degrees);
            } else {
                t.getMatrix().setRotate(degrees, mPivotX, mPivotY);
            }
        }
     
        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
            mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
        }
     
        public synchronized float getmFromDegrees() {
            return mFromDegrees;
        }
     
        public synchronized void setmFromDegrees(float mFromDegrees) {
            this.mFromDegrees = mFromDegrees;
        }
     
        public synchronized float getmToDegrees() {
            return mToDegrees;
        }
     
        public synchronized void setmToDegrees(float mToDegrees) {
            this.mToDegrees = mToDegrees;
        }
     
        public synchronized int getmPivotXType() {
            return mPivotXType;
        }
     
        public synchronized void setmPivotXType(int mPivotXType) {
            this.mPivotXType = mPivotXType;
        }
     
        public synchronized int getmPivotYType() {
            return mPivotYType;
        }
     
        public synchronized void setmPivotYType(int mPivotYType) {
            this.mPivotYType = mPivotYType;
        }
     
        public synchronized float getmPivotXValue() {
            return mPivotXValue;
        }
     
        public synchronized void setmPivotXValue(float mPivotXValue) {
            this.mPivotXValue = mPivotXValue;
        }
     
        public synchronized float getmPivotYValue() {
            return mPivotYValue;
        }
     
        public synchronized void setmPivotYValue(float mPivotYValue) {
            this.mPivotYValue = mPivotYValue;
        }
     
        public synchronized float getmPivotX() {
            return mPivotX;
        }
     
        public synchronized void setmPivotX(float mPivotX) {
            this.mPivotX = mPivotX;
        }
     
        public synchronized float getmPivotY() {
            return mPivotY;
        }
     
        public synchronized void setmPivotY(float mPivotY) {
            this.mPivotY = mPivotY;
        }
    }
    Et voici comment je l'utilise dans mon appli:
    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
    imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
            imageButton2.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				// Creation du random
    				Random randomGenerator = new Random();
    		        randomInt = randomGenerator.nextInt(1080);
    				MyRotateAnimation rotateAnimation = new MyRotateAnimation(0, randomInt);
    				rotateAnimation.setmPivotXValue(100f);
    				rotateAnimation.setmPivotYValue(100f);
    				rotateAnimation.setFillAfter(true);
    				rotateAnimation.setFillEnabled(true);
    				rotateAnimation.setDuration(1000);
    				imageButton2.startAnimation(rotateAnimation);
    				rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
    					@Override
    					public void onAnimationStart(Animation animation) { }
     
    					@Override
    					public void onAnimationRepeat(Animation animation) { }
     
    					@Override
    					public void onAnimationEnd(Animation animation) {
        	            	Toast.makeText(MainActivity.this, "Deuxieme test avec comme valeur d'angle: "+randomInt, Toast.LENGTH_SHORT).show();
    					}
    				});
    			}
    	});
    Encore une fois, cela fonctionne correctement mais cette fois que sur Eclipse... Lorsque j'essaye mon application sur mon téléphone, la rotation ne s'effectue plus correctement. Le nombre de tour est bien "setter" de manière aléatoire mais maintenant la rotation ne s'effectue plus par le centre de l'image...

    Si quelqu'un pourrait, ça serait vraiment sympa. Merci d'avance!

  2. #2
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Essaie de déterminer la relation entre le point de rotation et la zone utilisée.
    Il est possible que ce soit bien le centre, mais que la zone n'est plus la même.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Merci pour ta rapidité Hizin !

    En effet, je pense que lorsque j'utilise mon appli sur mon téléphone, le bouton image doit être redimensionné et du coup je ne peux pas utiliser une valeur fixe pour ma rotation.

    Je pense qu'il faudrait plutôt utiliser quelque chose du genre "50%" comme je fais déjà dans mon premier test mais cela ne fonctionne pas dans mon deuxième test...

    Quelqu'un aurait une idée ?

  4. #4
    Modérateur
    Avatar de Hizin
    Homme Profil pro
    Développeur mobile
    Inscrit en
    Février 2010
    Messages
    2 180
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur mobile

    Informations forums :
    Inscription : Février 2010
    Messages : 2 180
    Points : 5 072
    Points
    5 072
    Par défaut
    Oui, ton image et l'affichage sont redimensionnées, dépendant du couple densité/résolution.

    Tu devrais passer par un pourcentage je pense.
    getMeasuredWidth(), getMeasuredHeight()... ce genre de primitive.
    C'est Android, PAS Androïd, ou Androïde didiou !
    Le premier est un OS, le second est la mauvaise orthographe du troisième, un mot français désignant un robot à forme humaine.

    Membre du comité contre la phrase "ça marche PAS" en titre et/ou explication de problème.

    N'oubliez pas de consulter les FAQ Android et les cours et tutoriels Android

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    D'ac' ! Je ne connais pas encore très bien la programmation sous Android donc je vais faire quelques recherches sur getMeasuredWidth() et getMeasuredHeight() afin de comprendre comment les utiliser!

    En tout cas, merci pour ton aide! Je regarde ça et je reviens ici pour dire si j'y arrive. Encore merci!

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 7
    Points : 5
    Points
    5
    Par défaut
    Encore MERCI Hizin !!! Ça fonction nickel !

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

Discussions similaires

  1. problème d'éxècussion sous Android
    Par nacefo dans le forum Mobiles
    Réponses: 0
    Dernier message: 21/07/2014, 15h29
  2. [WM17] problème d'affichage sous android
    Par minoo66 dans le forum Windev Mobile
    Réponses: 4
    Dernier message: 26/04/2013, 07h10
  3. Problème trigger SQLite sous Android
    Par Shma94 dans le forum SQLite
    Réponses: 1
    Dernier message: 24/06/2012, 22h14
  4. animation sous android
    Par amal8881 dans le forum Développement 2D, 3D et Jeux
    Réponses: 1
    Dernier message: 13/12/2011, 00h17
  5. Problème avec "Service" sous Android
    Par n2engineer5 dans le forum Android
    Réponses: 0
    Dernier message: 07/07/2011, 22h00

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