android中Bitmap用法(显示,保存,缩放,旋转)实例分析
作者:Ruthless 时间:2022-07-03 14:18:41
本文实例讲述了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,Bitmap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot如何重定向外部网页
2022-11-12 05:19:19
如何基于SpringBoot部署外部Tomcat过程解析
2021-10-26 07:14:16
Android开发实现判断通知栏是否打开及前往设置页面的方法
2022-10-21 10:43:45
IDEA 设置显示内存的使用情况和内存回收的方法
2023-10-08 15:51:25
![](https://img.aspxhome.com/file/2023/7/81367_0s.jpg)
springboot跨域如何设置SameSite的实现
2022-08-03 21:27:07
![](https://img.aspxhome.com/file/2023/9/89609_0s.jpg)
C#实现图表中鼠标移动并显示数据
2022-10-30 20:45:30
![](https://img.aspxhome.com/file/2023/6/108746_0s.jpg)
WPF中使用CallerMemberName简化InotifyPropertyChanged的实现
2023-05-08 16:27:07
Android自定义控件之继承ViewGroup创建新容器
2023-06-15 08:50:11
![](https://img.aspxhome.com/file/2023/8/98688_0s.jpg)
Java线程安全问题小结_动力节点Java学院整理
2021-09-25 21:01:43
C++实现String类的方法详解
2023-04-27 08:49:27
Winform开发中使用下拉列表展示字典数据的几种方式
2022-02-23 11:56:41
![](https://img.aspxhome.com/file/2023/4/116564_0s.png)
详解java模板和回调机制
2023-08-13 15:33:46
关于BufferedReader读取文件指定字符集问题
2023-09-03 11:10:28
浅谈Action+Service +Dao 功能
2023-01-04 13:44:20
![](https://img.aspxhome.com/file/2023/5/113405_0s.png)
深入理解C#管道式编程
2023-03-18 20:54:25
![](https://img.aspxhome.com/file/2023/3/96383_0s.png)
Android 游戏开发之Canvas画布的介绍及方法
2021-12-14 23:10:54
使用SSM+Layui+Bootstrap实现汽车维保系统的示例代码
2023-11-28 18:30:04
![](https://img.aspxhome.com/file/2023/6/60416_0s.jpg)
Java关于MyBatis缓存详解
2021-11-01 00:40:20
IDEA配置JRebel实现热部署的方法
2022-08-28 20:53:00
![](https://img.aspxhome.com/file/2023/1/100051_0s.png)
如何使用JJWT及JWT讲解和工具类
2023-10-28 04:21:35