0

Je reçois des données json. Dans ce JSON j'ai un tableau d'images urls pour différentes résolutions d'écran pour une image comme celleandroid afficher des images (différentes résolutions d'écran) sur ImageView à partir de données json

"images": { 
       "full_url": "http://******/assets/tmp/", 
       "ldpi": "http://******/assets/tmp/ldpi/****.png", 
       "mdpi": "http://******/assets/tmp/mdpi/****.png", 
       "hdpi": "http://******/assets/tmp/hdpi/****.png", 
       "xdpi": "http://******/assets/tmp/xdpi/****.png" 
      } 

je veux afficher cette image en fonction de la taille de l'écran

Répondre

1

Vous pouvez utiliser comme ci-dessous,

float density = getResources().getDisplayMetrics().densityDpi; 

//DisplayMetrics.DENSITY_LOW - LDPI(120) 
//DisplayMetrics.DENSITY_MEDIUM - MDPI(160) 
//DisplayMetrics.DENSITY_HIGH - HDPI(240) 
//DisplayMetrics.DENSITY_XHIGH - XHDPI(320) 
//DisplayMetrics.DENSITY_XXHIGH - XXHDPI(480) 
//DisplayMetrics.DENSITY_XXXHIGH - XXXHDPI(640) 

en fonction de la densité, vous pouvez ajouter l'image, réf this aussi.

Ex;

if(density == DisplayMetrics.DENSITY_MEDIUM) { 
    // Use mdpi image in your JSON 
} else if(density == DisplayMetrics.DENSITY_XHIGH) { 
    // use xdpi image in your JSON 
} else if(density == DisplayMetrics.DENSITY_HIGH) { 
    // use xdpi image in your JSON 
} else { 
    // use full image 
} 
+0

Je veux savoir comment choisir l'URL à utiliser? –

+0

exemple ajouté jeter un oeil –

+0

Ok merci, je l'ai eu –