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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android Bitmap用法总结 Bitmap用法总结 1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } 2、从资源获取Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 3、Bitmap → byte[] private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } 4、byte[] → Bitmap private Bitmap Bytes2Bimap(byte[] b){ if(b.length!=0){ return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } } 5、保存bitmap static boolean saveBitmap2file(Bitmap bmp,String filename){ CompressFormat format= Bitmap.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try { stream = new FileOutputStream("/sdcard/" + filename); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. e.printStackTrace(); } return bmp.compress(format, quality, stream); } 6、将图片按自己的要求缩放 // 图片源 Bitmap bm = BitmapFactory.decodeStream(getResources() .openRawResource(R.drawable.dog)); // 获得图片的宽高 int width = bm.getWidth(); int height = bm.getHeight(); // 设置想要的大小 int newWidth = 320; int newHeight = 480; // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片 Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); // 放在画布上 canvas.drawBitmap(newbm, 0, 0, paint); 相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html 7、bitmap用法小结 BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出 Bitmap bm = BitmapFactory.decodeFile("",option);//文件流 URL url = new URL(""); InputStream is = url.openStream(); Bitmap bm = BitmapFactory.decodeStream(is); android:scaleType: android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别: CENTER /center 按图片的原来size居显示,当图片长/宽超过View的长/宽,则截取图片的居部分 显示 CENTER_CROP / centerCrop 按比例扩大图片的size居显示,使得图片长(宽)等于或大于View的长 (宽) CENTER_INSIDE / centerInside 将图片的内容完整居显示,通过按比例缩小或原来的size使得图片 长/宽等于或小于View的长/宽 Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示 MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。 //放大缩小图片 public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidht = ((float)w / width); float scaleHeight = ((float)h / height); matrix.postScale(scaleWidht, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return newbmp; } //将Drawable转化为Bitmap public static Bitmap drawableToBitmap(Drawable drawable){ int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0,0,width,height); drawable.draw(canvas); return bitmap; Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. } //获得圆角图片的方法 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ 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); 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); return output; } //获得带倒影的图片方法 public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ final int reflectionGap = 4; int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. canvas.drawRect(0, height,width,height + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值