位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。
1. 从资源中获取位图
可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。
当然,首先需要获取资源:
Resources res=getResources();
使用BitmapDrawable获取位图
使用BitmapDrawable (InputStreamis)构造一个BitmapDrawable;
使用BitmapDrawable类的getBitmap()获取得到位图;
// 读取InputStream并得到位图
InputStreamis=res.openRawResource(R.drawable.pic180);
BitmapDrawable bmpDraw=newBitmapDrawable(is);
Bitmap bmp=bmpDraw.getBitmap();或者采用下面的方式:
BitmapDrawablebmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);
Bitmapbmp=bmpDraw.getBitmap();使用BitmapFactory获取位图
(Creates Bitmap objects fromvarious sources, including files, streams, and byte-arrays.)
使用BitmapFactory类decodeStr