bitmap在开发中是重要的资源,有几种加载bitmap的方式:
1. 使用BitmapFactory加载
Resources res = context.getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.bgmainfinal);
2. 使用stream
InputStream is = context.getResources().openRawResource(R.drawable.bgmainfinal);
BitmapDrawable bmpDraw=new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();
3. 使用BitmapDrawable
Resources res = context.getResources();
BitmapDrawable bmpDraw=(BitmapDrawable) res.getDrawable(R.drawable.bgmainfinal);
Bitmap bmp = bmpDraw.getBitmap();
上面3种方式可以自由选择,不过我在使用 1,3 的时候发现加载的bitmap长宽会变化,使用第二种方式加载图片不会改变长宽。