分享好友 移动开发首页 频道列表

android中Bitmap用法(显示,保存,缩放,旋转)实例分析

Android开发  2015-11-08 00:310

本文实例讲述了android中Bitmap用法。分享给大家供大家参考。具体如下:

在Android SDK中可以支持的图片格式如下:png , jpg , gif和bmp。

1.Bitmap的创建

借助于BitmapFactory。

1)资源中的图片

使用BitmapFactory获取位图

复制代码 代码如下:
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.testImg);

或者是:

Resources res=getResources();
//使用BitmapDrawable获取位图
//使用BitmapDrawable (InputStream is)构造一个BitmapDrawable;
//使用BitmapDrawable类的getBitmap()获取得到位图;
// 读取InputStream并得到位图
InputStream is=res.openRawResource(R.drawable.testImg); 
BitmapDrawable bmpDraw=new BitmapDrawable(is);
Bitmap bmp=bmpDraw.getBitmap();

2)SD卡中的图片

复制代码 代码如下:
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/testBitmap/testImg.png")

2. 把 Bitmap 保存在sdcard中

File fImage = new File("/sdcard/testBitmap/testImg.png");  
fImage.createNewFile();
FileOutputStream iStream = new FileOutputStream(fImage); 
bmp.compress(CompressFormat.PNG, 100, iStream); 
iStream.close();
fImage.close();
iStream =null;
fImage =null;
//写到输出流里,就保存到文件了。

3.使用网络中的图片

//图片的链接地址  
String imgURLStr = "http://tx.bdimg.com/sys/portrait/item/990e6271796a7a6c170c.jpg";  
URL imgURL = new URL(imgURLStr);  
URLConnection conn = imgURL.openConnection();  
conn.connect();  
InputStream is = conn.getInputStream();  
BufferedInputStream bis = new BufferedInputStream(is);
//下载图片
Bitmap bmp = BitmapFactory.decodeStream(bis);
//关闭Stream
bis.close();  
is.close(); 
imgURL =null;

4.显示图片

1)转换为BitmapDrawable对象显示位图

// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);

2)使用Canvas类显示位图

复制代码 代码如下:
canvas.drawBitmap(bmp, 0, 0, null);

5.缩放位图

1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:

复制代码 代码如下:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)

2)在原有位图的基础上,缩放原位图,创建一个新的位图:

复制代码 代码如下:
CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

3)借助Canvas的scale(float sx, float sy) ,不过要注意此时整个画布都缩放了。

4)借助Matrix:

Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
canvas.drawBitmap(dstbmp, 10, 10, null); 

6.旋转位图

借助Matrix或者Canvas来实现。

Matrix matrix=new Matrix();
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true);
canvas.drawBitmap(dstbmp, 10, 10, null);

希望本文所述对大家的Android程序设计有所帮助。

查看更多关于【Android开发】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
SDK热更之如何在SDK代码中自动插桩及如何生成补丁包
写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更

0评论2017-02-05358

Supporting Multiple Screens
术语和概念Screen size 屏幕尺寸又称「屏幕大小」,是屏幕对角线的物理尺寸。单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。Resolution 屏幕分辨率屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440

0评论2017-02-05363

AssetBundle里的Shader问题
关于Resources和AssetBundle优劣之前已经提过很多次了(参考官方教程The Resources folder),正好最近@张迪在做框架AssetBundle部分的优化,特此整理一下两个特常见的坑及对应解决办法。之前在关于Unity中的资源管理,你可能遇到这些问题里有有人提到过这个问

0评论2017-02-05610

ASimpleCache
ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。1、它可以缓存什么东西?普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。2、它有什么特色?特色主要是

0评论2017-02-05376

更多推荐