美文网首页
工具类——Bitmap压缩

工具类——Bitmap压缩

作者: 楷桐 | 来源:发表于2018-07-24 11:15 被阅读94次
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Point;
    
    /**
     * Created by zkt on 2018-7-24.
     * Description: Bitmap压缩工具类
     */
    
    public class PictureUtils {
    
        public static Bitmap getScaledBitmap(String path, Activity activity) {
            Point size = new Point();
            activity.getWindowManager().getDefaultDisplay()
                    .getSize(size);
            return getScaledBitmap(path, size.x, size.y);
        }
    
        private static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
            // Read in the dimensions of the image on disk
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
            float srcWidth = options.outWidth;
            float srcHeight = options.outHeight;
            // Figure out how much to scale down by
            int inSampleSize = 1;
            if (srcHeight > destHeight || srcWidth > destWidth) {
                float heightScale = srcHeight / destHeight;
                float widthScale = srcWidth / destWidth;
                inSampleSize = Math.round(heightScale > widthScale ? heightScale :
                        widthScale);
            }
            options = new BitmapFactory.Options();
            options.inSampleSize = inSampleSize;
            // Read in and create final bitmap
            return BitmapFactory.decodeFile(path, options);
        }
    }
    

    相关文章

      网友评论

          本文标题:工具类——Bitmap压缩

          本文链接:https://www.haomeiwen.com/subject/xnsemftx.html