首页 > 移动平台 > 详细

Android 显示大图片

时间:2014-04-13 19:00:54      阅读:539      评论:0      收藏:0      [点我收藏+]

主要的代码如下:

bubuko.com,布布扣
     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);
bubuko.com,布布扣

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

bubuko.com,布布扣
   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;
    }
bubuko.com,布布扣

 

Android 显示大图片,布布扣,bubuko.com

Android 显示大图片

原文:http://www.cnblogs.com/wuyou/p/3662377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!