Exemples de code Android

Afficher une image

Ce petit programme affiche cinq fois la même image à l'aide du composant ImageView. L'attribut android:scaleType permet de redimensionner ou décentrer cette image. Cette dernière doit être de type "bmp", "gif", "jpeg" ou "png" et être stockée dans le répertoire "res/drawable".

MainActivity.java

Aucune modification nécessaire au fichier généré par Android Studio.

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/chien" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:scaleType="center"
        android:src="@drawable/chien" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:scaleType="centerCrop"
        android:src="@drawable/chien" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/chien" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:scaleType="matrix"
        android:src="@drawable/chien" />

</LinearLayout>

res/drawable/chien.jpg