Android获得图片资源的三种方式

、        使用BitmapFactory解析图片

 

01 // --> 使用BitmapFactory解析图片
02 public void myUseBitmapFactory(Canvas canvas){
03 // 定义画笔
04    Paint paint = new Paint();
05 // 获取资源流
06    Resources rec = getResources();
07    InputStream in = rec.openRawResource(R.drawable.haha);
08 // 设置图片
09    Bitmap bitmap =BitmapFactory.decodeStream(in);
10 // 绘制图片
11    canvas.drawBitmap(bitmap, 0,20, paint);        
12 }

 

 

、        使用BitmapDrawable解析图片

 

01 // --> 使用BitmapDrawable解析图片
02     public void myUseBitmapDrawable(Canvas canvas){
03     // 定义画笔
04        Paint paint = new Paint();
05     // 获得资源
06        Resources rec = getResources();
07     // BitmapDrawable
08        BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha);
09     // 得到Bitmap
10        Bitmap bitmap = bitmapDrawable.getBitmap();
11     // 在画板上绘制图片
12        canvas.drawBitmap(bitmap, 20,120,paint);
13     }

 

 

三、        使用InputStream和BitmapDrawable绘制

 

01 // --> 使用InputStream和BitmapDrawable解析图片
02     public void myUseInputStreamandBitmapDrawable(Canvas canvas){
03     // 定义画笔
04        Paint paint = new Paint();
05     // 获得资源
06        Resources rec = getResources();
07     // InputStream得到资源流
08        InputStream in = rec.openRawResource(R.drawable.haha);
09     // BitmapDrawable 解析数据流
10        BitmapDrawable bitmapDrawable =  new BitmapDrawable(in);
11     // 得到图片
12        Bitmap bitmap = bitmapDrawable.getBitmap();
13     // 绘制图片
14        canvas.drawBitmap(bitmap, 100100,paint);
15     }
posted @ 2012-08-15 09:46  肆意感受  阅读(223)  评论(0编辑  收藏  举报