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上的更多相关文章

  1. [Android] 拍照、截图、保存并显示在ImageView控件中

    近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法.     PS:作者购买 ...

  2. Android系统Bitmap内存分配原理与优化

    一.前言 笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起.谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素--图片,如果对图 ...

  3. Android笔记--Bitmap(三) 针对不用Android版本的位图管理

    Bitmap(三) | Android不同版本的相应操作 在不同的Android版本中.位图的存储方式是不同的. 1.小于等于 Android 2.2 (API level 8) 垃圾收集器回收内存时 ...

  4. int android.graphics.Bitmap.getRowBytes()

    int android.graphics.Bitmap.getRowBytes() Return the number of bytes between rows in the bitmap's pi ...

  5. Android处理Bitmap的一些方法

    http://www.it165.net/pro/html/201305/5795.html # 文件与Bitmap间的方法 1. 从文件载入Bitmap 01./** 02.* @brief 从文件 ...

  6. Android中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  7. Android笔记——Bitmap自动取色(纯搬运)

    2015/6/12更新:发现一个更好的,带demo https://github.com/MichaelEvans/ColorArt 说明: 这个是一个老外写的自动自动从bitmap中取主色与第二主色 ...

  8. Android中bitmap的相关处理

    加载大图片 Options options=new Options(); options.inJustDecodeBounds=true;//不加载图片,只加载文件信息 //加载图片,获取到配置信息 ...

  9. android 缓存Bitmap - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...

随机推荐

  1. 自定义一个可以使用foreach语句进行迭代的类(IEnumerable)

    在c#中,凡是实现了IEnumerable接口的数据类型都可以用foreach语句进行迭代访问.所以,我们要定义一个可以使用foreach进行迭代访问的类,就必须要实现IEnumerable接口. / ...

  2. GEOS库 介绍 (转)

    http://wiki.woodpecker.org.cn/moin/lilin/geos-introduce 介绍 GEOS是一个集合形状的拓扑关系操作实用库(可能这么说不太准确),简单得说,就是判 ...

  3. 通达OA 公共文件柜二次开发添加管理信息(图文)

    公共文件柜当内容较多时,管理起来非常easy乱,特别是当有多个名字相近的目录时.这里通过简单的开发添加了两个管理信息,能够通过添加备注的方式加以区分. watermark/2/text/aHR0cDo ...

  4. 微信公共服务平台开发(.Net 的实现)9-------处理二维码

    今天我们来共同学习一下微信公共服务平台中一个重要内容---二维码扫描.众所周知二维码目前应用范围很广,在这里不再叙述背景了,但是值得一提的是目前大家手机上面应用的二维码扫描工具是支持的都是QR码和PD ...

  5. 黑客破译android开发代码真就那么简单?

    很多程序员辛辛苦苦开发出的android开发代码,很容易就被黑客翻译了. Google似乎也发现了这个问题,从SDK2.3开始我们可以看到在android-sdk-windows\tools\下面多了 ...

  6. 通过输入方式在Android上进行微博OAuth登录

    在微博认证方式里,基本的OAuth认证是必须要调整到跳转到第三方页面上进行授权的,例如下面的例子:     1.从http://open.weibo.com/wiki/index.php/SDK#An ...

  7. 表ADT

    表一般不用简单数组来实现,通常将其实现为链表.在链表中要不要使用表头则属于个人兴趣问题.在下面的例程中我们都使用表头. 按照C的约定,作为类型的List(表)和Position(位置)以及函数的原型都 ...

  8. 配置apache虚拟主机的实例总结

    如何实现apache虚拟主机配置. 1.基于ip地址的虚拟主机Listen 80<VirtualHost 172.20.30.40> DocumentRoot /home/httpd/ht ...

  9. 关于QStringRef

      QString 为字符串操作提供了各种成员比如mid().left().right().它们都创建会一个新的字符串,因此有一个对在已存在QString的malloc和深拷贝. 与此相反,QStri ...

  10. IDEA社区版运行并发布web项目

    IDEA社区版相对收费版少了很多功能,其中包括tomcat等web服务器的支持.网上大部分的IDEA web应用发布教程都是基于收费版的,社区版并没有这么直接的图形化工具可以运行或发布web应用.幸运 ...