### Android 應用資源可分為兩大類
- 官方介紹,[這里](https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources),如果無法科學上網,請打開Google中國開發者網站,進入指南——[應用資源](https://developer.android.google.cn/guide/topics/resources/providing-resources)
- 無法通過R 資源清單類訪問的原生資源, 保存在assets 目錄下。
- 一般來說,assets 目錄下存放的資源代表應用無法直接訪問的原生資源, 應用程序需要通過AssetManager以二進制流的形式來讀取資源
- 可通過R 資源清單類訪問的資源, 保存在res 目錄下。
- res 目錄下的資源, Android SDK 會在編譯該應用時, 自動在R.java 交件中為這些資源創建索引,程序可直接通過R 資驚清單類進行訪問。
#### 資源的類型以及存儲方式
- 使用Android studio工具,如下圖所示

從圖中可以看到,當我們創建一個Android資源文件夾時,可以創建13個不同類型的資源分別是**anim、animator、color、drawable、font、interpolator、layout、menu、mipmap、raw、transition、values、xml**等不同類型的資源。(PS:使用Android studio2..3.3),具體每種資源的含義卡查閱[官網](https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources)
- 下表是Android瘋狂講義對不同資源的類型以及存儲方式的描述,由于官網的更新,可能有些描述不正確或者過時,僅供參考
| 目錄 |存放的資源 |
| --- | --- |
| /res/anim/ |存放定義補間動畫的XML文件 |
| /res/animator/ |存放定義屬性動畫的XML文件 |
| /res/color / |存放定義不同狀態下顏色列表的XML文件 |
|/res/drawable/ | 存放各種位圖文件〈如*.png 、*.9.png、*.jpg、*.gif) 等. 除此之外,也可能是編譯成如下各種Drawable 對象的XML 文件: BitmapDrawable 對象、NinePatchDrawable 對象、StateListDrawable 對、SbapeDrawable 對象、 AnimationDraw油le 對象、Drawable 的其他各種子類的對象 。注:官網目前已將.png的圖片放在了mipmap的文件夾下。來作為啟動圖標 |
|/res/layout/ | 存放各種用戶界麗的布局文件 |
| /res/menu/ | 存放為應用程序定義各種菜單的資源, 包括選項菜單、子菜單、上下文菜單資源 |
| /res/raw/ | 存放任意類型的原生資源(比如音頻文件、視頻文件等).在Java 代碼中可通過調用Resource對象的openRawResource(int id)方法來獲取該資源的二迸制輸入流. 實際上,如果應用程序需要使用原生資源,也可把這些原生資源保存到/assets/ 目錄下,然后在應用程序中使用AssetManager 采訪問這些資源 |
| /res/xml/ | 存放任意的原生XML 文件, 這些XML 文件可以在Java 代碼中使用Resources.getXML()方法進行訪問 |
| /res/values/ | 存放各種簡單值的XML文件,這些簡單值包括字符串值,整數值、顏色值、數組等,這些資源文件的根元素都是<resources.. ./>,為該<resources../>元素添加不同的子元素則代表不同的資源,例如:string/integer/bool 子元素: 代表添加一個字符串值、整數值或boolean 值;color 子元素: 代表添加一個顏色值;array 子元素或string-array、int-array 子元素:代表添加一個數組; style 子元素: 代表添加一個樣式;dimen : 代表添加一個尺寸等等。由于各種簡單值都可定義在/res/values/ 目錄下的資源文件中,如果在同一份資源文件中定義各種值,勢必增加程序維護的難度. 為此, Android 建議使用不同的文件來存放不同類型的值,例如:arrays.xml : 定義數組資源;colors.xml : 定義顏色值資源;dimens.xml : 定義尺寸值資源;strings.xml : 定義字符串資源;styles.xml : 定義樣式資源 |
- 官網目前的介紹如下表
| Directory| Resource Type |
| --- | --- |
| animator/ | XML files that define[ property animations](https://developer.android.com/guide/topics/graphics/prop-animation.html). |
| anim/ | XML files that define [tween animations](https://developer.android.com/guide/topics/graphics/view-animation.html#tween-animation). (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.) |
| color/ | XML files that define a state list of colors. See [Color State List Resource ](https://developer.android.com/guide/topics/resources/color-list-resource.html) |
| drawable/ | Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:①Bitmap files②Nine-Patches (re-sizable bitmaps)③State lists④Shapes⑤Animation drawables⑥Other drawables。See [Drawable Resources](https://developer.android.com/guide/topics/resources/drawable-resource.html).|
| mipmap/ | Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders, see [Managing Projects Overview.](https://developer.android.com/studio/projects/index.html#mipmap) |
| layout/ | XML files that define a user interface layout. See [Layout Resource](https://developer.android.com/guide/topics/resources/layout-resource.html). |
| menu/ | XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See [Menu Resource](https://developer.android.com/guide/topics/resources/menu-resource.html). |
| raw/ | Arbitrary files to save in their raw form. To open these resources with a raw [InputStream](https://developer.android.com/reference/java/io/InputStream.html), call [Resources.openRawResource()](https://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)) with the resource ID, which is R.raw.filename.However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using[ AssetManager.](https://developer.android.com/reference/android/content/res/AssetManager.html)|
| values/ | XML files that contain simple values, such as strings, integers, and colors.Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:①arrays.xml for resource arrays ([typed arrays](https://developer.android.com/guide/topics/resources/more-resources.html#TypedArray)).②colors.xml for [color values](https://developer.android.com/guide/topics/resources/more-resources.html#Color)③dimens.xml for [dimension values](https://developer.android.com/guide/topics/resources/more-resources.html#Dimension).④strings.xml for [string values](https://developer.android.com/guide/topics/resources/string-resource.html).⑤styles.xml for [styles](https://developer.android.com/guide/topics/resources/style-resource.html).See [String Resources](https://developer.android.com/guide/topics/resources/string-resource.html), [Style Resource](https://developer.android.com/guide/topics/resources/style-resource.html), and [More Resource Types.](https://developer.android.com/guide/topics/resources/more-resources.html) |
| xml/ | Arbitrary XML files that can be read at runtime by calling [Resources.getXML()](https://developer.android.com/reference/android/content/res/Resources.html#getXml(int)). Various XML configuration files must be saved here, such as a [searchable configuration](https://developer.android.com/guide/topics/search/searchable-config.html) |
| font/ |Font files with extensions such as .ttf, .otf, or .ttc, or XML files that include a <font-family> element. For more information about fonts as resources, go to [Fonts in XML](https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html). |
- 應用的/res/ 目錄下不僅包含了drawable 子目錄,而且還提供了drawable-ldpi (低分辨率)、drawable-mdpi (中等分辨率)、drawable-hdpi (高分辨率)、drawable-xhdpi (超高分辨率) 4 個子目錄,其實這4個子目錄的作用就相當于drawable子目錄,這四個子目錄下存放的圖片文件的文件名完全相同,只是分辨率不同-----這種做法可以讓系統根據屏幕分辨率來選擇相應的圖片。大部分程序、系統將會選擇drawable-mdpi 目錄下的圖片文件,如果系統使用高分辨率屏幕,那么系統將會選擇drawable-hdpi 目錄下的圖片文件。目前官網已建議將.png的圖片放在mipmap的文件夾下。
#### 使用資源
1. 在Java 代碼中使用資源清單項
- Java 代碼中訪問資源主要通過R 類來完成。其完整的語法格式為:`[<package_name> . ]R . <res 。urce_type> . <resource_name>`
- <package_name>: 指定R 類所在包,實際上就是使用全限定類名。當然,如果在Java程序中導入R 類所在包,就可以省略包名。
- <resource_name>: 指定資源的名稱。該資源名稱可能是無后綴的文件名〈如圖片資源) , 也可能是XML 資源元素中由android: name 屬性所指定的名稱。
2. 在Java 代碼中訪問實際資源
- 為了通過資源清單項來獲取實際資源,可以借助于Android 提供的Resources 類。把Resources 類稱為“燦的id 資源訪問總管家”
- Resources 主要提供了如下兩類方法。
- getXxx(int id): 根據資源清單ID 來獲取實際資源。比如getColor(int id) 、getDimension(int id) 、getDrawable(int id) 、getXml(int id) 、 getText(int id) 等等
- getAssets(): 獲取訪問/assets/目錄下資源的AssetManager 對象。
- Resources (android.content.res)由Context 調用getResources()方法來獲取。
3. 在XML 文件中使用資源
- 在XML 代碼中使用資源的完整語法格式為:`@[<package name> : ]<resource type>/<resource name>`
- <package_name> : 指定資源類所在應用的包。如果所引用的資源和當前資源位于同一個包下,則<package_name>可以省略。
- <resource_name>: 指定資源的名稱。該資源名稱可能是無后綴的文件名(如圖片資源〉,也可能是XML 資源元素中由android :name 屬性所指定的名稱。
- 資源文件位于/res/values/ 目錄下,官網介紹,可以多種資源類型,除了上面表格介紹的資源類型之外,還可以有Bool、Color、Dimension、ID、Integer、Integer Array、Typed Array等
- 前言
- Android系統的體系結構
- Dalvik VM 和 JVM 的比較
- Android 打包應用程序并安裝的過程
- Android ADB工具
- Android應用開發
- Android UI相關知識總結
- Android 中window 、view、 Activity的關系
- Android應用界面
- Android中的drawable和bitmap
- AndroidUI組件adapterView及其子類和Adapter的關系
- Android四大組件
- Android 數據存儲
- SharedPreference
- Android應用的資源
- 數組資源
- 使用Drawable資源
- Material Design
- Android 進程和線程
- 進程
- 線程
- Android Application類的介紹
- 意圖(Intent)
- Intent 和 Intent 過濾器(Google官網介紹)
- Android中關于任務棧的總結
- 任務和返回棧(官網譯文)
- 總結
- Android應用安全現狀與解決方案
- Android 安全開發
- HTTPS
- 安卓 代碼混淆與打包
- 動態注入技術(hook技術)
- 一、什么是hook技術
- 二、常用的Hook 工具
- Xposed源碼剖析——概述
- Xposed源碼剖析——app_process作用詳解
- Xposed源碼剖析——Xposed初始化
- Xposed源碼剖析——hook具體實現
- 無需Root也能Hook?——Depoxsed框架演示
- 三、HookAndroid應用
- 四、Hook原生應用程序
- 五、Hook 檢測/修復
- Android 應用的逆向與加固保護技術
- OpenCV在Android中的開發
- Android高級開發進階
- 高級UI
- UI繪制流程及原理
- Android新布局ConstraintLayout約束布局
- 關鍵幀動畫
- 幀動畫共享元素變換
- Android異步消息處理機制完全解析,帶你從源碼的角度徹底理解
- Android中為什么主線程不會因為Looper.loop()里的死循環卡死?
- 為什么 Android 要采用 Binder 作為 IPC 機制?
- JVM 中一個線程的 Java 棧和寄存器中分別放的是什么?
- Android源碼的Binder權限是如何控制?
- 如何詳解 Activity 的生命周期?
- 為什么Android的Handler采用管道而不使用Binder?
- ThreadLocal,你真的懂了嗎?
- Android屏幕刷新機制