Animation qui ne fonctionne qu'une seule fois.
Salut !
J'ai voulu faire un test: 4 boutons, lorsque l'on clique sur un bouton une anim apparait dont le texte change en fonction du bouton.
Sauf que l'animation s'execute correctement uniquement lorsque j'appuie pour la première fois sur un boutton, ensuite c'est comme si le "duration" n'était plus pris en compte.
Si je tourne le telephone en mode portrait ou paysage peu importe, la prochaine anim se relance de nouveau correctement puis rebolotte.
Voici mon code:
activity_main.xml
Code:
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
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
</LinearLayout>
<TextView
android:id="@+id/TextViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout> |
ma_premiere_anim.xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="20.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
<translate
android:fromXDelta="-400"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="200"
android:duration="3000"
/>
</set> |
MainActivity.java:
Code:
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
| import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button1, button2, button3, button4;
TextView textViewTest;
Animation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.Button1);
button2 = (Button) findViewById(R.id.Button2);
button3 = (Button) findViewById(R.id.Button3);
button4 = (Button) findViewById(R.id.Button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
textViewTest = (TextView) findViewById(R.id.TextViewTest);
animation = AnimationUtils.loadAnimation(this, R.anim.ma_premiere_anim);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.Button1:
textViewTest.setText("+69");
break;
case R.id.Button2:
textViewTest.setText("-18");
break;
case R.id.Button3:
textViewTest.setText("-32");
break;
case R.id.Button4:
textViewTest.setText("-24");
break;
}
textViewTest.setAnimation(animation);
}
} |
Quelqu'un a une idée du soucis ?