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. Linux就业技术指导(七):游戏类运维重点流程解析

    一,某游戏公司例行上线与更新流程示例 例行维护/更新流程 1.1 更新前天 提前确认好要更新的是什么,更新会有人通知你,一般是运营人员 比如:我们明天做什么什么更新 1.2 第2天更新 一般固定点更新 ...

  2. ThreadPoolExecutor的execute源码分析

    上一篇文章指出,ThreadPoolExecutor执行的步骤如下: 向线程池中添加任务,当任务数量少于corePoolSize时,会自动创建thead来处理这些任务: 当添加任务数大于corePoo ...

  3. 第八章 高级搜索树 (xa4)红黑树:删除

  4. np.hsplit()

    numpy.hsplit numpy.hsplit(ary, indices_or_sections)[source] Split an array into multiple sub-arrays ...

  5. rubber

    rubber - 必应词典 美['rʌbər]英['rʌbə(r)] n.橡胶:橡皮:黑板擦 v.涂橡胶于…:〈美俚〉同“rubberneck” 网络橡皮擦:胶皮:橡皮轮胎 变形复数:rubbers:

  6. .net的内置对象

    一 . 获取客户端,服务器端信息: Response.Write("客户端信息:"); Response.Write("<br>浏览器类型,版本:" ...

  7. 【校招面试 之 C/C++】第4题 拷贝构造函数被调用的3个时机

    1.被调用的3个时机: (1)直接初始化或拷贝初始化: (2)将一个对象作为一个实参传递,形参采用非指针或非引用的对象进行接收时(指针即指向了同一块空间,并未实现拷贝:而引用就是实参本身): (3)函 ...

  8. mybatis遍历array数组与集合、json

    遍历数组: int saveOptions(@Param("items")String[] items, @Param("questionId")int que ...

  9. OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...

  10. Cairo编程

    一.简介 cairo 是一个免费的矢量绘图软件库,它可以绘制多种输出格式.cairo 支持许多平台,包括 Linux.BSD.Microsoft® Windows® 和 OSX(BeOS 和 OS2 ...