Android 之 Bitmap等比缩放

 // 等比缩放图片
        public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
           // 获得图片的宽高
           int width = bm.getWidth();
           int height = bm.getHeight();
           // 计算缩放比例
           float scaleWidth = ((float) newWidth) / width;
           float scaleHeight = ((float) newHeight) / height;
           // 取得想要缩放的matrix参数
           Matrix matrix = new Matrix();
           matrix.postScale(scaleWidth, scaleHeight);
           // 得到新的图片
           Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
            return newbm;
        }
 /** 
         * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
         */  
        public static int dip2px(Context context, float dpValue) {  
            final float scale = context.getResources().getDisplayMetrics().density;  
            return (int) (dpValue * scale + 0.5f);  
        } 

猜你喜欢

转载自blog.csdn.net/jky_yihuangxing/article/details/52605136
今日推荐