• ANDROID笔记:根据长宽实现图片压缩


       /**
         * 得到一定高度的图片
         * 
         * @param height
         * @param imagePath
         * @return
         */
        public Bitmap getImageByHeight(float height, String imagePath) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            // 获取这个图片的宽和高
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
            options.inJustDecodeBounds = false;
            // 计算缩放比
            int be = (int) (options.outHeight / height);
            if (be <= 0)
                be = 1;
            options.inSampleSize = be;
            // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(imagePath, options);
            return bitmap;
    
        }
    
        /**
         * 得到一定宽度的图片
         * 
         * @param width
         * @param imagePath
         * @return
         */
        public Bitmap getImageByWidth(float width, String imagePath) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            // 获取这个图片的宽和高
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
            options.inJustDecodeBounds = false;
            // 计算缩放比
            int be = (int) (options.outWidth / width);
            if (be <= 0)
                be = 1;
            options.inSampleSize = be;
            // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(imagePath, options);
            return bitmap;
    
        }
        /**
         * 得到一定宽度或高度的图片
         * @param width 宽度
         * @param height 高度
         * @param srcPath 图片路径
         * @return Bitmap
         */
        public static Bitmap getBitmap(String srcPath,float width,float height) {  
            BitmapFactory.Options newOpts = new BitmapFactory.Options();  
            //开始读入图片,此时把options.inJustDecodeBounds 设回true了  
            newOpts.inJustDecodeBounds = true;  
            Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空  
              
            newOpts.inJustDecodeBounds = false;  
            int w = newOpts.outWidth;  
            int h = newOpts.outHeight;  
           
            //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
            int be = 1;//be=1表示不缩放  
            if (w > h && w > width) {//如果宽度大的话根据宽度固定大小缩放  
                be = (int) (newOpts.outWidth / width);  
            } else if (w < h && h > height) {//如果高度高的话根据高度固定大小缩放  
                be = (int) (newOpts.outHeight / height);  
            }  
            if (be <= 0)  
                be = 1;  
            newOpts.inSampleSize = be;//设置缩放比例  
            //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
            bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
            return bitmap;
        }
  • 相关阅读:
    快使用阿里云的maven仓库
    谈谈对MVC、MVP和MVVM的理解
    [个人项目] 使用 Vuejs 完成的音乐播放器
    手把手教你封装 Vue 组件并使用 NPM 发布
    Chrome 的 Material Design Refresh UI初探
    Vue图片懒加载插件
    手淘的移动端适配方案flexible
    css 实现元素长宽等比缩放
    css 中 stick footer 布局实现
    页面滚动插件 better-scroll 的用法
  • 原文地址:https://www.cnblogs.com/afluy/p/3385306.html
Copyright © 2020-2023  润新知