图片的二次采样就是解决图片过大而造成的加载时间过长。所以将图片进行压缩。
1.首先通过网络工具类下在图片数据,将数据转化为数组;
2.对图片进行压缩处理。
下列是主要代码;
// 获取图片的测量工具;
DisplayMetrics metrics = getResources().getDisplayMetrics();
// 获取图片的分辨率的大小;
int pixWidth = metrics.widthPixels;
int pixHidth = metrics.heightPixels;
// 获取图片采样队象,画片的优先选择权
BitmapFactory.Options options = new BitmapFactory.Options();
// 通过采样对象,对图片的边界进行处理
options.inJustDecodeBounds = true;
// 将图片进行处理之后的对象,放到option中;
BitmapFactory.decodeByteArray(by, 0, by.length, options);
// 图片的原始宽高;
int sWidth = options.outWidth;
int sHeight = options.outHeight;
// 声名一个图片比例;
int num = 8;
if (sWidth / pixWidth > num || sHeight / pixHidth > num) {
if (sWidth / pixWidth > sHeight / pixHidth) {
num = sWidth / pixWidth;
} else {
num = sHeight / pixHidth;
}
}
// 获取图片的采样比例
options.inSampleSize = num;
options.inJustDecodeBounds = false;
Bitmap bit = BitmapFactory.decodeByteArray(by, 0, by.length, options);
return bit;
}
DisplayMetrics metrics = getResources().getDisplayMetrics();
// 获取图片的分辨率的大小;
int pixWidth = metrics.widthPixels;
int pixHidth = metrics.heightPixels;
// 获取图片采样队象,画片的优先选择权
BitmapFactory.Options options = new BitmapFactory.Options();
// 通过采样对象,对图片的边界进行处理
options.inJustDecodeBounds = true;
// 将图片进行处理之后的对象,放到option中;
BitmapFactory.decodeByteArray(by, 0, by.length, options);
// 图片的原始宽高;
int sWidth = options.outWidth;
int sHeight = options.outHeight;
// 声名一个图片比例;
int num = 8;
if (sWidth / pixWidth > num || sHeight / pixHidth > num) {
if (sWidth / pixWidth > sHeight / pixHidth) {
num = sWidth / pixWidth;
} else {
num = sHeight / pixHidth;
}
}
// 获取图片的采样比例
options.inSampleSize = num;
options.inJustDecodeBounds = false;
Bitmap bit = BitmapFactory.decodeByteArray(by, 0, by.length, options);
return bit;
}