方法(1):先获取Resource,然后可以通过资源ID获取Drawable,也可以通过资源ID获取资源文件的数据流Drawable是个抽象类,在BitmapDrawable中我们就看到位图的具体操作,在仔细看下BitmapDrawable的构造函数,我们就会发现与Resource中的openRawResource()接口是相对应的,就可以通过以下方法来获取位图:

Resources r = this.getContext().getResources();
Inputstream is = r.openRawResource(R.drawable.my_background_image);
BitmapDrawable  bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();

方法(2):通过Resource的函数:InputStream  openRawResource(int id)获取得到资源文件的数据流后,也可以通过2种方法来获取Bitmap,如下:
使用BitmapDrawable
(A Drawable   that wraps a bitmap and can be tiled, stretched, or aligned.)
使用BitmapDrawable   (InputStream is)构造一个BitmapDrawable;使用BitmapDrawable类的getBitmap()获取得到位图;
BitmapDrawable也提供了显示位图等操作
使用BitmapFactory
(Creates Bitmap objects from various   sources, including files, streams, and byte-arrays.)
使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图
BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。
以上方法在编程的时候可以自由选择,在Android SDK中说明可以支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged),虽然bmp格式没有明确说明,但是在Android SDK Support Media Format中是明确说明了。


posted on 2011-05-12 11:35  denniswang  阅读(2202)  评论(0编辑  收藏  举报