安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法:

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

  在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们取图片的缩略图,取缩略图的方法我们可以使用BitmapFactory的decodeFile方法,然后通过传递进去 BitmapFactory.Option类型的参数进行取缩略图,在Option中,属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。但是,如果我们想取固定大小的缩略图就比较困难了,比如,我们想将不同大小的图片取出来的缩略图高度都为200px,同时要保证图片不失真,那我们该怎么办?总不能将原始图片加载到内存中再进行缩放处理吧,要知道在移动开发中,内存是相当宝贵的,一张100K的图片,加载完所占用的内存何止是100K。

  经过阅读文档发现,Options中有个属性inJustDecodeBounds,文档中的是这么说的:

  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,就可以根据已经得到的缩放比例得到自己想要的图片缩放图了。

实现代码如下:

BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  // 记得把assets目录下的图片拷贝到SD卡中
  // 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空
  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg", options);
  // 计算缩放比例,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  int scale = (int) (options.outHeight / (float) 200);
  // 因为结果为int型,如果相除后值为0.n,则最终结果将是0
  if (scale <= 0)
  {
  scale = 1;
  }
  System.out.println("Scale=" + scale);
  options.inSampleSize = scale;
  options.inJustDecodeBounds = false;
  // 重新读入图片,注意此时已经把options.inJustDecodeBounds设回false
  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg", options);
  int width = mBitmap.getWidth();
  int height = mBitmap.getHeight();
  System.out.println(width + " " + height);
  ImageView image = (ImageView) findViewById(R.id.image);
  image.setImageBitmap(mBitmap);
  这样我们就可以读取较大的图片而不会出现内存溢出问题了。
  如果你想把压缩后的图片保存在sdcard上的话,通过如下代码就可以了:
  File file = new File("/sdcard/ruoshui.png");
  try
  {
  //记得添加sdcard读写权限
  FileOutputStream out = new FileOutputStream(file);
  if (mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out))
  {
  out.flush();
  out.close();
  Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
  }
  }catch (Exception e)
  {
  e.printStackTrace();
  Toast.makeText(MainActivity.this, "保存失败!", Toast.LENGTH_LONG).show();
  }

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

上面的方法缩放保存是按长宽比例的,我们当然也可以按固定大小进行缩放:

BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = false;
  // 记得把assets目录下的图片拷贝到SD卡中
  // 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空
  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg", options);
  int bmpWidth = mBitmap.getWidth();
  int bmpHeight = mBitmap.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(mBitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);
  mBitmap.recycle();
  ImageView image = (ImageView) findViewById(R.id.image);
  image.setImageBitmap(resizeBitmap);

在用imageView. setBackgroundDrawable的时候有的图片会失真,事实上,如果不用上面方法进行图片的缩放,仅设置GridView或者ListView里面的LayoutParams也可以达到图片相应的图片大小,但是setBackgroundDrawable有个缺陷,会拉伸图片,解决方法是用imageView.setImageDrawable就可以了,实现图片等比例缩放

Android图片缩放方法的更多相关文章

  1. Android 简单的图片缩放方法

    很简单的一个图片缩放方法,注意要比例设置正确否则可能会内存溢出 相关问题 java.lang.IllegalArgumentException: bitmap size exceeds 32bits ...

  2. android图片压缩方法

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  3. android图片处理方法

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

  4. Android图片压缩方法总结

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

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

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

  6. Android 图片处理方法

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

  7. android图片处理方法(转)

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

  8. android图片缩放平移

    <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" ...

  9. Android 图片缩放

    以下演示将一个ImageView的高度设置为两倍: 布局文件main.xml <?xml version="1.0" encoding="utf-8"?& ...

随机推荐

  1. Repair the database using DBCC CHECKDB

    So now if you want to place AdventureWorks2008R2 sample database in a single-user mode, then write t ...

  2. jquery方法回到顶部代码

    <style> /*默认样式,主要是position:fixed实现屏幕绝对定位*/ #gotoTop{display:none;position:fixed;top:75%;left:5 ...

  3. JSON数组操作

    在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...

  4. C++string的使用

    在这里总结一下string的用法 String是可变长字符串,使用的时候要包含string头文件. 要想使用标准C++中string类,必须要包含 #include <string>// ...

  5. SQLiteAPI函数详解

    使用的过程根据使用的函数大致分为如下几个过程: sqlite3_open() sqlite3_prepare() sqlite3_step() sqlite3_column() sqlite3_fin ...

  6. Android -- TouchEvent的分发和截获方式

    Android系统中的每个ViewGroup的子类都具有下面三个和TouchEvent处理密切相关的方法: public boolean dispatchTouchEvent(MotionEvent ...

  7. 【 Regular Expression Matching 】cpp

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  8. android sdk manager下载慢可以使用代理信息

    mirrors.neusoft.edu.cn  80

  9. python 网络编程-TCP/UDP

    摘抄自:廖雪峰的官方网站:http://www.liaoxuefeng.com/ TCP客户端和服务器端代码: #coding=utf-8 #客户端程序TCP 连接 import socket s=s ...

  10. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors

    题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...