什么是资源

Android应用程序由两部分组成,功能部分(代码指令)和数据部分(资源),功能部分是决定你应用程序行为的代码,包括程序运行的任何算法,资源包括了文本字符串、样式和主题、尺寸、图片和图标、音频文件、视频以及应用使用的其他数据。

存储应用程序资源

Android的资源文件不同于.java类。是分开存在Android项目内,所有的资源必须存放在项目的/res目录下的特定子目录下,其目录名必须是小写。
管理Android应用程序的资源_第1张图片
不同的资源存放在不同的目录中,当创建一个Android项目时,生成的资源子目录如上表所示

不同的资源类型对应一个特定的资源子目录名,例如所有的图形资源都是存放在/res/drawable目录结构下。资源可以使用更特殊的目录限定方式组织,例如/res/drawable-hdpi 目录存储高像素密度屏幕的图形,/res/drawable-ldpi目录存储低像素密度的图形等。如果你的图形资源被所有屏幕尺寸共享,你可以简单地将资源存储在/res/drawable目录中。

应用资源类型

Android应用程序使用多种不同类型的资源,如文本字符串、图形和颜色方案、以供用户页面设计,这些资源都存储在Android项目的/res/目录下,遵守严格的目录和文件名规定。所有的资源文件名必须是小写。

  • Android SDK支持的资源类型,参考下表所示

管理Android应用程序的资源_第2张图片
管理Android应用程序的资源_第3张图片

  • 可绘制的图形资源
    管理Android应用程序的资源_第4张图片

  • 颜色状态列表元素属性
    管理Android应用程序的资源_第5张图片

  • Android支持的图片格式
    管理Android应用程序的资源_第6张图片

  • 引用系统资源
    管理Android应用程序的资源_第7张图片

定义和使用资源
  • 字符串资源 strings.xml
<string name = "name">"zhangsan"</string>String name = getResources().getString(R.string.name);
  • 数字资源 integers.xml
<integer name = "age">24</integer>int age = getResources().getInteger(R.integer.age);
  • 布尔资源 bools.xml
<bool name = "debug">true</bool>boolean debug = getResources().getBoolean(R.bool.debug);
  • 颜色资源 colors.xml
<color name = "red">#ff0</bool>int color = getResources().getColor(R.color.red);
  • 尺寸资源 dimens.xml
<dimen name = "width">200dp</dimen>float width = getResources().getDimension(R.dimen.width);
  • id资源 ids.xml
<item name="tv_text" type="id" />布局使用 @id/tv_text ,代码使用 tvText.setId(R.id.tv_text);
  • drawable资源 drawables.xml
<drawable name="icon">@drawable/ic_launcher</drawable>Drawable icon = getResources().getDrawable(R.drawable.icon);
  • /res/color 资源
<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/colorPrimary" android:state_enabled="true" />    <item android:color="@color/colorAccent" />  </selector>
  • /res/menu 资源
<menu xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/setting"        android:title="Setting">    </item>    <item        android:id="@+id/help"        android:title="Help">    </item></menu>
// Activity复写此方法@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.menu,menu);    return true;}
  • /res/raw 资源
InputStream is = getResources().openRawResource(R.raw.test);
  • /res/anim 资源
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false">    <rotate        android:duration="1000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="180">    </rotate></set>
Animation animation = AnimationUtils.loadAnimation(this,R.anim.spin);imageView.startAnimation(animation);
  • /res/animator 资源
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"    android:valueFrom="1"    android:valueTo="0"    android:valueType="floatType"    android:propertyName="alpha"/>
Animator animator = AnimatorInflater.loadAnimator(context,R.animator.view_animation);  animator.setTarget(view);  animator.start();
  • /res/drawable 帧动画资源
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <item android:drawable="@drawable/drawable1"        android:duration="500">    </item>    <item android:drawable="@drawable/drawable2"        android:duration="500">    </item>    <item android:drawable="@drawable/drawable3"        android:duration="500">    </item></animation-list>
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.test);imageView.setImageDrawable(animationDrawable);animationDrawable.start();
  • /res/xml 资源
// XmlResourceParser parser = getResources().getXml(R.xml.test);public void parseXml() throws IOException, XmlPullParserException {        XmlResourceParser parser = getResources().getXml(R.xml.my_pets);        int eventType = -1;        while (eventType != XmlResourceParser.END_DOCUMENT) {            if (eventType == XmlResourceParser.START_DOCUMENT) {                Log.d("TAG", "Document start");            } else if (eventType == XmlResourceParser.START_TAG) {                String strName = parser.getName();                if (strName.equals("pet")) {                    Log.d("TAG", "Found a pet");                    Log.d("TAG", "name:" + parser.getAttributeValue(null, "name"));                    Log.d("TAG", "Type:" + parser.getAttributeValue(null, "type"));                }            }            eventType = parser.next();        }        Log.d("TAG", "Document end");}
  • assets 资源
InputStream is = getResources().getAssets().open("test.html");
  • 数组资源 arrays.xml
<!--定义字符串数组--><string-array name="string_array">    <item>"zhangsan"</item>    <item>"lisi"</item>    <item>"wangwu"</item></string-array><!--定义整形数组--><integer-array name="int_array">    <item>1</item>    <item>2</item>    <item>3</item></integer-array>
String[] stringArray = getResources().getStringArray(R.array.string_array);int[] intArray = getResources().getIntArray(R.array.int_array);
  • styles资源 styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item></style>引用方式 R.style.AppTheme
  • TypedArray资源
<array name="icons">    <item>@drawable/ic_launcher_foreground</item>    <item>@drawable/ic_launcher_background</item></array><array name="colors">    <item>@color/colorAccent</item>    <item>@color/colorPrimary</item></array> // 获取方式TypedArray taIcons = getResources().obtainTypedArray(R.array.icons);Drawable drawable1 = taIcons.getDrawable(0);Drawable drawable2 = taIcons.getDrawable(1);taIcons.recycle();TypedArray taColors = getResources().obtainTypedArray(R.array.colors);int color1 = taColors.getColor(0, -1);int color2 = taColors.getColor(1, -1);taColors.recycle();
raw和asset区别

两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。

  • /res/raw 文件会被映射到 R.java 文件中,访问的时候直接通过资源 ID 即可访问,但它不能有目录结构,就是不能再创建文件夹。

  • assets 文件不会映射到 R.java 文件中,通过 AssetManager 来访问,能有目录结构,即可以自行创建子文件夹。

  • 注意1:Google的Android系统处理Assert有个bug,在AssertManager中不能处理单个超过1MB的文件,不然会报异常,raw没这个限制可以放个4MB的Mp3文件没问题。

  • 注意2:assets 文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像 xml, java 文件被预编译,可以存放一些html,js, css 等文件。

更多相关文章

  1. android不同应用程序之间启动Activity
  2. android http通过post上传文件和提交参数(通过拼装协议)
  3. EACCES (permission denied)解决办法 android 文件读写
  4. Android-Fresco系列2 加载资源
  5. 【Android】用于打开各种文件的intent
  6. android 文件的读取与写入以及TextView的滚动

随机推荐

  1. 手把手带你搭建 Elasticsearch 集群
  2. Android采用PULL解析XML文档
  3. 关于android AppWidget初探①
  4. Android 菜单(SubMenu)
  5. 如何去除Android布局文件xml中的斜体样式
  6. Android ListView的背景和黑色边缘化的问
  7. [ant]通过Android命令自动编译出build.xm
  8. 使用风格化Android的GridView元素背景
  9. 在Android上有类似于session的东西,叫做Ap
  10. Android 4.4 KitKat 新特性