---
title: "获得Bitmap的三种方法"
author: "kouriba"
date: "2012-08-19T19:30:00Z"
tags: []
slug: "bitmap"
draft: false
lastmod: "2012-08-20T14:11:10Z"
id: 1638309
url: "https://kouriba.iteye.com/blog/1638309"
---

使用BitmapFactory解析图片

// --&gt;使用BitmapFactory解析图片

**publicvoid**myUseBitmapFactory(Canvas canvas){

//定义画笔

Paint paint =**new**Paint();

//获取资源流

Resources rec = getResources();

InputStream in = rec.openRawResource(R.drawable.*haha*);

//设置图片

Bitmap bitmap =BitmapFactory.*decodeStream*(in);

//绘制图片

canvas.drawBitmap(bitmap, 0,20, paint);

}

使用BitmapDrawable解析图片

// --&gt;使用BitmapDrawable解析图片

**publicvoid**myUseBitmapDrawable(Canvas canvas){

//定义画笔

Paint paint =**new**Paint();

//获得资源

Resources rec = getResources();

// BitmapDrawable

BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.*haha*);

//得到Bitmap

Bitmap bitmap = bitmapDrawable.getBitmap();

//在画板上绘制图片

canvas.drawBitmap(bitmap, 20,120,paint);

}

使用InputStream和BitmapDrawable绘制

// --&gt;使用InputStream和BitmapDrawable解析图片

**publicvoid**myUseInputStreamandBitmapDrawable(Canvas canvas){

//定义画笔

Paint paint =**new**Paint();

//获得资源

Resources rec = getResources();

// InputStream得到资源流

InputStream in = rec.openRawResource(R.drawable.*haha*);

// BitmapDrawable解析数据流

BitmapDrawable bitmapDrawable =**new**BitmapDrawable(in);

//得到图片

Bitmap bitmap = bitmapDrawable.getBitmap();

//绘制图片

canvas.drawBitmap(bitmap, 100, 100,paint);

}

![](http://qp.qq.com/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Flovewf.blog.51cto.com%2Fattachment%2F201111%2F101428980.png&size=0)

这就是三种方法的效果。。。。