Bonjour, presque tout est dans le titre.
Débutant Xamarin sous Visual studio, j'ai écrit une visionneuse de photos du dossier DCIM pour mon téléphone. Elle fonctionne mais est "brut de décoffrage".
Première question : Quelle formule pour que les photos ne soient pas déformées ?
J'attends vos critiques et propositions pour la portabilité vers d'autres appareils Android.
Merci

<?xml version="1.0" encoding="utf-8"?>
<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"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="@string/PhraseBouton"
android:textSize="?android:attr/dialogPreferredPadding"
android:textStyle="bold"
android:typeface="normal"
android:textColor="@android:color/holo_red_light"
android:ellipsize="none"
android:gravity="top"
android:background="@android:color/holo_blue_light" />
<Button
android:text="Cliquez-moi !"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:clickable="true" />
<ImageView
android:src="@drawable/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitCenter" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="PhraseBouton">Cliquez pour afficher image</string>
<string name="ApplicationName">Mon Xamarin à moi !</string>
<string name="AcessoPath">Nom de chemin DCIM</string>
</resources>


using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Content.PM;
using System.Collections.Generic;
using Android.Graphics;

namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]

public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// CHEMIN DCIM : en path
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
// en string
string CHEMIN = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
string CHEMIN2 = string.Concat(CHEMIN, "/Camera");
// Tous les appreils android n'ont pas un sous dossier Camera ?
// Retourne les noms de fichiers et les place dans le tableau "photos"
string[] Photos = Directory.GetFiles(CHEMIN2, "*.jpg");
// Affiche le nombre de photos
int NbF = Photos.Length;
FindViewById<TextView>(Resource.Id.textView1).Text = NbF.ToString();
// Initialisation compteur affichage photos
int ComptPhotos = 0;
// Gestion de l'événement Button1
Button BTEV = FindViewById<Button>(Resource.Id.button1);
BTEV.Click += delegate
{
// Nom de fichier de la première photo
var filename = System.IO.Path.Combine(path.ToString(), Photos[ComptPhotos]);
// Affiche le nom du fichier
string SNomF = filename.ToString();
FindViewById<TextView>(Resource.Id.textView1).Text = SNomF;
// Affiche le fichier spécifié
Bitmap myBitmap = BitmapFactory.DecodeFile(SNomF);
// A réfléchir pour tous les appreils android
int widthscalled = FindViewById<ImageView>(Resource.Id.imageView1).Width;
int heightscalled = FindViewById<ImageView>(Resource.Id.imageView1).Height;
// Pour l'instant, les photos sont déformées...
Bitmap bitmapScalled = Bitmap.CreateScaledBitmap(myBitmap, widthscalled, heightscalled, true);
// scaletype dans axml
FindViewById<ImageView>(Resource.Id.imageView1).SetImageBitmap(bitmapScalled);
// Incrémenter compteur
ComptPhotos++;
if (ComptPhotos == NbF) { ComptPhotos = 0; };
// Vider la mémoire
myBitmap.Dispose();
bitmapScalled.Dispose();
// Quid quand on fait pivoter le téléphone ?
};
}
}
}