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:
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
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) { } }); } });
Maintenant je souhaiterais pouvoir paramétrer mon animation afin de "setter" aléatoirement le nombre de tour que va faire ma roue.
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>
Pour cela, j'ai opté pour une option trouvée sur un autre forum, recoder la classe RotateAnimation.java.
Voici ma nouvelle classe:
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
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; } }
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...
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(); } }); } });
Si quelqu'un pourrait, ça serait vraiment sympa. Merci d'avance!
Partager