主要的代码如下:

     BitmapFactory.Options options = new BitmapFactory.Options();        //图片解析配置        options.inJustDecodeBounds = true;        //获取图片的属性并赋予options        BitmapFactory.decodeResource(getResources(), R.drawable.f1, options);        //获得图片实际宽高        int imgWidth = options.outWidth;        int imgHeight = options.outHeight;        System.out.println("outWidth = " + imgWidth);        System.out.println("outHeight = " + imgHeight);        //获取屏幕大小        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);        int windowwidth = windowManager.getDefaultDisplay().getWidth();        int windowheight = windowManager.getDefaultDisplay().getHeight();        System.out.println("width = " + windowwidth);        System.out.println("height = " + windowheight);        //计算缩放        int scale = 1;        int scaleX = imgWidth/windowwidth;        int scaleY = imgHeight/windowheight;        if(scaleX>1 && scaleX>scaleY) {            scale = scaleX;        }        if(scaleY>1 && scaleY>scaleX) {            scale = scaleY;        }        System.out.println("scale = " + scale);        //真的解析图片        options.inJustDecodeBounds = false;        options.inSampleSize = scale;        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f1, options);        imageView.setImageBitmap(bitmap);

附(计算inSampleSize的工具方法):

   public static int calculateInSampleSize(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 heightRatio = Math.round((float) height                    / (float) reqHeight);            final int widthRatio = Math.round((float) width / (float) reqWidth);            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;        }        return inSampleSize;    }

更多相关文章

  1. android 判断 网络 类型
  2. android 单击 切换图片 --- 注意图片大小
  3. [置顶] Android(安卓)通过经纬度获取地理位置信息
  4. android设置一个图片为全屏大小
  5. Android(安卓)调用系统相机拍照的返回结果
  6. Android遍历某个文件夹的图片并实现滑动查看的的Gallery
  7. android sdcard文件存储 + 媒体库更新方法
  8. android之sax解析xml文件
  9. android中调用相册里面的图片并返回

随机推荐

  1. Android Studio如何使用快捷键生成get,set
  2. android捕获Home键的方法
  3. Android分别使用HTTP协议和TCP协议实现上
  4. WebRTC Android(安卓)源码编译
  5. android开机自动唤醒屏幕、打开锁屏页并
  6. Android 选项卡实现
  7. 【Android】Android如何查看分区情况
  8. 下载、更新Android(安卓)SDK慢的解决方案
  9. android用户界面之SeekBar教程实例汇总
  10. Android Service教程