简体   繁体   中英

How to change image according resolution?

I am working on an android application. i have used different layout folder. Like layout,layout-large,layout-xlarge. So that it can adjust for all resolutions. But i am setting images dynamically in activity. How can i change them according to screen resolution? How too big image will replace smaller if we change resolution?

Android works with density buckets, which go from l(low)dpi to xx(extra extra)h(high)dpi.

You want to create different versions of your images in folders as

  • drawable-ldpi
  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi
  • and drawable-xxhdpi if you want to support the Nexus 10.

That's kind of loose from the layout-large folders, which enable you to define different layouts for different sizes.

2 pretty different things, which you can read much more about at screen practices in Android .

======= Edit; Seems this wasn't exactly the question.

If you're doing it 'the right way' the Android system will choose the correct image for you, even when adding them dynamically (you can still call R.drawable.my_image from java code).

If for some reason you do have to choose, you can simply check for the current density with something like (a little outdated);

public String getDensity() {
    String density;
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    // will either be DENSITY_LOW (120) , DENSITY_MEDIUM (160) or
    // DENSITY_HIGH (240)
    switch (dm.densityDpi) {
    case 120:
        density = "ldpi";
        break;
    case 160:
        density = "mdpi";
        break;
    case 240:
        density = "hdpi";
        break;
    // use hdpi as default, because flurry shows this will be suited for
    // most of our users.
    default:
        density = "hdpi";
        break;
    }
    return density;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM