Android图片缩放方法
安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法:
方法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图片缩放方法的更多相关文章
- Android 简单的图片缩放方法
很简单的一个图片缩放方法,注意要比例设置正确否则可能会内存溢出 相关问题 java.lang.IllegalArgumentException: bitmap size exceeds 32bits ...
- android图片压缩方法
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- android图片处理方法
Java代码 //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
- android图片处理方法(不断收集中)
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...
- Android 图片处理方法
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...
- android图片处理方法(转)
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...
- android图片缩放平移
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" ...
- Android 图片缩放
以下演示将一个ImageView的高度设置为两倍: 布局文件main.xml <?xml version="1.0" encoding="utf-8"?& ...
随机推荐
- hdu 1434 幸福列车
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1434 幸福列车 Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长 ...
- hdu 1316 How Many Fibs?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition ...
- Eclipse基金会
昨天Eclipse基金会庆祝其成立十周年.2004年2月的新闻稿宣布该非盈利组织的正式成立,由包括开发者.消费者和插件提供商在内的各独立团体组成的董事会,为Eclipse的长期发展负责. 基金会成立时 ...
- linux调整分区大小
查看一下当前分区情况 1 2 3 4 5 6 7 8 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mou ...
- INPC & RaizePropertyChanged in mvvmlight
INPC & RaizePropertyChanged in mvvmlight In WPF app, MvvM Framework, we binding the UIElement fr ...
- QtSpim实现MIPS指令的编写
QtSpim实现MIPS指令的编写 由于各种对齐问题,cnblogs的格式难以控制,故贴图片,谅解.
- PB中获取datawindow提交的sql语句
PB的群里边,有人问的到这个问题,查了一下,综合了两条回答,得到了答案 1.DW 控件的SQLpreview 事件里的sqlsyntax 参数即是 2.pb一般使用占位符优化SQL语句,也就是你看到的 ...
- C语言中结构体的初始化
直接上例子: struct point { int x; int y; int z; } //常规写法 struct point pt1 = {100, 300, 200}; //初始化个数少于实际个 ...
- Notes of the scrum meeting(12.5)
meeting time:18:00~18:30p.m.,December 5th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- nodejs笔记三--url处理、Query String;
URL--该模块包含用以 URL 解析的实用函数. 使用 require('url') 来调用该模块. 一.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看 ...