Created
April 1, 2020 13:57
-
-
Save GravenilvecTV/866afbd81ba92cb2bfda6a8d17fc21f6 to your computer and use it in GitHub Desktop.
Correction 13/30 - Pecheur du dimanche - Android Exo 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/fondecran" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/compteur" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="40dp" | |
android:padding="10dp" | |
android:text="0" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent"/> | |
<ImageView | |
android:id="@+id/poisson" | |
android:layout_width="300dp" | |
android:layout_height="200dp" | |
android:src="@drawable/clown" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.gravendev.fisherclick; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
private int clicks = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// créer une liste qui va stocker temporairement chaque image de poisson | |
final List<Integer> listeImages = new ArrayList<>(); | |
listeImages.add(R.drawable.anguille); | |
listeImages.add(R.drawable.clown); | |
listeImages.add(R.drawable.crevette); | |
listeImages.add(R.drawable.epee); | |
listeImages.add(R.drawable.oursin); | |
listeImages.add(R.drawable.poulpe); | |
// faire le random | |
final Random random = new Random(); | |
// recuperer le compteur | |
final TextView textCompteur = findViewById(R.id.compteur); | |
// recuperer l'image de notre poisson | |
final ImageView imagePoisson = findViewById(R.id.poisson); | |
// lorsqu'on clique sur l'image du poisson | |
imagePoisson.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// ajouter un à ce compteur | |
clicks++; | |
// changer le texte du compteur | |
textCompteur.setText(String.valueOf(clicks)); | |
// choisir aleatoirement un poisson dans la liste | |
int limite = listeImages.size(); | |
int nombrehasard = random.nextInt(limite); | |
int resourcePoisson = listeImages.get(nombrehasard); | |
Drawable image = getResources().getDrawable(resourcePoisson); | |
// appliquer l'image à partir de cette ressource à la place du poisson | |
imagePoisson.setImageDrawable(image); | |
// d'afficher en console (debug) un message | |
Log.d("MainActivityP", "Fish’N Chips : " + nombrehasard); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment