方法1:按固定比例进行缩放

在开发图片浏览器等 软件是,很多时候要显示图片的缩略图,而一般情况下,我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的 decodeFile方法,然后通过传递进去 BitmapFactory.Option类型的参数进行取缩略图,在Option中,属性值inSampleSize表示缩略图大小为原始图片大小的几 分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。

然而,如果我们想取固定大小的缩略图就比较困难了,比如,我们想将不同大小的图片去出来的缩略图高度都为200px,而且要保证图片不失真,那怎么 办?我们总不能将原始图片加载到内存中再进行缩放处理吧,要知道在移动开发中,内存是相当宝贵的,而且一张100K的图片,加载完所占用的内存何止 100K?

经过研究,发现,Options中有个属性inJustDecodeBounds,研究了一下,终于明白是什么意思了,SDK中的E文是这么说的

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

意思就是说如果该值设为true那么将不返回实际的bitmap不给其分配内存空间而里面只包括一些解码边界信息即图片大小信息,那么相应的方法也就出来 了,通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),然后就可以取图片了,这里要注意的是,inSampleSize 可能小于0,必须做判断。也就是说先将Options的属性inJustDecodeBounds设为true,先获取图片的基本大小信息数据(信息没有 保存在bitmap里面,而是保存在options里面),通过options.outHeight和 options. outWidth获取的大小信息以及自己想要到得图片大小计算出来缩放比例inSampleSize,然后紧接着将inJustDecodeBounds 设为false,就可以根据已经得到的缩放比例得到自己想要的图片缩放图了。

具体代码如下:

[java] FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01); 
 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
 
      options.inJustDecodeBounds = true; 
 
      Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空  
 
      options.inJustDecodeBounds = false; 
 
       //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
 
      int be = (int)(options.outHeight / (float)200); 
 
      if (be <= 0) 
 
          be = 1; 
 
      options.inSampleSize = be; 
 
      //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
 
      bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options); 
 
      int w = bitmap.getWidth(); 
 
      int h = bitmap.getHeight(); 
 
      System.out.println(w+"   "+h); 
 
      ImageView iv=new ImageView(this); 
 
      iv.setImageBitmap(bitmap); 
  FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01);

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空

options.inJustDecodeBounds = false;

//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

int be = (int)(options.outHeight / (float)200);

if (be <= 0)

be = 1;

options.inSampleSize = be;

//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options);

int w = bitmap.getWidth();

int h = bitmap.getHeight();

System.out.println(w+"   "+h);

ImageView iv=new ImageView(this);

iv.setImageBitmap(bitmap);

这样我们就可以读取较大的图片而不会内存溢出了。如果你想把压缩后的图片保存在Sdcard上的话就很简单了:

[java] File file=new File("/sdcard/feng.png"); 
 
try { 
 
    FileOutputStream out=new FileOutputStream(file); 
 
    if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){ 
 
        out.flush(); 
 
        out.close(); 
 
    } 
 
} catch (FileNotFoundException e) { 
 
    // TODO Auto-generated catch block  
 
    e.printStackTrace(); 
 
} catch (IOException e) { 
 
    // TODO Auto-generated catch block  
 
    e.printStackTrace(); 
 

File file=new File("/sdcard/feng.png");

try {

FileOutputStream out=new FileOutputStream(file);

if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){

out.flush();

out.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ok,这样就把图片保存在/sdcard/feng.png这个文件里面了,呵呵。

方法2:按长宽各自比例进行缩放

但是这里的缩放保存是按长宽比例的,下边也可以按固定大小缩放哦:

[java] int bmpWidth  = bitmap.getWidth();  
 
int bmpHeight  = bitmap.getHeight();  
 
//缩放图片的尺寸   
 
float scaleWidth  = (float) sWidth / bmpWidth;     //按固定大小缩放  sWidth 写多大就多大  
 
float scaleHeight = (float) sHeight / bmpHeight;  //  
 
Matrix matrix = new Matrix();  
 
matrix.postScale(scaleWidth, scaleHeight);//产生缩放后的Bitmap对象   
 
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);  
 
bitmap.recycle();  
 
Bitmap resizeBitmap = bitmap;  
 
//Bitmap to byte[]   
 
byte[] photoData = bitmap2Bytes(resizeBitmap);  
 
//save file   
 
String fileName = "/sdcard/test.jpg"; 
 
FileUtil.writeToFile(fileName, photoData);  
 
if(icon.getMinimumWidth() > 163 || icon.getMinimumHeight() > 163) 
 

 
    BitmapDrawable b = (BitmapDrawable)icon; 
 
    Bitmap bitmap = b.getBitmap(); 
 
    int bmpWidth  = bitmap.getWidth();  
 
    int bmpHeight  = bitmap.getHeight(); 
 
    //缩放图片的尺寸   
 
    float scaleWidth  = (float) sWidth / bmpWidth;     //按固定大小缩放  sWidth 写多大就多大  
 
    float scaleHeight = (float) sHeight / bmpHeight;  //  
 
    Matrix matrix = new Matrix();  
 
    matrix.postScale(scaleWidth, scaleHeight);//产生缩放后的Bitmap对象   
 
    Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);  
 
    bitmap.recycle(); 
 
    Drawable d =new BitmapDrawable(resizeBitmap); 
 
    icon = d; 
 

int bmpWidth  = bitmap.getWidth();

int bmpHeight  = bitmap.getHeight();

//缩放图片的尺寸

float scaleWidth  = (float) sWidth / bmpWidth;     //按固定大小缩放  sWidth 写多大就多大

float scaleHeight = (float) sHeight / bmpHeight;  //

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);//产生缩放后的Bitmap对象

Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);

