从资产文件夹加载图像有不同的尺寸比RES /绘 [英] Image loaded from assets folder comes in different size than res/drawable

查看:186
本文介绍了从资产文件夹加载图像有不同的尺寸比RES /绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我dinamically加载图片来自绘制,华电国际到一个文件夹 ImageSwitcher 是这样的:

In my project I was loading images dinamically from the folder drawable-hdpi into an ImageSwitcher like this:

int[] images = new int[2];
logoImage = (ImageSwitcher) findViewById(R.id.logo_image);
images[0] = getResources().getIdentifier(ej.getImagemResource(), "drawable", getPackageName());
images[1] = getResources().getIdentifier(ej.getImagemResolvidaResource(), "drawable", getPackageName());
//...
logoImage.setImageResource(images[0]);

但对于设计的问题,因为它会像600百利特图片300×300像素,每个像素,我决定把它们全部注入资产的文件夹,并开始加载它们,如:

but for design issues, since it will be like 600 hundred little images 300 x 300 pixels each I decided to put them all into the assets folder and start to load them like:

Drawable[] images = new Drawable[2];
images[0] = Drawable.createFromStream(getAssets().open(ej.getImagemResource() + ".png"), null);
images[1] = Drawable.createFromStream(getAssets().open(ej.getImagemResolvidaResource() + ".png"), null);
//...

问题是,在第二种方式显示图像尺寸非常不同取决于器件密度(我猜的),但是当图像被放入绘制,华电国际的文件夹,他们中的任何密度显示就好了。

The problem is that in the second way the image size is displayed very differently depending on the device density (I guess), but when the images were into the drawable-hdpi folder they were displayed just fine in any density.

我该如何解决这个问题?或者是有有600几百张图片到绘制,华电国际文件夹中的一个问题?这是正确的方式做到这一点?

How do I solve this? Or is there a problem to have 600 hundred images into the drawable-hdpi folder? Which is the 'right' way to do this?

在此先感谢

推荐答案

当你把一个位图在 drawable-<密度> 文件夹中,并没有变种该设备的精确显示密度,框架会自动规模考虑它的比例<密度方式> 和设备的密度

When you put a bitmap in a drawable-<density> folder, and there is no variant for the device's exact display density, the framework will auto-scale it considering the ratio between <density> and the device's density.

这是这样做的图片尺寸,在DPS ,仍然设备(和你是不是从每个可能的密度提供一个变种义务)。

This is done so that the image dimensions, in dps, remains constant between devices (and that you are not obligated from providing a variant for each possible density).

当你从资产加载,源密度是未知的,所以不执行此自动缩放。因此,差

When you load from assets, the "source" density is unknown, so this autoscaling is not performed. Hence, the difference.

如果您想从资产加载图像的就好像它是华电国际的,你可以这样做:

If you want to load the image from the assets "as if it were hdpi", you can do something like:

Options opts = new BitmapFactory.Options();
opts.inDensity = DisplayMetrics.DENSITY_HIGH;
drawable = Drawable.createFromResourceStream(context.getResources(), null, is, srcName, opts);

这是说,我没有看到在所有的问题,包括绘制文件夹中的所有文件。

That said, I don't see any problems at all with including all the files in drawable folders.

这篇关于从资产文件夹加载图像有不同的尺寸比RES /绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