展会信息港展会大全

android获得图片资源的三种方式
来源:互联网   发布日期:2016-01-14 11:06:38   浏览:1741次  

导读:一、 使用BitmapFactory解析图片// -- 使用BitmapFactory解析图片 public void myUseBitmapFactory(Canvas canvas){ // 定义画笔 Paint paint = new Paint(); // 获取资源流 Resources rec ......

一、使用BitmapFactory解析图片

// --> 使用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);

}

二、使用BitmapDrawable解析图片

// --> 使用BitmapDrawable解析图片

public void 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绘制

// --> 使用InputStream和BitmapDrawable解析图片

public void 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);

}

赞助本站

人工智能实验室

相关热词: android获得图片资源

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港