Android中经常使用的bitmap处理方法
收集了非常多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处理方法的更多相关文章
- Android 中对于图片的内存优化方法
Android 中对于图片的内存优化方法,需要的朋友可以参考一下 1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFact ...
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...
- 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)
原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...
- Android中解析XML格式数据的方法
XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...
- Android中利用C++处理Bitmap对象
相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...
- URL转Drawable之 Android中获取网络图片的三种方法
转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...
- 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布 ...
- Android中传递对象的三种方法
Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者I ...
- Android中定时执行任务的3种实现方法
在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二.采用Handler的postDelayed(Runn ...
随机推荐
- 洛谷 [P3496] BLO
割点 首先 tarjan 求割点, 对于不是割点的点, 答案是 2 * (n-1) 有序,所以要乘 2 对于是割点的点, 答案是删去该点后所有连通块的个数加上 n-1 在乘 2 #include &l ...
- 洛谷 [P3620] 数据备份
贪心神题 首先我们发现一个显然的贪心策略,连接相邻两个写字楼总是更优. 所以本题就变成了数轴上一堆点,要选 k 个彼此不相邻的区间,使得区间长度最小 对于 10000 的数据来说,我们可以用 DP 解 ...
- [LeetCode] Climbing Stairs 斐波那契数列
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 反射的基本使用以及原理(Class获取方式)
1.什么是反射技术? 动态获取指定类以及类中的内容(成员),并运行其内容. 应用程序已经运行,无法在其中进行new对象的建立,就无法使用对象.这时可以根据配置文件的类全名去找对应的字节码文件,并加载进 ...
- 移动电子商务:五个技术标准与Trustonic TEE解决方案【转】
转自:http://www.vonwei.com/post/mobileTrustonicTEE.html 转载申明:本站原创,欢迎转载.但转载时请保留原文地址.原文地址:http://www.von ...
- python 集合比较(交集、并集,差集)
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- hdu 4522(图论,构图)
湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- 洛谷——P1785 漂亮的绝杀
P1785 漂亮的绝杀 题目背景 话说absi2011的企鹅在和斗神塔第60层的Boss战斗 不好,这局要输了,企鹅还剩4血了Boss还有392呢,哇,漂亮——红缨枪连击,280,343(暴击),绝杀 ...
- go语言学习之路六:接口详解
Go语言没有类和继承的概念,但是接口的存在使得它可以实现很多面向对象的特性.接口定义了一些方法,但是这些方法不包含实现的代码.也就是说这些代码没有被实现(抽象的方法).同时接口里面也不包含变量. 看一 ...
- 苹果放宽了 iOS 5.0 对应用本地存储的限制
iOS5.0引入了iCloud,让那些需要本地存储较多数据的app开发者(比如支持离线的杂志,新闻类app)陷入了 尴尬的境地,因为将大量数据存储在/Documents 文件夹将导致iCloud同步变 ...