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. cocos2dx 网络编程(CCHttpRequest和CURL两个方式)

    转自:http://blog.csdn.net/sg619262284/article/details/20144087 在使用之前需要设置一些参数:参考:http://blog.csdn.net/w ...

  2. 前向否定界定符 python正则表达式不匹配某个字符串 以及无捕获组和命名组(转)

    [编辑] 无捕获组和命名组 精心设计的 REs 也许会用很多组,既可以捕获感兴趣的子串,又可以分组和结构化 RE 本身.在复杂的 REs 里,追踪组号变得困难.有两个功能可以对这个问题有所帮助.它们也 ...

  3. 你应该知道的JavaScript中NaN的秘密

    NaN,不是一个数字,是一种特殊的值来代表不可表示的值,使用typeof或其他任何与之比较的处理方式,‘NaN’则会引起一些混乱, 一些操作会导致NaN值的产生.这里有些例子: Math.sqrt(- ...

  4. 网站性能扩展案例:每天30-50亿请求,300K QPS是如何炼成的

    Reduce Data广告服务网站(http://reducedata.com)如何扩展到每天300K QPS请求?分享经验如下: 1. 为大规模而设计,广告服务平台从一开始增长就很惊人,因此,系统开 ...

  5. [AngularJS] Best Practise - Controller

    ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...

  6. iOS开发——实用OC篇&多种定时器详细介绍

    多种定时器详细介绍   在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 然而,在iOS中有很多方法完成以上的任务,到底有 ...

  7. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  8. Matlab程序怎样打包

    本人安装的版本号是MATLAB(R2010b) 打包过程例如以下: MATLAB命令窗体输入deploytool,打开一个Eeployment Project的窗体: 1.在Name输入你想要打包后的 ...

  9. The LMAX disruptor Architecture--转载

    原文地址: LMAX is a new retail financial trading platform. As a result it has to process many trades wit ...

  10. 如何制作gif动画,丰富自己的博客?

    不久前在博客园上看到有个哥们发表了一篇博客,其中使用了大量的动态的gif动画,感觉这种方法对于丰富博客内容非常有帮助,然后在网上搜索了一些关于制作gif动画的资料.我的方法不一定好,在这里仅作为抛砖引 ...