Bitmap工具类,获取Bitmap对象

public class BitmapUtil {

    private BitmapUtil(){}

    /**
* 根据资源id获取指定大小的Bitmap对象
* @param context 应用程序上下文
* @param id 资源id
* @param height 高度
* @param width 宽度
* @return
*/
public static Bitmap getBitmapFromResource(Context context, int id, int height, int width){
Options options = new Options();
options.inJustDecodeBounds = true;//只读取图片,不加载到内存中
BitmapFactory.decodeResource(context.getResources(), id, options);
options.inSampleSize = calculateSampleSize(height, width, options);
options.inJustDecodeBounds = false;//加载到内存中
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id, options);
return bitmap;
} /**
* 根据文件路径获取指定大小的Bitmap对象
* @param path 文件路径
* @param height 高度
* @param width 宽度
* @return
*/
public static Bitmap getBitmapFromFile(String path, int height, int width){
if (TextUtils.isEmpty(path)) {
throw new IllegalArgumentException("参数为空,请检查你选择的路径:" + path);
}
Options options = new Options();
options.inJustDecodeBounds = true;//只读取图片,不加载到内存中
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateSampleSize(height, width, options);
options.inJustDecodeBounds = false;//加载到内存中
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
} /**
* 获取指定大小的Bitmap对象
* @param bitmap Bitmap对象
* @param height 高度
* @param width 宽度
* @return
*/
public static Bitmap getThumbnailsBitmap(Bitmap bitmap, int height, int width){
if (bitmap == null) {
throw new IllegalArgumentException("图片为空,请检查你的参数");
}
return ThumbnailUtils.extractThumbnail(bitmap, width, height);
} /**
* 将Bitmap对象转换成Drawable对象
* @param context 应用程序上下文
* @param bitmap Bitmap对象
* @return 返回转换后的Drawable对象
*/
public static Drawable bitmapToDrawable(Context context, Bitmap bitmap){
if (context == null || bitmap == null) {
throw new IllegalArgumentException("参数不合法,请检查你的参数");
}
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
return drawable;
} /**
* 将Drawable对象转换成Bitmap对象
* @param drawable Drawable对象
* @return 返回转换后的Bitmap对象
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
throw new IllegalArgumentException("Drawable为空,请检查你的参数");
}
Bitmap bitmap =
Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} /**
* 将Bitmap对象转换为byte[]数组
* @param bitmap Bitmap对象
* @return 返回转换后的数组
*/
public static byte[] bitmapToByte(Bitmap bitmap){
if (bitmap == null) {
throw new IllegalArgumentException("Bitmap为空,请检查你的参数");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} /**
* 计算所需图片的缩放比例
* @param height 高度
* @param width 宽度
* @param options options选项
* @return
*/
private static int calculateSampleSize(int height, int width, Options options){
int realHeight = options.outHeight;
int realWidth = options.outWidth;
int heigthScale = realHeight / height;
int widthScale = realWidth / width;
if(widthScale > heigthScale){
return widthScale;
}else{
return heigthScale;
}
}
}

Android-BitmapUtil工具类的更多相关文章

  1. 53. Android常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...

  2. Android 常见工具类封装

    1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...

  3. 【转】Android常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...

  4. Android基础工具类重构系列一Toast

    前言: 一直在考虑写一下Android实际项目中的一些总结,翻看CSDN博客,上一篇已经是一年多曾经. 本系列定位Android基础工具类重构.旨在记录实际项目中经经常使用到的一些工具类,比方Toas ...

  5. (转载)android 一些工具类汇总

    android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...

  6. 随笔分类 - Android之工具类

    Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...

  7. Android 系统工具类SystemUtils

    包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...

  8. Android Sqlite 工具类封装

    鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...

  9. Android 常用工具类之SPUtil,可以修改默认sp文件的路径

    参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...

  10. Android常见工具类封装

    MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...

随机推荐

  1. 对象池 object pool

    对象池适用于: 对象的创建很耗时 对象频繁地释放再创建 对象池的实现:将释放的对象放进对象池中,在新建对象时,从对象池取对象 public class ObjectPool<T> wher ...

  2. The Unique MST

    The Unique MST http://poj.org/problem?id=1679 Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. Message: u'The given selector btn dropdown-toggle btn-info is either invalid or does not result in a WebElement

    html代码: <html> <head> <meta http-equiv="content-type" content="text/ht ...

  4. apache中开启rewrite

    1.在apache配置文件httpd.conf中找到如下行: #LoadModule rewrite_module modules/mod_rewrite.so 去掉该行前面的#号 2.在httpd. ...

  5. SSL - 简介

    一.密码技术 要了解SSL协议,首先要了解:加密算法.消息摘要算法(又称为哈希算法Hash),数字签名等概念.这些技术每个都可以写出一整本的书,它们结合在一起,提供了保密性.完整性和身份验证的功能. ...

  6. 为什么二代测序的原始数据中会出现Read重复现象?

    为什么二代测序的原始数据中会出现Read重复现象? 要搞清楚这个read重复(duplicate)的问题,我想我们需要从NGS数据的产出过程说起,具体来说如下: 基因组DNA提取: DNA随机打断,最 ...

  7. linux两个线程

    http://blog.csdn.net/marksinoberg/article/details/50945212

  8. Autotest Weekly Report

    Autotest Weekly Report Reported by: 12/16/2013 What I Did Last Week Debug autotest scripts of ‘smart ...

  9. web札记

    url中不能是#号,struts不读取#之后的字符串.

  10. part1:8-远程登录Linux

    Linux远程登录 Linux系统中是通过ssh服务实现的远程登录功能.默认ssh服务开启了22端口,而且在安装完成系统时,这个服务已经安装,并且是开机启动的.所以不需要额外配置就能直接远程登录Lin ...