广告
返回顶部
首页 > 资讯 > 移动开发 >android中Bitmap用法(显示,保存,缩放,旋转)实例分析
  • 624
分享到

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

bitmapAndroid 2022-06-06 09:06:50 624人浏览 八月长安
摘要

本文实例讲述了Android中Bitmap用法。分享给大家供大家参考。具体如下: 在Android SDK中可以支持的图片格式如下:png , jpg , gif和bmp。 1

本文实例讲述了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 = "/file/imgs/upload/202206/06/1gekfcrbvvp.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详细介绍Android Activity之间传递图片(Bitmap)的方法android bitmap compress(图片压缩)代码android保存Bitmap图片到指定文件夹示例Android截取视频帧并转化为Bitmap示例Android读取本地或网络图片并转换为Bitmapandroid中Bitmap的放大和缩小实例代码解析Android开发优化之:对Bitmap的内存优化详解Android中使用Bitmap类将矩形图片转为圆形的方法Android中Bitmap常见的一些操作:缩放、裁剪、旋转和偏移


--结束END--

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

本文链接: https://www.lsjlt.com/news/26578.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作