使用BitmapFactory解析图片
// -->使用BitmapFactory解析图片
publicvoidmyUseBitmapFactory(Canvas canvas){
//定义画笔
Paint paint =newPaint();
//获取资源流
Resources rec = getResources();
InputStream in = rec.openRawResource(R.drawable.haha);
//设置图片
Bitmap bitmap =BitmapFactory.decodeStream(in);
//绘制图片
canvas.drawBitmap(bitmap, 0,20, paint);
使用BitmapDrawable解析图片
// -->使用BitmapDrawable解析图片
publicvoidmyUseBitmapDrawable(Canvas canvas){
//定义画笔
Paint paint =newPaint();
//获得资源
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绘制
// -->使用InputStream和BitmapDrawable解析图片
publicvoidmyUseInputStreamandBitmapDrawable(Canvas canvas){
//定义画笔
Paint paint =newPaint();
//获得资源
Resources rec = getResources();
// InputStream得到资源流
InputStream in = rec.openRawResource(R.drawable.haha);
// BitmapDrawable解析数据流
BitmapDrawable bitmapDrawable =newBitmapDrawable(in);
//得到图片
Bitmap bitmap = bitmapDrawable.getBitmap();
//绘制图片
canvas.drawBitmap(bitmap, 100, 100,paint);

图片解析方法
本文介绍了三种不同的图片解析方法:使用BitmapFactory、BitmapDrawable以及InputStream结合BitmapDrawable进行图片解析和绘制的过程。这些方法适用于Android开发中对资源图片的处理。
1346

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



