Android中常用的bitmap处理方法 (bitmap工具类)
来源:互联网 发布:linux awk 内置函数 编辑:程序博客网 时间:2024/05/02 03:02
收集了很多bitmap相关的处理方法,几乎全部应用在项目中,所以特记录下!
- package com.tmacsky.utils;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import android.content.Context;
- import android.content.res.Resources;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.PixelFormat;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.graphics.Bitmap.Config;
- import android.graphics.PorterDuff.Mode;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.view.View;
- import android.view.View.MeasureSpec;
- public class ImageUtils {
- //--->bitmap相关
- //参考网站http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-drawable.html
- // 见博客:http://blog.sina.com.cn/s/blog_afb547c60101j7qn.html
- /**
- * View转成bitmap
- * @param view
- * @return
- */
- public static Bitmap convertViewToBitmap(View view) {
- view.setDrawingCacheEnabled(true);
- view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- view.buildDrawingCache();
- return view.getDrawingCache();
- }
- /**
- * 缩放Drawable
- * @param drawable
- * @param w 缩放后需要的宽度
- * @param h 缩放后需要的高度
- * @return
- */
- public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
- int width = drawable.getIntrinsicWidth();
- int height = drawable.getIntrinsicHeight();
- // drawable转换成bitmap
- Bitmap oldbmp = drawableToBitmap(drawable);
- // 创建操作图片用的Matrix对象
- Matrix matrix = new Matrix();
- // 计算缩放比例
- float sx = ((float) w / width);
- float sy = ((float) h / height);
- // 设置缩放比例
- matrix.postScale(sx, sy);
- // 建立新的bitmap,其内容是对原bitmap的缩放后的图
- Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
- matrix, true);
- return new BitmapDrawable(newbmp);
- }
- /**
- * 缩放bitmap
- * @param oldBitmap 输入bitmap
- * @param newWidth
- * @param newHeight
- * @return
- */
- public static Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {
- // 获得图片的宽高
- int width = oldBitmap.getWidth();
- int height = oldBitmap.getHeight();
- // 计算缩放比例
- float scaleWidth = ((float) newWidth) / width;
- float scaleHeight = ((float) newHeight) / height;
- // 取得想要缩放的matrix参数
- Matrix matrix = new Matrix();
- matrix.postScale(scaleWidth, scaleHeight);
- // 得到新的图片
- Bitmap newbm = Bitmap.createBitmap(oldBitmap, 0, 0, width, height, matrix,
- true);
- return newbm;
- }
- /**
- * 缩放网络图片 依赖于zoomBitmap
- * @param img
- * @param newWidth
- * @param newHeight
- * @return
- */
- public static Bitmap zoomImg(String img, int newWidth, int newHeight) {
- // 图片源
- Bitmap bm = BitmapFactory.decodeFile(img);
- if (null != bm) {
- return zoomBitmap(bm, newWidth, newHeight);
- }
- return null;
- }
- /**
- * 缩放网络图片 依赖于zoomBitmap
- * @param context
- * @param img
- * @param newWidth
- * @param newHeight
- * @return
- */
- public static Bitmap zoomImg(Context context, String img, int newWidth,
- int newHeight) {
- // 图片源
- try {
- Bitmap bm = BitmapFactory.decodeStream(context.getAssets()
- .open(img));
- if (null != bm) {
- return zoomBitmap(bm, newWidth, newHeight);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 判断bitmap是否存在
- * @param bitmap
- * @return
- */
- public static boolean bitmapAvailable(Bitmap bitmap) {
- return bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0;
- }
- /**
- * drawable 转成bitmap
- * @param drawable
- * @return
- */
- public static Bitmap drawableToBitmap(Drawable drawable) {
- // 取 drawable 的长宽
- int w = drawable.getIntrinsicWidth();
- int h = drawable.getIntrinsicHeight();
- // 取 drawable 的颜色格式
- Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565;
- // 建立对应 bitmap
- Bitmap bitmap = Bitmap.createBitmap(w, h, config);
- // 建立对应 bitmap 的画布
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, w, h);
- // 把 drawable 内容画到画布中
- drawable.draw(canvas);
- return bitmap;
- }
- /**
- * Bitmap转换成Drawable
- * @param context
- * @param bitmap
- * @return
- */
- public static Drawable bitmapToDrawable(Context context,Bitmap bitmap){
- //因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
- BitmapDrawable bd= new BitmapDrawable(context.getResources(), bitmap);
- return bd;
- }
- /**
- * 从资源中获取Bitmap
- * @param context
- * @param req R.drawable.icon(eg.)
- * @return
- */
- public Bitmap getBitmapFromResources(Context context,int req){
- Resources res = context.getResources();
- Bitmap bmp = BitmapFactory.decodeResource(res, req);
- return bmp;
- }
- /**
- * Byte[] -> Bitmap的转换
- */
- public Bitmap Bytes2Bimap(byte[] b) {
- if (b.length != 0) {
- return BitmapFactory.decodeByteArray(b, 0, b.length);
- } else {
- return null;
- }
- }
- /**
- * Bitmap->Byte[]的转换
- * @param bm
- * @return
- */
- public byte[] Bitmap2Bytes(Bitmap bm) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
- /**
- * 获取圆角图片
- * @param bitmap
- * @param roundPx 圆角的弧度
- * @return
- */
- public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
- Canvas canvas = new Canvas(output);
- final int color = 0xff424242;
- final Paint paint = new Paint();
- final Rect rect = new Rect(0, 0, w, h);
- 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;
- }
- }
转自:http://blog.csdn.net/tmacsky/article/details/38121283
0 0
- Android中常用的bitmap处理方法 (bitmap工具类)
- Android中常用的bitmap处理方法
- Android的Bitmap类中常用方法
- Android对Bitmap处理的工具类
- Android Bitmap类的常用方法
- Android中一些常用类的常用方法(Math、Random、Color、Paint、Canvas、Bitmap、BitmapFactory)
- Android Bitmap工具方法;
- bitMap处理工具类
- Android工具类之Bitmap图片处理
- Android工具类之Bitmap图片处理
- Bitmap常用操作工具方法
- Android处理Bitmap的一些方法(多)
- Android Bitmap 工具类
- Android Bitmap 工具类
- android中位图Bitmap工具类的实现
- 【bitmap】Android图片处理方法
- Bitmap 图片处理工具类
- Android模糊bitmap的工具类
- 编译程序出现 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 解决方法
- API经济蓄势待发
- DragNGo拖拽插件-- 操作更便捷:鼠标手势加拖拽
- Expedia的微渠道革命:API与新商业平台
- 闲杂
- Android中常用的bitmap处理方法 (bitmap工具类)
- 黑马程序员——Java高新技术
- 2014年值得关注的十个Hadoop大数据创业公司
- SRDJHLhfkj的卡和国际客户
- The Lottery uva+容斥原理+求最小公倍数(多个数)
- 计算机学院研发第二轮考核--------Activity的回调(startActivityForResult)
- Linux--线程编程
- POJ 1061 青蛙的约会
- 什么是SysWow64