一、问题描述

  在开发中,当我们需要的有一张大图片同时还需要一些小图片时,我们只需要通过代码对此图片进行不同比例的缩放即可,这样大大节约资源,减小了安装包的尺寸 。除缩放外,我们还经常对图片进行其他操作如裁剪、旋转、存储等。

  这样我们可以编写对于图片进行处理的通用组件,方便开发。下面就分享一下对图片进行处理的组件BitmapUtil,案例界面:

二、技术点描述

  1、通过BitmapFactory取得Bitmap

Bitmap bm=BitmapFactory.decodeStream(InputStream is );

  2、Bimap的createBitmap()方法

Bitmap newbm = Bitmap.createBitmap( Bitmap s, int x, int y, int w, int h, Matrix m, boolean f);

  该方法可实现位图的缩放、裁剪、旋转操作

  参数说明:

Bitmap s:要处理的原始位图

int x ,y:起始位置坐标

int w:要截的图的宽度

int h:要截的图的宽度

Matrix m 矩阵,主要是用于平面的缩放、平移、旋转

boolean f:是否保证等比

返回值:返回处理后的Bitmap

三、BitmapUtil组件

  可实现对图片进行按比例缩放、图片按比例裁剪、圆形图片处理等方法,实现功能如下:

1、readBitmapById()方法

/*** 通过资源id转化成Bitmap* @param context* @param resId* @return*/public static Bitmap readBitmapById(Context context, int resId){BitmapFactory.Options opt = new BitmapFactory.Options();opt.inPreferredConfig = Bitmap.Config.RGB_565;opt.inPurgeable = true;opt.inInputShareable = true;InputStream is = context.getResources().openRawResource(resId);return BitmapFactory.decodeStream(is, null, opt);}

2、scaleImage()方法,实现按指定宽高缩放图片

  执行效果如图:

/**
* 缩放图片* @param bm 要缩放图片* @param newWidth 宽度* @param newHeight 高度* @return处理后的图片*/public static  Bitmap  scaleImage(Bitmap bm, int newWidth, int newHeight){if (bm == null){return null;}int width = bm.getWidth();int height = bm.getHeight();float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,true);if (bm != null & !bm.isRecycled()){bm.recycle();//销毁原图片bm = null;}return newbm;}

3、imageCrop()方法

执行效果如图:

/*** 按照一定的宽高比例裁剪图片* @param bitmap 要裁剪的图片* @param num1 长边的比例* @param num2 短边的比例* @param isRecycled是否回收原图片* @return 裁剪后的图片*/public static Bitmap  imageCrop(Bitmap bitmap, int num1, int num2, boolean isRecycled){if (bitmap == null){return null;}int w = bitmap.getWidth(); // 得到图片的宽,高int h = bitmap.getHeight();int retX, retY;int nw, nh;if (w > h){if (h > w * num2 / num1){nw = w;nh = w * num2 / num1;retX = 0;retY = (h - nh) / 2;} else{nw = h * num1 / num2;nh = h;retX = (w - nw) / 2;retY = 0;}} else{if (w > h * num2 / num1){nh = h;nw = h * num2 / num1;retY = 0;retX = (w - nw) / 2;} else{nh = w * num1 / num2;nw = w;retY = (h - nh) / 2;retX = 0;}}Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,false);if (isRecycled && bitmap != null && !bitmap.equals(bmp)&& !bitmap.isRecycled()){bitmap.recycle();//回收原图片bitmap = null;}return bmp;}

4、toRoundCorner()实现将图片转圆角

  执行效果如图:

/***图片转圆角 * @param bitmap需要转的bitmap* @param pixels转圆角的弧度* @return 转圆角的bitmap*/public static Bitmap toRoundCorner(Bitmap bitmap, int pixels)    {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);final float roundPx = pixels;paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);if (bitmap != null && !bitmap.isRecycled()){bitmap.recycle();}return output;}

5、toRoundBitmap()方法将图像裁剪成圆形

  执行效果如图:

public static Bitmap toRoundBitmap(Bitmap bitmap){
if (bitmap == null){return null;}int width = bitmap.getWidth();int height = bitmap.getHeight();float roundPx;float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;if (width <= height){roundPx = width / 2;top = 0;bottom = width;left = 0;right = width;height = width;dst_left = 0;dst_top = 0;dst_right = width;dst_bottom = width;} else{roundPx = height / 2;float clip = (width - height) / 2;left = clip;right = width - clip;top = 0;bottom = height;width = height;dst_left = 0;dst_top = 0;dst_right = height;dst_bottom = height;}Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect src = new Rect((int) left, (int) top, (int) right,(int) bottom);final Rect dst = new Rect((int) dst_left, (int) dst_top,(int) dst_right, (int) dst_bottom);final RectF rectF = new RectF(dst);paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, src, dst, paint);if (bitmap != null && !bitmap.isRecycled()){bitmap.recycle();bitmap = null;}return output;}

6、rotaingImageView()方法,实现旋转图片

执行效果如图:

