图片缩放处理

步骤一、得到屏幕的宽高

//得到屏幕的宽高(方法一、使用场景Android3.2) Point point = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(point);
int winW = point.x;
int winH = point.y;
//得到屏幕的宽高(方法二,通用) winW = display.getWidth();
winH = display.getHeight();
步骤二、得到图片的宽高

//得到图片的宽高 BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//设置不将图片加载到内存,只是得到宽和高的属性 BitmapFactory.decodeFile(path+"/bit.bmp",options);
int bitW = options.outWidth;
int bitH = options.outHeight;
步骤三、得到缩放比例进行缩放并显示

//得到缩放比例 int scale = Math.max(bitW/winW,bitH/winH);
options.inSampleSize = scale;//设置缩放比例,将图片缩放到原图的scale分之一 options.inJustDecodeBounds = false;//设置将图片加载到您内存 Bitmap bitmap = BitmapFactory.decodeFile(path+"/bit.bmp",options);
iv.setImageBitmap(bitmap);

如果得到的是一个流则:

private void scaleImg1(Uri uri){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //得到屏幕的宽高  int winW = getWindowManager().getDefaultDisplay().getWidth();
        int winH = getWindowManager().getDefaultDisplay().getHeight();
        //得到图片的宽高  options.inJustDecodeBounds = true;
        InputStream is = getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(is,null,options);
        int bitW = options.outWidth;
        int bitH = options.outHeight;
        //得到缩放比例  int scale = Math.max(bitW/winW,bitH/winH);
        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
        //显示图片  is = getContentResolver().openInputStream(uri);//因为流is读到了末尾的位置,在这里要重新生成一下流,这样才能将图片显示出来  Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
        iv.setImageBitmap(bitmap);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

你可能感兴趣的:(图片缩放处理)