import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
public class ImagesTool
{
/**
* 根据ImageView的大小压缩图片
* @param path
* @param imageView
* @return
*/
public static Bitmap decodeSampledBitmapFromPath(String path,ImageView imageView)
{
Options options = new Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(path,options);
ImageSize imageSize=getImageViewSize(imageView); //获取图片大小,ImageSize是封装着ImageView大小的类
//计算采样率
options.inSampleSize=caculateInSampleSize(options,imageSize.width,imageSize.height);
options.inJustDecodeBounds=false;
Bitmap bitmap=BitmapFactory.decodeFile(path, options);
return bitmap;
}
//计算采样率
public static int caculateInSampleSize(Options options,ImageView imageView)
{
ImageSize imageSize=getImageViewSize(imageView);
int inSampleSize=caculateInSampleSize(options, imageSize.width,imageSize.height);
return inSampleSize;
}
/**
* 根据具体的大小要求解析图片
* @param path
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap decodeSampledBitmapFromPath(String path,int reqWidth, int reqHeight)
{
Options options = new Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(path,options);
//计算采样率
options.inSampleSize=caculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds=false;
Bitmap bitmap=BitmapFactory.decodeFile(path, options);
return bitmap;
}
//计算采样率
private static int caculateInSampleSize(Options options, int reqWidth, int reqHeight)
{
int width=options.outWidth; //原始图片宽
int height=options.outHeight; //原始图片高
int inSampleSize=1; //采样率
if(width>reqWidth || height>reqHeight) //原始的宽比目标宽大,或者原始高比目标高大
{
int widthRadio=Math.round(width *1.0f/reqWidth);
int heightRadio = Math.round(height * 1.0f / reqHeight);
inSampleSize = Math.max(widthRadio, heightRadio);
}
return inSampleSize;
}
//获取ImageView的大小
protected static ImageSize getImageViewSize(ImageView imageView)
{
ImageSize imageSize = new ImageSize();
DisplayMetrics metrics = imageView.getContext().getResources().getDisplayMetrics();
LayoutParams lp = imageView.getLayoutParams();
int width = imageView.getWidth();
if (width <= 0) {
width = lp.width;
}
if (width <= 0) {
width = imageView.getMaxWidth();
}
if (width <= 0) {
width = metrics.widthPixels;
}
int height = imageView.getHeight();
if (height <= 0) {
height = lp.height;
}
if (height <= 0) {
height = imageView.getMaxHeight();
}
if (height <= 0) {
height = metrics.heightPixels;
}
imageSize.width = width;
imageSize.height = height;
return imageSize;
}
//ImageView大小的封装类
private static class ImageSize
{
int width;
int height;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有