在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);
<!--EndFragment-->
分享到:
相关推荐
感觉Uri 、File、bitmap 比较混乱,这里进行记载,方便以后查看.下面话不多说了,来一起看看详细的介绍吧 Bitmap、File与Uri 1、将一个文件路径path转换成File String path ; File file = new File(path) 2...
本文实例讲述了Android中Bitmap用法。分享给大家供大家参考,具体如下: 一般在android程序中把图片文件放在res/drawable目录下就可以通过R.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能...
Android中Bitmap缓存池(基于2Q算法),供大一起共同分享学习。
Android中bitmap引起内存溢出的解决方案的对比分析.pdf
android中对Bitmap图片设置任意角为圆角
Android中bitmap转化成string格式的工具类,主要用于联网操作传递数据情境中
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory
Android图片Bitmap和字符串String之间相互转换的工具类,外加项目代码,封装好的,直接可以用,
主要介绍了android中Bitmap用法,以实例形式较为详细的分析了android中Bitmap操作图片的显示、保存、缩放、旋转等相关技巧,需要的朋友可以参考下
Bitmap是Android中处理图片的一个重要的类,下面这篇文章主要给大家介绍了关于Android中Bitmap常见的一些操作:缩放、裁剪、旋转和偏移的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...
主要介绍了 Android canvas drawBitmap方法详解及实例的相关资料,需要的朋友可以参考下
一个基于android平台Bitmap 工具类,一个基于android平台Bitmap 工具类,一个基于android平台Bitmap 工具类,一个基于android平台Bitmap 工具类,一个基于android平台Bitmap 工具类,一个基于android平台Bitmap 工具...
Bitmap放大或缩小:长和宽放大缩小的比例具体实现如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
Android bitmap Android Bitmap相关
主要介绍了Android中把bitmap存成BMP格式图片的方法,需要的朋友可以参考下