android 图片压缩

来源:互联网 发布:淘宝宝贝怎么推广 编辑:程序博客网 时间:2024/06/05 10:52
/**
 * 通过view的高和宽压缩
 * 
 * @param pathName
 * @param vWidth
 *                view的宽,0表示不压缩
 * @param vHeight
 *                view的高,0表示不压缩
 * @return
 */
public Bitmap compressBitmap(String pathName, int vWidth, int vHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
if (vHeight > 0 && vWidth > 0) {
/**
 * 计算图片的宽高
 */
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, opts);
/**
 * 压缩比例,如果小1则=1,表示不压缩 大于1,则返回原大小的1/n的图片
 */
opts.inSampleSize = Math.min(opts.outWidth / vWidth, opts.outHeight / vHeight);
/**
 * 表示没有透明度
 */
opts.inPreferredConfig = Config.RGB_565;
opts.inJustDecodeBounds = false;
// opts.inPurgeable = true;
// opts.inInputShareable = true;

}
return BitmapFactory.decodeFile(pathName, opts);
}
0 0