游锦绣中华作文:android 从assets和res中读取文件(转)
来源:百度文库 编辑:偶看新闻 时间:2024/04/28 21:15:10
android 从assets和res中读取文件(转)
1. 相关文件夹介绍目录Directory
资源类型Resource Types
res/anim/
XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象
res/drawable/
.png、.9.png、.jpg文件,它们被编译进以下的Drawable资源子类型中:
要获得这种类型的一个资源,可以使用Resource.getDrawable(id)
位图文件
9-patches(可变尺寸的位图)
为了获取资源类型,使用mContext.getResources().getDrawable(R.drawable.imageId)
注意:放在这里的图像资源可能会被aapt工具自动地进行无损压缩优化。比如,一个真彩色但并不需要256色的PNG可能会被转换为一个带调色板的8位PNG。这使得同等质量的图片占用更少的资源。所以我们得意识到这些放在该目录下的二进制图像在生成时可能会发生变化。如果你想读取一个图像位流并转换成一个位图(bitmap),请把图像文件放在 res/raw/目录下,这样可以避免被自动优化。
res/layout/
被编译为屏幕布局(或屏幕的一部分)的XML文件。参见布局声明(Declaring Layout)
res/values/
可以被编译成很多种类型的资源的XML文件。
注意: 不像其他的res/文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。XML元素类型控制这些资源应该放在R类的什么地方。
尽管这个文件夹里的文件可以任意命名,不过下面使一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中):
res/xml/
任意的XML文件,在运行时可以通过调用Resources.getXML()读取。
res/raw/
直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。
2.自动生成的R class
3. 在代码中使用资源
下面是一个引用资源的语法:R.resource_type.resource_name - //
Load a background for the current screen from a drawable resource. - this.getWindow().setBackgroundDrawableRes
ource(R.drawable.my_background_image); -
- //
WRONG Sending a string resource reference into a - //
method that expects a string. - this.getWindow().setTitle(R.string.main_title);
-
- //
RIGHT Need to get the title from the Resources wrapper. - this.getWindow().setTitle(Resources.getText(R.string.main_title));
-
- //
Load a custom layout for the current screen. - setContentView(R.layout.main_screen);
-
- //
Set a slide in animation for a ViewFlipper object. - mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
-
R.anim.hyperspace_in)); -
- //
Set the text on a TextView object. - TextView
msgTextView = (TextView)findViewByID(R.id.msg); - msgTextView.setText(R.string.hello_message);
- //在屏幕上显示标准应用程序的图标
- public
class MyActivity extends Activity { -
public void onStart() { -
requestScreenFeatures(FEATURE_BADGE_IMAGE); -
super.onStart(); -
setBadgeResource(android.R.drawable.sym_def_app_icon); -
} - }
-
- //应用系统定义的标准"绿色背景"视觉处理
- public
class MyActivity extends Activity -
public void onStart() { -
super.onStart(); -
setTheme(android.R.style.Theme_Black); -
} - }
4. xml文件内引用资源
1) 引用自定义的资源- version="1.0"
encoding="utf-8"?> -
name="hello">Hello World, HelloDemo! -
5. 替换资源(为了可替换的资源和配置)
6. Color Value
语法:name="color_name">#color_value
- version="1.0"
encoding="utf-8"?> -
name="opaque_red">#f00 -
name="translucent_red">#80ff0000 -
7.Color Drawables
语法:name="color_name">color_value
- version="1.0"
encoding="utf-8"?> -
name="opaque_red">#f00 -
name="translucent_red">#80ff0000 -
8. 图片
9. dimension
语法:name="dimen_name">dimen_value单位
Java: float dimen = Resources.getDimen(R.dimen.some_name)
- version="1.0"
encoding="utf-8"?> -
name="one_pixel">1px -
name="double_density">2dp -
name="sixteen_sp">16sp -
10. string
下面是官方给出的正确/错误的例子:- //不使用转义符则需要用双引号包住整个string
name="good_example">"This'll work" -
- //使用转义符
name="good_example_2">This\'ll also work -
- //错误
name="bad_example">This won't work! -
- //错误
不可使用html转义字符 name="bad_example_2">XML encodings won't work either!
- version="1.0"
encoding="utf-8"?> -
name="simple_welcome_message">Welcome! -
name="styled_welcome_message">We are so glad to see you. -
-
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:textAlign="center" -
android:text="@string/simple_welcome_message"/>
- //
Assign a styled string resource to a TextView on the current screen. - CharSequence
str = getString(R.string.styled_welcome_message); - TextView
tv = (TextView)findViewByID(R.id.text); - tv.setText(str);
- version="1.0"
encoding="utf-8"?> -
name="search_results_resultsTextFormat">%1$d results for <b>"%2$s"</b> -
- //title是我们想赋值给%2$s的字符串
- String
escapedTitle = TextUtil.htmlEncode(title);
- String
resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat); - String
resultsText = String.format(resultsTextFormat, count, escapedTitle); - CharSequence
styledResults = Html.fromHtml(resultsText);
11. assets文件夹资源的访问
以下为从Raw文件中读取:
代码