1. 使用BitmapFactory解析图片
public void 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);
}2. 使用BitmapDrawable解析图片
public void myUseBitmapDrawable(Canvas canvas){
//定义画笔
Paint paint = new Paint();
//获得资源
Resources rec = getResources();
BitmapDrawable bitDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha);
//得到Bitmap
Bitmap bitmap = bitmapDrawable.getBitmap();
//在画板上绘制图片
canvas.drawBitmap(bitmap,20,120,paint);
}3. 使用InputStream和BitmapDrawable绘制
public void myUseInputStreamBitmapDrawable(Canvas canvas){
//定义画笔
Paint paint = new Paint();
//获得资源
Resources rec = getResources();
InputStream in = rec.openRawResource(R.drawable.haha);
//解析数据流
BitmapDrawable bitDrawable = new BitmapDrawable(in);
//得到Bitmap
Bitmap bitmap = bitmapDrawable.getBitmap();
//在画板上绘制图片
canvas.drawBitmap(bitmap,100,100,paint);
}
本文详细介绍了在Android自定义View中如何通过BitmapFactory解析图片,从而获取Bitmap对象的方法。
104

被折叠的 条评论
为什么被折叠?



