/ RES和/资产目录的区别 [英] Difference between /res and /assets directories

查看:213
本文介绍了/ RES和/资产目录的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在 RES 目录是从 R.class访问的文件,而资产的行为就像一个文件系统,但我想知道,在一般情况下,当它的最好使用一个和另一个。
谁能帮我在明知资源和资产之间的真正区别是什么?

I know that files in the res directory are accessible from R.class while assets behaves like a file system, but I would like to know, in general, when it's best to use one and the other.
Can anyone help me in knowing the real differences between res and assets?

推荐答案

随着资源,有内置的支持,提供不同的语言,操作系统版本,屏幕方向等的替代品,如<一个href="http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources">here.没有可用与资产。另外,API的许多部分支持使用资源标识符。最后,资源的名称都变成了那些在编译时检查常量字段名,所以有少之间为code和资源本身不匹配的机会。没有一个适用于资产。

With resources, there's built-in support for providing alternatives for different languages, OS versions, screen orientations, etc., as described here. None of that is available with assets. Also, many parts of the API support the use of resource identifiers. Finally, the names of the resources are turned into constant field names that are checked at compile time, so there's less of an opportunity for mismatches between the code and the resources themselves. None of that applies to assets.

那么,为什么有资产的文件夹呢?如果要计算你需要在运行时使用的资产,这是pretty的方便。有了资源,你就必须申报所有可能被使用和计算的一个索引列表中的资源ID的列表。 (这是一种尴尬和引入了错误的机会,如果一组在开发周期的资源的变化。)(编辑:您可以通过名称来获取使用资源ID <一href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29"><$c$c>getIdentifier,但这种失去编译时检查的好处。)资产,也可以组织成一个文件夹层次结构,这是不支持的资源。这是管理数据的方式不同。虽然资源覆盖大多数的情况下,资产有其偶尔使用。

So why have an assets folder at all? If you want to compute the asset you want to use at run time, it's pretty easy. With resources, you would have to declare a list of all the resource IDs that might be used and compute an index into the the list. (This is kind of awkward and introduces opportunities for error if the set of resources changes in the development cycle.) ( you can retrieve a resource ID by name using getIdentifier, but this loses the benefits of compile-time checking.) 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.

另外一个区别:在库项目中定义的资源会自动导入到依赖于该库的应用项目。对于资产,这不会发生;资源文件必须是present中的应用程序项目(S)的资产目录。

One other difference: resources defined in a library project are automatically imported to application projects that depend on the library. For assets, that doesn't happen; asset files must be present in the assets directory of the application project(s).

编辑:如果要打包一个自定义的字体与您的应用程序还有一个区别就出现了。有 API调用以创建一个字样从存储>资产/ 目录中的文件系统或在您的应用程序的字样从存储在 RES / 目录(或从字体文件的InputStream ,这将允许使用 RES / 目录)。

Yet another difference arises if you want to package a custom font with your app. There are API calls to create a Typeface from a font file stored in the file system or in your app's assets/ directory. But there is no API to create a Typeface from a font file stored in the res/ directory (or from an InputStream, which would allow use of the res/ directory).

这篇关于/ RES和/资产目录的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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