Android中常用的bitmap处理方法 (bitmap工具类)

2024-06-09 23:18

本文主要是介绍Android中常用的bitmap处理方法 (bitmap工具类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

收集了很多bitmap相关的处理方法,几乎全部应用在项目中,所以特记录下!

[java]  view plain copy
  1. package com.tmacsky.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.content.Context;  
  7. import android.content.res.Resources;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.Matrix;  
  12. import android.graphics.Paint;  
  13. import android.graphics.PixelFormat;  
  14. import android.graphics.PorterDuffXfermode;  
  15. import android.graphics.Rect;  
  16. import android.graphics.RectF;  
  17. import android.graphics.Bitmap.Config;  
  18. import android.graphics.PorterDuff.Mode;  
  19. import android.graphics.drawable.BitmapDrawable;  
  20. import android.graphics.drawable.Drawable;  
  21. import android.view.View;  
  22. import android.view.View.MeasureSpec;  
  23.   
  24. public class ImageUtils {  
  25.   
  26.     //--->bitmap相关  
  27.     //参考网站http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-drawable.html  
  28.     // 见博客:http://blog.sina.com.cn/s/blog_afb547c60101j7qn.html  
  29.     /** 
  30.      * View转成bitmap 
  31.      * @param view 
  32.      * @return 
  33.      */  
  34.     public static Bitmap convertViewToBitmap(View view) {  
  35.         view.setDrawingCacheEnabled(true);  
  36.         view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  37.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  38.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  39.         view.buildDrawingCache();  
  40.         return view.getDrawingCache();  
  41.     }  
  42.     /** 
  43.      * 缩放Drawable 
  44.      * @param drawable 
  45.      * @param w  缩放后需要的宽度 
  46.      * @param h  缩放后需要的高度 
  47.      * @return 
  48.      */  
  49.     public static Drawable zoomDrawable(Drawable drawable, int w, int h) {  
  50.         int width = drawable.getIntrinsicWidth();  
  51.         int height = drawable.getIntrinsicHeight();  
  52.         // drawable转换成bitmap  
  53.         Bitmap oldbmp = drawableToBitmap(drawable);  
  54.         // 创建操作图片用的Matrix对象  
  55.         Matrix matrix = new Matrix();  
  56.         // 计算缩放比例  
  57.         float sx = ((float) w / width);  
  58.         float sy = ((float) h / height);  
  59.         // 设置缩放比例  
  60.         matrix.postScale(sx, sy);  
  61.         // 建立新的bitmap,其内容是对原bitmap的缩放后的图  
  62.         Bitmap newbmp = Bitmap.createBitmap(oldbmp, 00, width, height,  
  63.                 matrix, true);  
  64.         return new BitmapDrawable(newbmp);  
  65.     }  
  66.       
  67.     /** 
  68.      * 缩放bitmap 
  69.      * @param oldBitmap 输入bitmap 
  70.      * @param newWidth  
  71.      * @param newHeight 
  72.      * @return 
  73.      */  
  74.     public static Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {  
  75.         // 获得图片的宽高  
  76.         int width = oldBitmap.getWidth();  
  77.         int height = oldBitmap.getHeight();  
  78.         // 计算缩放比例  
  79.         float scaleWidth = ((float) newWidth) / width;  
  80.         float scaleHeight = ((float) newHeight) / height;  
  81.         // 取得想要缩放的matrix参数  
  82.         Matrix matrix = new Matrix();  
  83.         matrix.postScale(scaleWidth, scaleHeight);  
  84.         // 得到新的图片  
  85.         Bitmap newbm = Bitmap.createBitmap(oldBitmap, 00, width, height, matrix,  
  86.                 true);  
  87.         return newbm;  
  88.     }  
  89.     /** 
  90.      * 缩放网络图片 依赖于zoomBitmap 
  91.      * @param img 
  92.      * @param newWidth 
  93.      * @param newHeight 
  94.      * @return 
  95.      */  
  96.     public static Bitmap zoomImg(String img, int newWidth, int newHeight) {  
  97.         // 图片源  
  98.         Bitmap bm = BitmapFactory.decodeFile(img);  
  99.         if (null != bm) {  
  100.             return zoomBitmap(bm, newWidth, newHeight);  
  101.         }  
  102.         return null;  
  103.     }  
  104.     /** 
  105.      * 缩放网络图片 依赖于zoomBitmap 
  106.      * @param context 
  107.      * @param img 
  108.      * @param newWidth 
  109.      * @param newHeight 
  110.      * @return 
  111.      */  
  112.     public static Bitmap zoomImg(Context context, String img, int newWidth,  
  113.             int newHeight) {  
  114.         // 图片源  
  115.         try {  
  116.             Bitmap bm = BitmapFactory.decodeStream(context.getAssets()  
  117.                     .open(img));  
  118.             if (null != bm) {  
  119.                 return zoomBitmap(bm, newWidth, newHeight);  
  120.             }  
  121.         } catch (IOException e) {  
  122.             // TODO Auto-generated catch block  
  123.             e.printStackTrace();  
  124.         }  
  125.         return null;  
  126.     }  
  127.     /** 
  128.      * 判断bitmap是否存在 
  129.      * @param bitmap 
  130.      * @return 
  131.      */  
  132.     public static boolean bitmapAvailable(Bitmap bitmap) {  
  133.         return bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0;  
  134.     }  
  135.     /** 
  136.      * drawable 转成bitmap 
  137.      * @param drawable 
  138.      * @return 
  139.      */  
  140.     public static Bitmap drawableToBitmap(Drawable drawable) {  
  141.         // 取 drawable 的长宽  
  142.         int w = drawable.getIntrinsicWidth();  
  143.         int h = drawable.getIntrinsicHeight();  
  144.         // 取 drawable 的颜色格式  
  145.         Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  146.                 : Bitmap.Config.RGB_565;  
  147.         // 建立对应 bitmap  
  148.         Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
  149.         // 建立对应 bitmap 的画布  
  150.         Canvas canvas = new Canvas(bitmap);  
  151.         drawable.setBounds(00, w, h);  
  152.         // 把 drawable 内容画到画布中  
  153.         drawable.draw(canvas);  
  154.         return bitmap;  
  155.     }  
  156.     /** 
  157.      * Bitmap转换成Drawable 
  158.      * @param context 
  159.      * @param bitmap 
  160.      * @return 
  161.      */  
  162.     public static Drawable bitmapToDrawable(Context context,Bitmap bitmap){  
  163.         //因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。  
  164.         BitmapDrawable bd= new BitmapDrawable(context.getResources(), bitmap);  
  165.         return bd;  
  166.     }  
  167.       
  168.     /** 
  169.      * 从资源中获取Bitmap 
  170.      * @param context 
  171.      * @param req  R.drawable.icon(eg.) 
  172.      * @return 
  173.      */  
  174.     public Bitmap getBitmapFromResources(Context context,int req){  
  175.           Resources res = context.getResources();  
  176.           Bitmap bmp = BitmapFactory.decodeResource(res, req);  
  177.           return bmp;  
  178.     }  
  179.       
  180.     /** 
  181.      * Byte[] -> Bitmap的转换 
  182.      */  
  183.     public Bitmap Bytes2Bimap(byte[] b) {  
  184.         if (b.length != 0) {  
  185.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  186.         } else {  
  187.             return null;  
  188.         }  
  189.     }  
  190.     /** 
  191.      * Bitmap->Byte[]的转换 
  192.      * @param bm 
  193.      * @return 
  194.      */  
  195.      public byte[] Bitmap2Bytes(Bitmap bm) {  
  196.          ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  197.          bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  198.          return baos.toByteArray();  
  199.     }  
  200.     /** 
  201.      * 获取圆角图片 
  202.      * @param bitmap 
  203.      * @param roundPx 圆角的弧度 
  204.      * @return 
  205.      */  
  206.     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {  
  207.         int w = bitmap.getWidth();  
  208.         int h = bitmap.getHeight();  
  209.         Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);  
  210.         Canvas canvas = new Canvas(output);  
  211.         final int color = 0xff424242;  
  212.         final Paint paint = new Paint();  
  213.         final Rect rect = new Rect(00, w, h);  
  214.         final RectF rectF = new RectF(rect);  
  215.         paint.setAntiAlias(true);  
  216.         canvas.drawARGB(0000);  
  217.         paint.setColor(color);  
  218.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  219.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  220.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  221.         return output;  
  222.     }  
  223. }  

转自:http://blog.csdn.net/tmacsky/article/details/38121283


这篇关于Android中常用的bitmap处理方法 (bitmap工具类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1046579

相关文章

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySql死锁怎么排查的方法实现

《MySql死锁怎么排查的方法实现》本文主要介绍了MySql死锁怎么排查的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录前言一、死锁排查方法1. 查看死锁日志方法 1:启用死锁日志输出方法 2:检查 mysql 错误

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -