收集了非常多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. windows 批处理删除指定目录下 指定类型 指定天数之前文件

    删除D:\test下5天前所有文件,如下: @echo offset SrcDir=D:\testset DaysAgo=5forfiles /p %SrcDir% /s /m *.* /d -%Da ...

  2. sql中的like和正则的区别

    like,模糊查询,更多的是用于匹配已知的字符,比如查询该字段含有1的记录,like ‘%1%’:但是如果要匹配不确定的,但是一个系列的字符,比如数字,最好用regexpselect * from t ...

  3. vue-router 页面切换后保持在页面顶部而不是保持原先的滚动位置的办法

    vue-router有提供一个方法scrollBehavior,它可以使切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. 这个功能只在 HTML5 history 模 ...

  4. poj 3250 Bad Hair Day

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21084   Accepted: 7202 Des ...

  5. 用dataset做数据源时,让gridview显示的列名与数据库表中的字段名不同

    原文发布时间为:2008-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 确定GridView的AutoGenerateColumns设置为False;使用GridView的“编辑列”,添 ...

  6. react dva 表单校验

    import React,{ Component } from 'react'; import { connect } from 'dva'; import { WhiteSpace,NavBar , ...

  7. hdu 3518 Boring counting 后缀数组 height分组

    题目链接 题意 对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\). 思路 枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分. 显然,对于每一 ...

  8. 安装配置Vim中文帮助文档

    1.home/.vimrc是用户自己的vim配置文件,在这个配置文件中设置的配置只影响该用安装前的准备工作: 在home目录下列新建文件夹  : .vim ------------------> ...

  9. linux device tree源代码解析【转】

    转自:http://blog.csdn.net/Tommy_wxie/article/details/42806457 //Basedon Linux v3.14 source code Linux设 ...

  10. BZOJ——3412: [Usaco2009 Dec]Music Notes乐谱

    http://www.lydsy.com/JudgeOnline/problem.php?id=3412 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit:  ...