收集了非常多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. pom.xml(Project Object Model) 文件简单介绍

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. 【HDOJ5517】Triple(二维BIT)

    题意:给你n个二元组<a,b>, m个三元组<c,d,e>. 如果d = e,那么<a,c,d>会组成一个新的三元组集合G. 问G中有多少个三元组在凸点.(没有其它 ...

  3. 【HDOJ5951】Winning an Auction(博弈DP)

    题意:A和B两个人做一个拍卖游戏.每一轮两人分别给出一个价格,出价高者获得该轮的物品,出价相同则奇数轮A优先,偶数轮B优先. 两个人的目标都是最大化自己的商品数量,给定轮数n与两人分别的总资金a,b, ...

  4. Android开发跳坑记录

    本文主要记录在Android开发中遇见的一些问题,以及解决方法. 2015.12.01 1.adb.exe 端口被占用 解决: http://blog.csdn.net/xiaanming/artic ...

  5. sublime 常用快捷键备忘

    转一篇sublime常用的快捷键备忘 sublime常用快捷键 选择类Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本.Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本 ...

  6. Codeforces Round #449 Div. 2 A B C (暂时)

    A. Scarborough Fair 题意 对给定的长度为\(n\)的字符串进行\(m\)次操作,每次将一段区间内的某一个字符替换成另一个字符. 思路 直接模拟 Code #include < ...

  7. linux下不是很完美的提高android虚拟机的启动速度

    去年双十一换的新电脑,华硕vivo4000的,配置的不算很好,4k的屏幕:3840×2160, 940M的显卡, core i7的CPU, 8G的内存,硬盘是1T的机械硬盘,除了硬盘基本感觉还可以吧. ...

  8. hdu 2674(余数性质)

    N!Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. LeetCode OJ-- Longest Substring Without Repeating Characters ***@

    https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 给一个string,找出其中不含有重复 ...

  10. EasyUI左边树菜单和datagrid分页

    //这个页面是Home.html 1 <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...