学习android有段时间了,一直都是看别人的博客来解决问题,今天决定记录下自己的思考的一些问题,大家可以分享交流。
关于android的Resources类,是在学习Bitmap类时遇到的。因为Bitmap类的构造函数是private 私有的,所以不能通过 new Bitmap()来实现。
要创建一个Bitmap的对象,有3种办法。
1.利用Bitmap的静态方法createBitmap(). 具体参考下面链接,写的挺好。
http://www.cnblogs.com/igrl/archive/2010/07/30/Bitmap_createBitmap.html
下面的两种方法涉及到今天探讨的问题。
2.利用BitmapDrawable类(Bitmap的一个封装类)
Resources res=context.getResources(); //得到系统的资源对象。
InputStream
is=res.openRawResource(R.drawable.pic180); // 读取资源文件获取输入流
BitmapDrawable bmpDraw=new BitmapDrawable(is); //利用is构造一个BitmapDrawable
Bitmap bmp=bmpDraw.getBitmap(); //通过BitmapDrawable得到Bitmap的对象
或者:
BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);
Bitmap bmp=bmpDraw.getBitmap();
3.利用BitmapFactory类
Resources res=context.getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); // 转换为BitmapDrawable对象
我们可以看到2,3方法中都是从Resources res=context.getResources()开始的, 我们看看Android 文档。
/**
* Class for accessing an application's resources. This sits on top of the
* asset manager of the application (accessible through {@link #getAssets}) and
* provides a high-level API for getting typed data from the assets.
*
此类是为了访问应用的资源。它位于应用的资源(asset)管理者之上,提供一个高级别的API以至于从资源中得到某一类型的数据。
* <p>The Android resource system keeps track of all non-code assets associated with an
* application. You can use this class to access your application's resources. You can generally
* acquire the {@link android.content.res.Resources} instance associated with your application
* with {@link android.content.Context#getResources getResources()}.</p>
*
android的资源系统时刻与应用中所有非代码资源(assets 和res文件夹里的)保持联系。你可以用这个类Resources访问应用的资源。通常,你可以通过getResources()来得到Resources的对象。
* <p>The Android SDK tools compile your application's resources into the application binary
* at build time. To use a resource, you must install it correctly in the source tree (inside
* your project's {@code res/} directory) and build your application. As part of the build
* process, the SDK tools generate symbols for each resource, which you can use in your application
* code to access the resources.</p>
*
SDK工具在运行时将资源编译到应用中(索引 就是R文件里的id)。为了使用一个资源,你必须将资源(图片,xml等)正确的放在资源树(资源文件夹)里,然后创建应用。
作为创建流程的一部分,SDK工具通常会标记每个资源,以使得可以在代码中访问资源。(比如 button = (Button) findViewById(R.id.button))
* <p>Using application resources makes it easy to update various characteristics of your
* application without modifying code, and—by providing sets of alternative
* resources—enables you to optimize your application for a variety of device configurations
* (such as for different languages and screen sizes). This is an important aspect of developing
* Android applications that are compatible on different types of devices.</p>
*
使用应用的资源可以让你在不修改代码的情况下,通过可选的资源,更新,优化应用(例如 适应不同的语言和屏幕尺寸)。兼容不同类型的设备是android应用开发过程中重要的一方面。
* <p>For more information about using resources, see the documentation about <a
* href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.</p>
*/
另外,Resources里几乎所有的方法get...()都需要一个参数就是 int id,它就来自android里的自动生成的R文件。这就在资源和代码之间形成一个纽带的关系。
通过上面创建Bitmap的实例的例子,可以体会到虽然资源存在,但在Java代码只是存在id(索引),不存在实例,不能对资源进行操作,进行处理。
所以为了从资源新建一个实例,这个Resources登场了,它将资源和java代码成功地联系起来。