android 中获取Bitmap的两种方法

//原文地址 http://blog.csdn.net/biexf/article/details/6059083

 第一种方法 

Java代码 
  1. //得到Resources对象  
  2. Resources r = this.getContext().getResources();  
  3. //以数据流的方式读取资源  
  4. Inputstream is = r.openRawResource(R.drawable.my_background_image);  
  5. BitmapDrawable  bmpDraw = new BitmapDrawable(is);  
  6. Bitmap bmp = bmpDraw.getBitmap();  


  第二种方法这种方法是通过BitmapFactory这个工具类,BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。大家可以打开API 看一下里边全是静态方法。这个类里边有一个叫做 decodeStream(InputStream is)   
此方法可以 解码一个新的位图从一个InputStream。这是获得资源的InputStream。 
代码: 
Java代码 
  1. InputStream is = getResources().openRawResource(R.drawable.icon);    
  2.          Bitmap mBitmap = BitmapFactory.decodeStream(is);    
  3.          Paint mPaint = new Paint();    
  4.          canvas.drawBitmap(mBitmap, 4040, mPaint);    

/********************************************************************分割线**************************************************************************************/

//一下为个人总结

第一种方法

获取BitmapDrawable对象,通过getBitmap()方法获得Bitmap

getResources().getDrawable()

第二种方法

使用BitmapFactory.decodexxx()来获取Bitmap,不过不建议使用此方法。

BitmapFactory.decodeResource(Resources res ,R.drawable.icon)


猜你喜欢

转载自blog.csdn.net/thinkme323/article/details/51480431
今日推荐