简体   繁体   English

如何根据分辨率更改图像?

[英]How to change image according resolution?

I am working on an android application. 我正在开发一个android应用程序。 i have used different layout folder. 我使用了不同的布局文件夹。 Like layout,layout-large,layout-xlarge. 像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. Android使用密度存储桶,密度存储桶的范围从l(低)dpi到xx(额外)h(高)dpi。

You want to create different versions of your images in folders as 您要在文件夹中创建不同版本的图像

  • drawable-ldpi 可绘制ldpi
  • drawable-mdpi 绘图分辨率
  • drawable-hdpi drawable-hdpi
  • drawable-xhdpi drawable-xhdpi
  • and drawable-xxhdpi if you want to support the Nexus 10. 和drawable-xxhdpi(如果要支持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 . 2件截然不同的事情,您可以在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). 如果您以“正确的方式”进行操作,则即使是动态添加图像,Android系统也会为您选择正确的图像(您仍然可以从Java代码中调用R.drawable.my_image)。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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