四步实现根据View大小压缩加载Bitmap
2016-11-15 00:32
302 查看
根据View大小压缩加载Bitmap:
1.将BitmapFactory.Options的inJustDecodeBounds设置为:true,并加载图片
2.从BitmapFactory.Options取出图片的原始尺寸信息,它们应对与outWidth和outHeigth参数
3.根据采样率的规则并结合目标View的所需大小计算出采样率inSampleSize
4.将BitmapFactory.Options的inJustDecodeBounds参数设置成false,然后重新加载图片
*注意:这里inJustDecodeBounds设置为:true时,BitmapFactory并不会真正的去加载Bitmap,只是去解析Bitmap的宽高信息,所以这个操作是轻量级的
根据上面4步流程来实现,就产生了如下代码:
package com.example.administrator.myapplication.utils;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
/**
* Created by Administrator on 2016/11/14 0014.
*/
public class BitmapUtil {
public static Bitmap decodeSampledbitmapFromResource(Resources resources, int resID, int reqWidth, int reqHeight) {
final BitmapFactory.Options option = new BitmapFactory.Options();
//设置inJustDecodeBounds为:ture,预先加载Bitmap的宽高参数
option.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resID, option);
//计算图片的采样率
option.inSampleSize = calcuteInSapmleSize(option, reqWidth, reqHeight);
//根据图片采样率加载图片
option.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resID, option);
}
private static int calcuteInSapmleSize(BitmapFactory.Options option, int reqWidth, int reqHeight) {
final int height = option.outHeight;
final int width = option.outWidth;
Log.e("info", "width======" + width+"\n"+"height======"+height);
Log.e("info", "reqWidth======" + reqWidth+"\n"+"reqHeight======"+reqHeight);
int inSample = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSample) >= reqHeight && (halfWidth / inSample) >= reqWidth) {
inSample *= 2;
}
}
Log.e("info", "inSample======" + inSample);
return inSample;
}
}
1.将BitmapFactory.Options的inJustDecodeBounds设置为:true,并加载图片
2.从BitmapFactory.Options取出图片的原始尺寸信息,它们应对与outWidth和outHeigth参数
3.根据采样率的规则并结合目标View的所需大小计算出采样率inSampleSize
4.将BitmapFactory.Options的inJustDecodeBounds参数设置成false,然后重新加载图片
*注意:这里inJustDecodeBounds设置为:true时,BitmapFactory并不会真正的去加载Bitmap,只是去解析Bitmap的宽高信息,所以这个操作是轻量级的
根据上面4步流程来实现,就产生了如下代码:
package com.example.administrator.myapplication.utils;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
/**
* Created by Administrator on 2016/11/14 0014.
*/
public class BitmapUtil {
public static Bitmap decodeSampledbitmapFromResource(Resources resources, int resID, int reqWidth, int reqHeight) {
final BitmapFactory.Options option = new BitmapFactory.Options();
//设置inJustDecodeBounds为:ture,预先加载Bitmap的宽高参数
option.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resID, option);
//计算图片的采样率
option.inSampleSize = calcuteInSapmleSize(option, reqWidth, reqHeight);
//根据图片采样率加载图片
option.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resID, option);
}
private static int calcuteInSapmleSize(BitmapFactory.Options option, int reqWidth, int reqHeight) {
final int height = option.outHeight;
final int width = option.outWidth;
Log.e("info", "width======" + width+"\n"+"height======"+height);
Log.e("info", "reqWidth======" + reqWidth+"\n"+"reqHeight======"+reqHeight);
int inSample = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSample) >= reqHeight && (halfWidth / inSample) >= reqWidth) {
inSample *= 2;
}
}
Log.e("info", "inSample======" + inSample);
return inSample;
}
}
具体使用方法:
mImageView.setImageBitmap(BitmapUtil.decodeSampledbitmapFromResource(this.getResources(), R.mipmap.ic_launcher, 70, 70));
相关文章推荐
- 根据ImageView的大小来压缩Bitmap,避免OOM
- 根据ImageView的大小来压缩Bitmap,避免OOM
- Android-根据ImageView的大小来压缩Bitmap,避免OOM
- 根据ImageView的大小来压缩Bitmap,避免OOM
- (转)根据ImageView的大小来压缩Bitmap,避免OOM
- 根据String以及textView所设定的字体大小来计算所需要行数,实现展开和收起的效果。
- 根据String以及textView所设定的字体大小来计算所需要行数,实现展开和收起的效果。
- imageView加载大图,bitmap压缩图片质量RGB_565
- 重写PageStatePersister属性 自定义压缩方法来减小ViewState大小来优化页面加载时间
- 一种根据ImageView的大小缩放Bitmap的方法
- Android腾讯微博客户端开发五:利用FootView实现ListView滑动动态加载实现分页
- 根据view生成Bitmap
- ios 画图 绘图的实现(自由批注中视图的subview的实现)bitmap Quartz 2D 缓存位图 自由批注功能的实现 如何实现在view上自由绘图 提供源码
- Android中ViewFlipper实现动态加载view
- bitmap 将图片压缩到指定的大小
- Android图片大小调整动态实现方法(bitmap)
- Android中ViewFlipper实现动态加载view
- Android腾讯微博客户端开发5:利用FootView实现ListView滑动动态加载实现分页
- Web中模态对话框加载后根据加载内容动态改变其大小并使其居中
- UITableView实现加载更多功能