/*** 旋转图片* @param angle 旋转角度* @param bitmap 要处理的Bitmap* @return 处理后的Bitmap*/public static Bitmap rotaingImageView(int angle, Bitmap bitmap){// 旋转图片 动作Matrix matrix = new Matrix();matrix.postRotate(angle);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);if (resizedBitmap != bitmap && bitmap != null && !bitmap.isRecycled()){bitmap.recycle();bitmap = null;}return resizedBitmap;}

7、saveBmpToSd()实现将保存Bitmap到sdcard

        public static boolean saveBmpToSd(String dir, Bitmap bm, String filename,int quantity, boolean recyle) {boolean ret = true;if (bm == null) {return false;}File dirPath = new File(dir);if (!exists(dir)) {dirPath.mkdirs();}if (!dir.endsWith(File.separator)) {dir += File.separator;}File file = new File(dir + filename);OutputStream outStream = null;try {file.createNewFile();outStream = new FileOutputStream(file);bm.compress(Bitmap.CompressFormat.JPEG, quantity, outStream);} catch (Exception e) {e.printStackTrace();ret = false;} finally {try {if (outStream != null) outStream.close();} catch (IOException e) {e.printStackTrace();}if (recyle && !bm.isRecycled()) {bm.recycle();bm = null;}}return ret;}
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/Im-Victor/p/10102194.html

Android实现对图片的缩放、剪切、旋转、存储相关推荐

  1. java 给图片加马赛克_java处理图片--图片的缩放,旋转和马赛克化

    下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. 这是我自己结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化.(转 ...

  2. 安卓图片手势缩放、旋转、移动

    两个ImageView都可以手势缩放.旋转.移动 自定义ImageView一 import android.content.Context; import android.graphics.Matri ...

  3. android地图旋转监听,android百度地图:地图缩放、旋转、俯视角度以及屏幕像素与经纬度的转换Projection...

    MapControlDemo.java 通过MapController设置缩放.旋转.俯视角度private void perfomZoom(){ EditText t = (EditText) fi ...

  4. java图片镜像代码_java图片基本操作-缩放,旋转,镜像,拼接

    /*** 缩放*/ public static void zoomByScale(BufferedImage bufImage, double scale) throwsIOException {// ...

  5. android 点击图片旋转90度,Android UI之ImageView实现图片旋转和缩放

    这一篇,给大家介绍一下ImageView控件的使用,ImageView主要是用来显示图片,可以对图片进行放大.缩小.旋转的功能. android:sacleType属性指定ImageVIew控件显示图 ...

  6. Android自定义控件篇 图片进行平移,缩放,旋转

    一.自定义属性 <declare-styleable name="SingleTouchView"><attr name="src" form ...

  7. Android单点触控技术,对图片进行平移,缩放,旋转操作

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/42833893),请尊重他人的辛勤劳动成果,谢谢! 相信大家 ...

  8. java马赛克_java实现图片缩放、旋转和马赛克化

    本文是作者结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化. 不多说,上代码: package deal; import java.awt.Color; import jav ...

  9. java图片马赛克_java实现图片缩放、旋转和马赛克化

    本文是作者结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化. 不多说,上代码: package deal; import java.awt.Color; import jav ...

最新文章

  1. update 改写 merge into
  2. 服务器架构有哪些方式?—Vecloud微云
  3. JavaScript/JS的学习
  4. Hive SQL的编译过程
  5. 容器编排技术 -- Kubernetes kubectl create service externalname 命令详解
  6. 应用zip压缩的javascript以及Egret H5游戏实战
  7. 译林 五年级上 单词_牛津译林版九年级英语上Unit1单元重点单词、词组和句型总结...
  8. Google Code Review 代码审查速度
  9. 游戏手柄延迟测试软件,六款免费网络延迟测试工具
  10. centos漏洞系列(三):Google Android libnl权限提升漏洞
  11. gnuplot软件学习笔记
  12. spry提示信息设置html,CSS教程:12.4 借助于Spry实现折叠面板
  13. 小米手机ADB删除系统应用去广告
  14. 如何在Cisco Packet Tracer中创建多个路由器虚拟局域网(方法四)
  15. 量化交易策略matlab交易方案,Matlab量化交易策略之 GFTD+止损 附源码
  16. php java集成_PHP和Java 集成开发详解分析 强强联合第1/4页
  17. python把多个人声分离_一段音频中判断多个人声?
  18. 优先队列——最短路径
  19. 哪些人能创建百度百科词条,创建百科有什么规则
  20. C语言数据结构:什么是树?什么是二叉树?

热门文章

  1. 源码|并发一枝花之CopyOnWriteArrayList
  2. HBase参数配置及说明
  3. Winform 绘制圆形的图片
  4. PYTHON线程知识再研习F---队列同步Queue
  5. read(10, quot;NTP0 13690\nquot;, 64) 数据库登录缓慢
  6. 专家门诊[第258期] 备战2012下半年软考——项目管理、网络工程
  7. 为什么不让安装卫xing×××啊
  8. 前端定时器 setInterval 和 setTimeout
  9. JAVA SE学习day_08:TCP通信、多线程(并联)
  10. 文件和参数一起上传_Spring boot的文件上传