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. c++训练营--重载

    // demo1.cpp : 定义控制台应用程序的入口点. //通过此例程了解重载 #include "stdafx.h" #include <iostream> us ...

  2. linux下oracle11g R2的启动与关闭监听、数据库

    su - oracle           切换到oracle账户 lsnrctl start          启动监听 sqlplus /nolog     登陆sqlplus conn /as  ...

  3. char.js专门用来做数据统计图

    <canvas id="cashback" width="930" height="460"></canvas>&l ...

  4. android开发环境 eclipse + android sdk配置笔记

    本开发环境为:eclipse + android sdk,步骤说明的顺序,没有特别要求,看个人爱好了 步骤说明: 1.安装eclipse 2.配置jdk 3.安装android sdk 4.安装ADT ...

  5. 简单方便统一封装的傻瓜式GET/POST库AliasNet正式公布~开源喽~

    在进行网页自动化时我们做得最多的工作就是不停的往某个URL GET/POST数据并得到相应的Response,通过分析Response的结果再进行下一步操作,通过网页自动化我们可以做很多工作,比如去某 ...

  6. onConfigurationChanged is not called&& 翻转屏幕不执行onConfigurationChanged方法&&onConfigurationChanged不执行

    我总结出一句话: 如果target sdk>=13,必须使用如下方式声明activity:android:configChanges="orientation|screenSize&q ...

  7. [Ramda] Filter, Reject and Partition

    We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...

  8. Swift2.0 中的String(二):基本操作

    Swift中的字符串,第二篇,基本操作.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一) ...

  9. Eclipse中R文件不能自动生成

       R文件不能自动生成主要是因为编译有错误,这时你想什么办法都是没有用的,clean, fix properties,都不是从根上解决问题.    R文件主要是自动生成资源文件的id的,里边静态子类 ...

  10. 云服务器 ECS Linux 系统中常见的日志文件介绍

    云服务器 ECS Linux 系统中,日志文件是非常重要的文件,它们记录了很多系统中重要的事.Linux 系统中常见日志文件概述如下: /var/log/cron可以在 cron 文件中检查 cron ...