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

来源:互联网 发布:linux awk 内置函数 编辑:程序博客网 时间:2024/05/02 03:02

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

[java] view plaincopy
  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


0 0
原创粉丝点击