Android-BitmapUtil工具类
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工具类的更多相关文章
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- 【转】Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...
- Android基础工具类重构系列一Toast
前言: 一直在考虑写一下Android实际项目中的一些总结,翻看CSDN博客,上一篇已经是一年多曾经. 本系列定位Android基础工具类重构.旨在记录实际项目中经经常使用到的一些工具类,比方Toas ...
- (转载)android 一些工具类汇总
android 一些工具类汇总 作者:曾田生z 字体:[增加 减小] 类型:转载 时间:2016-08-14我要评论 本文给大家汇总介绍了一些常用的Android工具类,非常的简单实用,有需要的小伙伴 ...
- 随笔分类 - Android之工具类
Android之文件搜索工具类 /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 */ private void sear ...
- Android 系统工具类SystemUtils
包含的功能有: 获取系统中所有APP应用.获取用户安装的APP应用.根据包名和Activity启动类查询应用信息.跳转到WIFI设置.WIFI网络开关.移动网络开关.GPS开关 当前若关则打开 当前若 ...
- Android Sqlite 工具类封装
鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- Android常见工具类封装
MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...
随机推荐
- Ubuntu 安装 Zabbix 3.2详细步骤
创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...
- rabbitmq web管理界面 用户管理
安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...
- 使用sizemask来确保索引不越界
[使用sizemask来确保索引不越界] 在redis的字典实现中,有一个sizemask字段. 此字段的作用是当使用下标访问数据时,确保下标不越界. 如当前size为8时,sizemask为7(0x ...
- js简单校验form表单
/** * 数据简单校验 */ function checkData (formId) { var check = true; var emailReg = new RegExp("^[a- ...
- 【校招面试 之 C/C++】第7题 C++构造函数不能是虚函数的原因
1.虚拟函数调用只需要“部分的”信息,即只需要知道函数接口,而不需要对象的具体类型.但是构建一个对象,却必须知道具体的类型信息.如果你调用一个虚拟构造函数,编译器怎么知道你想构建是继承树上的哪种类型呢 ...
- 6-Qt给父widget加上styleSheet(添加背景图)而不改变子widget的styleSheet的方法
Qt给父widget加上styleSheet(添加背景图)而不改变子widget的styleSheet的方法 比如用stylesheet给widget加背景图,可以用qt designer修改ui文件 ...
- ueditor使用注意
问题1:后端配置项没有正常加载,上传插件不能正常使用! 我用的是开发版,1.4.3.3 .Net版 网上查了很多,后来发现只是配置的问题而已. 1.在根目录下:config.json 其中有Img上传 ...
- jquery节点获取
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(& ...
- 7.11 cookie 失效后 ,重新登陆 页面 可能跳出 框架 ,只剩主题 部分 ,
判断地址 不在 框架里 (项目 地址栏一般 都是 首页地址 ) function url(){ var page=getpage(); if(window==top&&(page ...
- Quartz教程三:Job与JobDetail介绍
Quartz教程三:Job与JobDetail介绍 原文链接 | 译文链接 | 翻译:nkcoder | 校对: 本系列教程由quartz-2.2.x官方文档翻译.整理而来,希望给同样对quartz感 ...