• Android 显示大图片


    主要的代码如下:

         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;
        }
  • 相关阅读:
    新框架的选择
    ‘’火星文‘’的解析
    http.request请求及在node中post请求参数解析
    http.request的请求
    ReactNative环境配置的坑
    return false与return true的区别
    什么是DOM,DOM level 123 的区别是什么
    页面重绘和回流以及优化
    时代人物之任正非
    Adriod与HTML+JS的交互
  • 原文地址:https://www.cnblogs.com/wuyou/p/3662377.html
Copyright © 2020-2023  润新知