Android 压缩图片

版权声明:https://blog.csdn.net/qq_32425789/article/details/85052201

 直接上图

原图信息:


 

压缩后:

 

贴代码:

扫描二维码关注公众号,回复: 5522567 查看本文章
public class imageviewtool {
    private static final String TAG = "MainActivity";

    public static Bitmap decodeSampledBitmapResesouce(Resources res,int resId,int reqWidth,int reqHeight){
        final BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(res,resId,options);
        options.inSampleSize=ImageSize(options,reqWidth,reqHeight);
        options.inJustDecodeBounds=false;
        return BitmapFactory.decodeResource(res,resId,options);
    }

    public static int ImageSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
        final int height=options.outHeight;
        final int width=options.outWidth;
        int inSampleSize=1;

        if(height>reqHeight ||width>reqWidth){
            final int hanflHeight=height/2;
            final int hanflWidth=width/2;

            while (hanflHeight/inSampleSize>=reqHeight&&hanflWidth/inSampleSize>=reqWidth){
                inSampleSize*=2;
            }
        }
        return inSampleSize;
    }
}

虽然图片压缩网上一找一大把,不过还是有些要注意的地方: options.inJustDecodeBounds=true;  该怎么说呢, 只是取巧了, 重新改变了 宽高,且大小为原来的1/2.

猜你喜欢

转载自blog.csdn.net/qq_32425789/article/details/85052201