Android:从资源/可绘制文件夹访问图像

时间:2021-02-01 00:33:14

The app I am currently working on has hundreds of images. At the moment I store them in the 'Drawable' folder. I was thinking about moving all of them to Assets folder.

我目前正在处理的应用程序有数百张图片。目前我将它们存储在'Drawable'文件夹中。我正考虑将所有这些文件移到Assets文件夹中。

My question is: Is there any difference in performance when using both approaches?

我的问题是:使用这两种方法时,性能有什么不同吗?

4 个解决方案

#1


10  

I don't think so there is bit difference in performance of using these two folders, I think using drawable folder you can get easily images (All will be indexed in the R file, which makes it much faster (and much easier!) to load them.), and If you want to use it from asset then you have to use AssetManager then using AssetFileDescriptor you have to get those images.

我不认为使用这两个文件夹的性能有点差异,我认为使用drawable文件夹可以轻松获得图像(所有将在R文件中编入索引,这使得它更快(更容易!)到加载它们。),如果你想从资产中使用它,那么你必须使用AssetManager然后使用AssetFileDescriptor你必须得到那些图像。

  • Assets can also be organized into a folder hierarchy, which is not supported by resources. It's a different way of managing data. Although resources cover most of the cases, assets have their occasional use.

    资产也可以组织到文件夹层次结构中,资源不支持。这是一种管理数据的不同方式。虽然资源涵盖了大多数情况,但资产偶尔会被使用。

  • In the res/drawable directory each file is given a pre-compiled ID which can be accessed easily through R.id.[res id]. This is useful to quickly and easily access images, sounds, icons...

    在res / drawable目录中,每个文件都有一个预编译的ID,可以通过R.id. [res id]轻松访问。这对于快速轻松地访问图像,声音,图标非常有用......

#2


3  

As far as I know, there shouldn't be big difference but I would rather go with Drawable.

据我所知,应该没有太大的区别,但我宁愿选择Drawable。

Drawable gives you, beside indexing, option to fight with screen density fragmentation and Assets shouldn't depend on density. Having big pictures on smaller screens and/or lower specification phones (RAM, CPU) can cause real trouble and influence usability and app responsivity.

除了索引之外,Drawable还可以选择与屏幕密度碎片作斗争,资产不应该依赖于密度。在较小的屏幕和/或较低规格的手机(RAM,CPU)上放置大图片可能会导致真正的麻烦并影响可用性和应用响应度。

#3


0  

I think that the main difference between these two is that Drawable folder is indexed and you can gain use of the android Alternative resources load... So I think that there is no difference of performance between these resource folders.

我认为这两者之间的主要区别在于Drawable文件夹已编入索引并且您可以使用android替代资源加载......所以我认为这些资源文件夹之间没有性能差异。

#4


0  

Actually, i also met the problems. i want my App's ImageView's image is loaded from assets/ instead of res/drawables, so that i am able to update my APP from internet dynamically.

实际上,我也遇到了问题。我希望我的应用程序ImageView的图像是从assets /而不是res / drawables加载的,这样我就可以动态地从互联网上更新我的APP。

From my test/feeling the performance is likely to be very different if you do not pay attention.

从我的测试/感觉来看,如果你不注意,表现可能会非常不同。

         //v.setImageBitmap(ImageUtil.getBitmapFromAsset(this, "data/dog/dog_0001.png", 60, 60));
        //v.setImageDrawable(ImageUtil.getDrawableFromAsset(this, "data/dog/dog_0001.png"));
        v.setImageResource(R.drawable.dog_0001);

my app will do above in Activity's onCreate. it seems proved that setImageResource is fastest.

我的应用程序将在Activity的onCreate上面执行。似乎证明setImageResource是最快的。

the reason from my point is that the decode from asset will take time which will block UI thread, while set the resource ID is faster than the decoding. i saw the code in setImageResource which seems also load the image. i am not sure why my decode is slower than setImageResource. but i do feel setImageResource is fastest. i am trying to let the decode asyncly decode and then set bitmap.

