收集了非常多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;
}
}

Android中经常使用的bitmap处理方法的更多相关文章

  1. Android 中对于图片的内存优化方法

    Android 中对于图片的内存优化方法,需要的朋友可以参考一下     1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFact ...

  2. Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...

  3. 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

    原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...

  4. Android中解析XML格式数据的方法

    XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...

  5. Android中利用C++处理Bitmap对象

    相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...

  6. URL转Drawable之 Android中获取网络图片的三种方法

    转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...

  7. 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布 ...

  8. Android中传递对象的三种方法

    Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者I ...

  9. Android中定时执行任务的3种实现方法

    在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二.采用Handler的postDelayed(Runn ...

随机推荐

  1. 【08】node 之 fs文件

    var fs = require("fs");//fs 系统文件模块,对文件进行操作.Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如读取文件内容的函数有 ...

  2. 【DirectX SDK Extra】提示缺少Qedit.h问题 (转)

    原文转自 http://blog.csdn.net/joeblackzqq/article/details/10944005 DirectX 9.0 SDK 开发包以及扩展包下载(February 2 ...

  3. Linux Shell 文本处理工具集锦【转】

    转自:http://www.cnblogs.com/me115/p/3427319.html 内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq ...

  4. Matcher类详解

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它包括两个类:Pattern和Matcher . Pattern: 一个Pattern是一个正则表达式经编 ...

  5. pyinstaller打包exe程序各种坑!!!

    pyinstaller打包python成exe可执行程序,各种报错,各种坑,在次记录下 一.pyinstaller打包报错for real_module_name, six_moduleAttribu ...

  6. nodejs后台启动

    可避免关闭窗口,程序就关闭,可在后台运行 安装forever包,一般用于服务器,调试环境可不安装 npm install forever -g 启动方式如图: 查询后台运行哪些程序 forever l ...

  7. Android Studio 打开后无故爆红后解决办法

    今天打开AndroidSutudio后表示一脸蒙蔽,项目无故爆红,我本以为是哪里的代码有错导致 报错,于是乎逐个检查,但是并没有发现任何问题,然后CelarProduct,ReBuildProduct ...

  8. 判断图连通的三种方法——dfs,bfs,并查集

    Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...

  9. NULL的学问

    在数据库中存在一种特殊的值:NULL(空值).一个字段如果没有被赋值,那么它的值就是NULL,NULL并不代表没有值而是表示值未知.员工信息表中存储着身份证号.姓名.年龄等信息,其中某条记录中年龄字段 ...

  10. SPOJ 20713 DIVCNT2 - Counting Divisors (square)

    DIVCNT2 - Counting Divisors (square) #sub-linear #dirichlet-generating-function Let \sigma_0(n)σ​0​​ ...