Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上
Android开发过程中,我们有时需要动态得显示一些图片,并且这些图片的大小差距会十分大,如果需求并不是需要图片完整显示,但是需要不失真,并且要图片中间部分的情况下,我们需要做一系列处理,因为这个时候ImageView的各种scale type都不适用。具体步骤详见下面代码,大家也可以直接拷过去作为工具类使用
/**
* 获取正确缩放裁剪适应IamgeView的Bitmap
* @param imageView
* @param bitmap
* @return
*/
public static Bitmap createFitBitmap(ImageView imageView, Bitmap bitmap) {
Log.i(TAG, "createFitBitmap<---------------------");
int widthTarget = imageView.getWidth();
int heightTarget = imageView.getHeight();
int widthBitmap = bitmap.getWidth();
int heightBitmap = bitmap.getHeight();
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
Bitmap result = null;
if( widthBitmap >= widthTarget && heightBitmap >= heightTarget ){
result = createLargeToSmallBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
}
else if( widthBitmap >= widthTarget && heightBitmap < heightTarget ){
Bitmap temp = createLargeWidthToEqualHeightBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
}
else if( widthBitmap < widthTarget && heightBitmap >= heightTarget ){
Bitmap temp = createLargeHeightToEqualWidthBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
}
else{
Bitmap temp = createSmallToEqualBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createFitBitmap(imageView, temp);
}
Log.i(TAG, "createFitBitmap--------------------->");
return result;
} private static Bitmap createLargeToSmallBitmap( int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){
Log.i(TAG, "createLargeToSmallBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
int x = (widthBitmap - widthTarget) / 2;
int y = (heightBitmap - heightTarget) / 2;
Log.i(TAG, "createLargeToSmallBitmap--------------------->");
return Bitmap.createBitmap(bitmap, x, y, widthTarget, heightTarget);
} private static Bitmap createLargeWidthToEqualHeightBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createLargeWidthToEqualHeightBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scale = ( heightTarget * 1.0 ) / heightBitmap;
Log.i(TAG, "createLargeWidthToEqualHeightBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale) , heightTarget, false);
} private static Bitmap createLargeHeightToEqualWidthBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createLargeHeightToEqualWidthBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scale = ( widthTarget * 1.0 ) / widthBitmap;
Log.i(TAG, "createLargeHeightToEqualWidthBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, widthTarget , (int)(heightTarget * scale), false);
} private static Bitmap createSmallToEqualBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createSmallToEqualBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scaleWidth = ( widthTarget * 1.0 ) / widthBitmap;
double scaleHeight = ( heightTarget * 1.0 ) / heightBitmap;
double scale = Math.min(scaleWidth, scaleHeight);
Log.i(TAG, "createSmallToEqualBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale), (int)(heightBitmap * scale), false);
}
Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上的更多相关文章
- [Android] 拍照、截图、保存并显示在ImageView控件中
近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买 ...
- Android系统Bitmap内存分配原理与优化
一.前言 笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起.谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素--图片,如果对图 ...
- Android笔记--Bitmap(三) 针对不用Android版本的位图管理
Bitmap(三) | Android不同版本的相应操作 在不同的Android版本中.位图的存储方式是不同的. 1.小于等于 Android 2.2 (API level 8) 垃圾收集器回收内存时 ...
- int android.graphics.Bitmap.getRowBytes()
int android.graphics.Bitmap.getRowBytes() Return the number of bytes between rows in the bitmap's pi ...
- Android处理Bitmap的一些方法
http://www.it165.net/pro/html/201305/5795.html # 文件与Bitmap间的方法 1. 从文件载入Bitmap 01./** 02.* @brief 从文件 ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- Android笔记——Bitmap自动取色(纯搬运)
2015/6/12更新:发现一个更好的,带demo https://github.com/MichaelEvans/ColorArt 说明: 这个是一个老外写的自动自动从bitmap中取主色与第二主色 ...
- Android中bitmap的相关处理
加载大图片 Options options=new Options(); options.inJustDecodeBounds=true;//不加载图片,只加载文件信息 //加载图片,获取到配置信息 ...
- android 缓存Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...
随机推荐
- Flex坐标
flash和flex针对不同的目的,提供了3种不同的坐标系. 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容坐标系(相对于本地坐标系说的) 这些坐标系的点是可以转换的,并且有相应的方法 ...
- 译 - EF 6秘诀(第二版) - 目录
本博文系Entity Framework 6 Recipes, 2nd Edition的目录译文.保留原文,方便参考. 第一章 EF入门Chapter 1. Getting Started with ...
- Linux 网络编程 高级套接字
一.套接字选项: 有以下3中方式可以对套接字选项进行设置: getsockopt 和 setsockopt 函数 fcntl函数 ioctl函数 getsockopt和setsockopt函数用于获得 ...
- 理解sizeof
1.sizeof返回的是字节个数,内存编址的最小单元是字节.因此,空对象,bool值占用的内存也是一个字节. 2.可以对哪些东西求sizeof ? a.对象和类型.如int a; sizeof(a), ...
- 財哥面京东dm的经历【帮財哥发的】
关于面京东,感触仅仅有一个,虐的快吐血了.首先说京东分四个板块,有京东商城.京东金融.京东刚收购的拍拍和海外事业部.我这个职位主要是在金融部数据组做数据挖掘和机器学习,还有推荐系统.面试是在周 ...
- Android 图像压缩,和LRU算法使用的推荐链接
近两日,看的关于这些方面的一些教程数十篇,最好的当属google原版的教程了.国内有不少文章是翻译这个链接的. 需要注意的一点是:Android的SDK中的LRU算法在V4包和Util包中各有一个,推 ...
- 反编译android应用,降低权限去广告及重新签名
功能:反编译apk降低权限及重新签名 场景:很多软件,申请了一些可能会导致付费(如,发短信,呼叫号码)或者泄漏隐私(如:读取通讯录)的权限,让人很不放心.比如:飞信.墨迹天气.iReader等都在此列 ...
- [CSS] Introduction to CSS Columns
Learn how to use CSS columns to quickly lay out fluid columns that are responsive, degrade gracefull ...
- PHP定时执行任务的实现(转)
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...
- 内存管理和@property的属性
内存管理和@property的属性 目录 对内存管理的理解 Objective C内存管理方式 内存的管理 对象的所有权和内存管理原则 合理解决内存管理带来的问题 自动释放池 @property的属性 ...