Tous les articles

Anime ton application

Le code java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void mauvaise_reponse(View v) {
        //Lancement de l'animation
        ImageView resultat = (ImageView) findViewById(R.id.resultat);
        resultat.setBackgroundResource(R.drawable.faux);
        resultat.setVisibility(View.VISIBLE);
        AlphaAnimation alpha2 = new AlphaAnimation(1f, 0f);
        alpha2.setDuration(2000);
        resultat.startAnimation(alpha2);

        //Disable les boutons
        Button bouton1 = (Button) findViewById(R.id.bouton1);
        Button bouton2 = (Button) findViewById(R.id.bouton2);
        bouton1.setClickable(false);
        bouton2.setClickable(false);

        //L'application attends 2 secondes avant de passer à la question suivante
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                question_suivante();
            }
        }, 2000);

    }

    public void bonne_reponse(View v) {
        //Lancement de l'animation
        ImageView resultat = (ImageView) findViewById(R.id.resultat);
        resultat.setBackgroundResource(R.drawable.vrai);
        resultat.setVisibility(View.VISIBLE);
        AlphaAnimation alpha2 = new AlphaAnimation(1f, 0f);
        alpha2.setDuration(2000);
        resultat.startAnimation(alpha2);

        //Disable les boutons
        Button bouton1 = (Button) findViewById(R.id.bouton1);
        Button bouton2 = (Button) findViewById(R.id.bouton2);
        bouton1.setClickable(false);
        bouton2.setClickable(false);

        //L'application attends 2 secondes avant de passer à la question suivante
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                question_suivante();
            }
        }, 2000);
    }

    public void question_suivante() {
        // Masquer l'image du résultat
        ImageView resultat = (ImageView) findViewById(R.id.resultat);
        resultat.setVisibility(View.INVISIBLE);

        //Réactiver les boutons
        Button bouton1 = (Button) findViewById(R.id.bouton1);
        Button bouton2 = (Button) findViewById(R.id.bouton2);
        bouton1.setClickable(true);
        bouton2.setClickable(true);

        //Changer la question et les réponses
        TextView question = (TextView) findViewById(R.id.question);
        question.setText("En quelle année s'est déroulée la bataille d'Austerlitz ?");
        bouton1.setText("1789");
        bouton2.setText("1805");
    }
}

Le code XML :

<?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"
    tools:context="com.example.formation.animationtransition.MainActivity"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/resultat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/vrai"
        android:visibility="invisible"
        />

    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quelle est la capitale de l'Allemagne ?"
        android:textAlignment="center"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp">

        <Button
            android:id="@+id/bouton1"
            android:layout_width="120dp"
            android:layout_height="60dp"
            android:text="Londres"
            android:background="#f6d76a"
            android:textColor="#c96157"
            android:onClick="mauvaise_reponse"/>

        <Button
            android:id="@+id/bouton2"
            android:layout_width="120dp"
            android:layout_height="60dp"
            android:text="Berlin"
            android:background="#e27555"
            android:textColor="#eba560"
            android:onClick="bonne_reponse"/>

    </LinearLayout>

</LinearLayout>

 

Les ressources :

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *