简体   繁体   English

Android可绘制资源问题

[英]Android drawable resources issue

I've noticed that real device picks up different resource from that eclipse shows in preview. 我注意到真实设备从预览中的eclipse中获取了不同的资源。 For example I created drawable resources for devices with smallest width of 720dp as 例如,我为最小宽度为720dp的设备创建了可绘制资源

  • drawable-sw720dp-mdpi for Samsung Galaxy Tab 1280x800 mdpi drawable-sw720dp-mdpi于三星Galaxy Tab 1280x800 mdpi的drawable-sw720dp-mdpi mdpi
  • drawable-sw720dp-hdpi for Nexus 10 2560x1600 hdpi. 适用于Nexus 10 2560x1600 hdpi的drawable-sw720dp-hdpi hdpi。

But Samsung Galaxy Tab 1280x800 mdpi picks up the drawable-sw720dp-hdpi instead of drawable-sw720dp-mdpi ? 三星Galaxy Tab 1280x800 mdpi选择了drawable-sw720dp-hdpi而不是drawable-sw720dp-mdpi

Same thing with other devices. 与其他设备相同。 If there are resources drawable-sw320dp-hdpi , drawable-sw320dp-xhdpi , the Sony Ericsson xperia-ARC 854x480 hdpi picks up drawable-sw320dp-xhdpi ? 如果有资源drawable-sw320dp-hdpidrawable-sw320dp-xhdpi索尼爱立信xperia-ARC 854x480 hdpi会选择drawable-sw320dp-xhdpi

I cant just keep drawable-hdpi resource because there are devices 5'5 inch screen size. 我不能保留drawable-hdpi资源,因为有5'5英寸屏幕尺寸的设备。 For example samsung galaxy 1280x720 hdpi 5'5 inch, and for such devices there is different graphical design so both devices 854x480 hdpi and 1280x720 hdpi will be using the same directory drawable-hdpi wich is not acceptable. 例如samsung galaxy 1280x720 hdpi 5'5英寸,并且对于这样的设备有不同的图形设计,因此两个设备854x480 hdpi和1280x720 hdpi将使用相同的目录drawable-hdpi wich是不可接受的。

Any ideas? 有任何想法吗?

Some time ago I had similar problem, while writing animated wallpaper which was using canvas and bitmaps. 前段时间我有类似的问题,同时编写使用画布和位图的动画壁纸。 I couldn't achieve what I wanted using default layout folders. 我无法使用默认布局文件夹实现我想要的效果。

In my case, for example, medium density device with xlarge screen (1280x800), should use the same resources as high density with normal screen (600x1024). 在我的情况下,例如,具有xlarge屏幕(1280x800)的中密度设备应使用与普通屏幕(600x1024)的高密度相同的资源。

I've decided to write my own solution, by putting resources into three groups and selecting them using custom rules. 我决定编写自己的解决方案,将资源分为三组,并使用自定义规则选择它们。

DisplayMetrics metrics = resources.getDisplayMetrics();
int screenSize = resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
    if (screenSize == Configuration.SCREENLAYOUT_SIZE_SMALL || screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
        return new SmallResourceProvider(resources);
    }
case DisplayMetrics.DENSITY_MEDIUM:
    if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
        return new MediumResourceProvider(resources);
    }
default:
    return new LargeResourceProvider(resources);
}

Maybe this will be helpful in your case. 也许这会对你的情况有所帮助。

I was also facing the same strange issue. 我也面临同样的奇怪问题。 In my case Galaxy tab GT P7500 was picking images from drawable-sw720dp-XXHDPI folder as i had the XXHDPI folder also. 在我的情况下,Galaxy选项卡GT P7500从drawable-sw720dp-XXHDPI文件夹中选取图像,因为我也有XXHDPI文件夹。 If i deleted the XXHDPI, it picks up the next lower which is the XHDPI folder. 如果我删除了XXHDPI,它会选择下一个较低的XHDPI文件夹。

The issue is with the swxxxdp qualifier for drawable folders. 问题在于可绘制文件夹的swxxxdp限定符。 If i remove the screen width and use just 1 set of images of all densities the issue seems to be resolved. 如果我删除屏幕宽度并仅使用一组所有密度的图像,问题似乎得到解决。 However i wanted to have different set of drawables for 7 inch and 10 inch devices. 但是,我希望为7英寸和10英寸设备提供不同的抽屉。 What i did was use a different set of names for the 10 inch images and 7 inch images and use 2 different set of layouts with sw600dp and sw720dp qualifiers. 我所做的是为10英寸图像和7英寸图像使用一组不同的名称,并使用sw600dp和sw720dp限定符使用2组不同的布局。 Here is my current structure which works well. 这是我目前的结构,运作良好。 However this is not the expected way of using the resources, as android recommends externalizing the drawables . 然而,这不是使用资源的预期方式,因为android建议外部化drawables unfortunately i couldn't find a better solution. 不幸的是,我找不到更好的解决方案。

layout-land
layout-sw600dp-land (uses image.png)
layout-sw720dp-land (image_large.png)

and for drawables, 对于drawables,

drawble-mdpi (image.png & image_large.png)
drawable-hdpi 
drawable-xhdpi
drawable-xxhdpi

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

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