bitmap.recycle();

Bitmap resizeBitmap = bitmap;

//Bitmap to byte[]

byte[] photoData = bitmap2Bytes(resizeBitmap);

//save file

String fileName = "/sdcard/test.jpg";

FileUtil.writeToFile(fileName, photoData);

if(icon.getMinimumWidth() > 163 || icon.getMinimumHeight() > 163)

{

BitmapDrawable b = (BitmapDrawable)icon;

Bitmap bitmap = b.getBitmap();

int bmpWidth  = bitmap.getWidth();

int bmpHeight  = bitmap.getHeight();

//缩放图片的尺寸

float scaleWidth  = (float) sWidth / bmpWidth;     //按固定大小缩放  sWidth 写多大就多大

float scaleHeight = (float) sHeight / bmpHeight;  //

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);//产生缩放后的Bitmap对象

Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);

bitmap.recycle();

Drawable d =new BitmapDrawable(resizeBitmap);

icon = d;

}
 
然后我在做的时候遇到一个问题,就是图片的大小确实已经改变,但是图片还是被拉伸了,在用imageView. setBackgroundDrawable的时候有的图片还是会失真,事实上,如果不用上面方法进行图片的缩放,仅设置GridView或者 ListView里面的LayoutParams也可以达到图片相应的图片大小,但是setBackgroundDrawable有个缺陷,会拉伸图片, 解决方法是用imageView.setImageDrawable就可以了,实现图片等比例缩放,问题解决。

转载于:https://www.cnblogs.com/Free-Thinker/p/3546385.html

Android图片缩放方法相关推荐

  1. android的图片缩放,Android图片缩放总结及比较

    在Android中对大图片进行缩放真的很不尽如人意,不知道是不是我的方法不对.下面我列出3种对图片缩放的方法,并给出相应速度.请高人指教. 第一种是BitmapFactory和BitmapFactor ...

  2. android图片处理方法(不断收集中)

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  3. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 4 5 ...

  4. j2me图片缩放方法

    今天在网上看到几个在j2me中实现图片缩放的函数,很不错,记录下来. 注:src为原始图片,destW为修改后的宽度,destH为修改后的高度 1.方法一 public static Image re ...

  5. android 图片压缩方法分析

    demo 参考会放在末尾 1 质量压缩 先看截图吧,手机截图可能有点不优雅 通过压缩前后数据可以知道, 质量压缩之后不会减少图片的像素,它是在保持像素的前提下改变图片的位深及透明度,来达到压缩图片的目 ...

  6. android图片叠加方法

    Android有时候会遇到需要两张图片叠加起来的效果,现记录如下: /* *此方法分别传入两个bitmap对象,一个为底图(背景图background), *另一个则位于其上面(前景图foregrou ...

  7. android图片缩放模式,Android使用缩放动画放大你的图片

    注:本篇文章是对官方开发文档的翻译,加上自己的理解和分析. 地址:https://developer.android.com/training/animation/zoom 本篇文章所实现的功能: 触 ...

  8. android 图片缩放算法,Android大图加载,缩放,滑动浏览--SubsamplingScaleImageView 源码分析大图加载...

    **************这个开源项目有点大的,也不知道几篇能写完,先根据功能点分析解读********************* 1.写在前面 图片浏览的坑不少,大图加载导致内存溢出的情况相信每个 ...

  9. 三种Android图片压缩方法 压缩到指定大小

    一.图片质量压缩 /*** 质量压缩方法* @param image* @return*/ public static Bitmap compressImage(Bitmap image) {Byte ...

最新文章

  1. 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构
  2. 【Kotlin】抽象类 ( 声明 | 抽象类成员 | 抽象类继承 | 抽象方法覆盖 | 抽象方法实现 )
  3. 计算机c盘隐藏了怎么办,win7怎么隐藏c盘 win7c盘被隐藏了怎么解除
  4. 【区块链Go语言实现】Part 1:区块链基本原型
  5. mysql安装条款_mysql 安装注意
  6. 排序算法之---堆排序(很重要的一个结构,新手入门必备)
  7. 华为云GaussDB专家走进课堂,跟莘莘学子聊聊数据库
  8. 源数据怎么排查重复MySQL_面试官:在使用mysql数据库时,遇到重复数据怎么处理?...
  9. ios保存录制好的视频 图片 到相簿
  10. 效果过度transition:all
  11. 用python画一只皮卡丘_画皮卡丘怎么执行不了
  12. python计算银行余额_Python:将银行扣费信息整理成账单
  13. 求教务排课系统(eclipes和MSQL)!
  14. qq远程听到计算机声音,QQ2017远程播放视频没有声音_软件应用_电脑知识学习_培训之家...
  15. 更换持续集成工具,从 Travis 到 Github Actions
  16. Python机器学习04——惩罚回归
  17. 圣思园——JAVA SE Lesson 1
  18. Poly-YOLO keras代码记录
  19. 如何使用激活工具Microsoft Toolkit
  20. centos环境下测试网速

热门文章

  1. 通俗易懂:说说 Python 里的线程安全、原子操作
  2. window下启动Redis闪退问题解决
  3. angular js 使用pdf.js_胶水(框架) Stencil.js
  4. 创建django项目,8月版本
  5. 对于vue的评价:没事情做可以学
  6. Mozilla的 MDN 学习区Web开发
  7. linux7配置iptables配置转发,Centos7安装iptables及配置
  8. php 加tab键,php 生成Tab键或逗号分隔的CSV
  9. 充一次电使用一年的手机_超级手机电池:充一次电用一年多?
  10. 【文献阅读】Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning Algorithms