我的观点是,从资产解码需要时间来阻止UI线程,而设置资源ID比解码更快。我看到setImageResource中的代码似乎也加载了图像。我不知道为什么我的解码比setImageResource慢。但我觉得setImageResource是最快的。我试图让解码异步解码,然后设置位图。

#1


10  

I don't think so there is bit difference in performance of using these two folders, I think using drawable folder you can get easily images (All will be indexed in the R file, which makes it much faster (and much easier!) to load them.), and If you want to use it from asset then you have to use AssetManager then using AssetFileDescriptor you have to get those images.

我不认为使用这两个文件夹的性能有点差异,我认为使用drawable文件夹可以轻松获得图像(所有将在R文件中编入索引,这使得它更快(更容易!)到加载它们。),如果你想从资产中使用它,那么你必须使用AssetManager然后使用AssetFileDescriptor你必须得到那些图像。

  • Assets can also be organized into a folder hierarchy, which is not supported by resources. It's a different way of managing data. Although resources cover most of the cases, assets have their occasional use.

    资产也可以组织到文件夹层次结构中,资源不支持。这是一种管理数据的不同方式。虽然资源涵盖了大多数情况,但资产偶尔会被使用。

  • In the res/drawable directory each file is given a pre-compiled ID which can be accessed easily through R.id.[res id]. This is useful to quickly and easily access images, sounds, icons...

    在res / drawable目录中,每个文件都有一个预编译的ID,可以通过R.id. [res id]轻松访问。这对于快速轻松地访问图像,声音,图标非常有用......

#2


3  

As far as I know, there shouldn't be big difference but I would rather go with Drawable.

据我所知,应该没有太大的区别,但我宁愿选择Drawable。

Drawable gives you, beside indexing, option to fight with screen density fragmentation and Assets shouldn't depend on density. Having big pictures on smaller screens and/or lower specification phones (RAM, CPU) can cause real trouble and influence usability and app responsivity.

除了索引之外,Drawable还可以选择与屏幕密度碎片作斗争,资产不应该依赖于密度。在较小的屏幕和/或较低规格的手机(RAM,CPU)上放置大图片可能会导致真正的麻烦并影响可用性和应用响应度。

#3


0  

I think that the main difference between these two is that Drawable folder is indexed and you can gain use of the android Alternative resources load... So I think that there is no difference of performance between these resource folders.

我认为这两者之间的主要区别在于Drawable文件夹已编入索引并且您可以使用android替代资源加载......所以我认为这些资源文件夹之间没有性能差异。

#4


0  

Actually, i also met the problems. i want my App's ImageView's image is loaded from assets/ instead of res/drawables, so that i am able to update my APP from internet dynamically.

实际上,我也遇到了问题。我希望我的应用程序ImageView的图像是从assets /而不是res / drawables加载的,这样我就可以动态地从互联网上更新我的APP。

From my test/feeling the performance is likely to be very different if you do not pay attention.

从我的测试/感觉来看,如果你不注意,表现可能会非常不同。

         //v.setImageBitmap(ImageUtil.getBitmapFromAsset(this, "data/dog/dog_0001.png", 60, 60));
        //v.setImageDrawable(ImageUtil.getDrawableFromAsset(this, "data/dog/dog_0001.png"));
        v.setImageResource(R.drawable.dog_0001);

my app will do above in Activity's onCreate. it seems proved that setImageResource is fastest.

我的应用程序将在Activity的onCreate上面执行。似乎证明setImageResource是最快的。

the reason from my point is that the decode from asset will take time which will block UI thread, while set the resource ID is faster than the decoding. i saw the code in setImageResource which seems also load the image. i am not sure why my decode is slower than setImageResource. but i do feel setImageResource is fastest. i am trying to let the decode asyncly decode and then set bitmap.

我的观点是,从资产解码需要时间来阻止UI线程,而设置资源ID比解码更快。我看到setImageResource中的代码似乎也加载了图像。我不知道为什么我的解码比setImageResource慢。但我觉得setImageResource是最快的。我试图让解码异步解码,然后设置位图。