zoukankan      html  css  js  c++  java
  • 缩放图片加载大图片

    > 图片大小 = 图片的总像素 * 每个像素占用的大小
     
    * 单色图:每个像素占用1/8个字节
    * 16色图:每个像素占用1/2个字节
    * 256色图:每个像素占用1个字节
    * 24位图:每个像素占用3个字节
     
    ---
    #加载大图片到内存(掌握)
    >Android系统以ARGB表示每个像素,所以每个像素占用4个字节,很容易内存溢出
     
    ##对图片进行缩放(掌握)
    *第一步:获取屏幕宽高
     
    Display dp = getWindowManager().getDefaultDisplay();
         int screenWidth = dp.getWidth();
         int screenHeight = dp.getHeight();
    * 第二步:获取图片宽高
     
    Options opts = new Options();
    //请求图片属性但不申请内存
         opts.inJustDecodeBounds = true;
         BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
         int imageWidth = opts.outWidth;
         int imageHeight = opts.outHeight;
    * 第三步:图片的宽高除以屏幕宽高,算出宽和高的缩放比例,取较大值作为图片的缩放比例
     
    int scale = 1;
         int scaleX = imageWidth / screenWidth;
         int scaleY = imageHeight / screenHeight;
         if(scaleX >= scaleY && scaleX > 1){
         scale = scaleX;
         }
         else if(scaleY > scaleX && scaleY > 1){
         scale = scaleY;
         }
    * 第四步:按缩放比例加载图片
     
    //设置缩放比例
    opts.inSampleSize = scale;
    //为图片申请内存
         opts.inJustDecodeBounds = false;
         Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
         iv.setImageBitmap(bm);
     
    部分代码:
    Options opts = new Options();
    //只请求图片宽高,不解析图片像素
    opts.inJustDecodeBounds = true;
    //返回null,获取图片宽高,保存在opts对象中
    BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
    //获取图片宽高
    int imageWidth = opts.outWidth;
    int imageHeight = opts.outHeight;
     
    //获取屏幕宽高
    Display dp = getWindowManager().getDefaultDisplay();
    int screenWidth = dp.getWidth();
    int screenHeight = dp.getHeight();
     
    //计算缩放比例
    int scale = 1;
    int scaleWidth = imageWidth / screenWidth;
    int scaleHeight = imageHeight / screenHeight;
     
    //判断取哪个比例
    if(scaleWidth >= scaleHeight && scaleWidth > 1){
    scale = scaleWidth;
    }
    else if(scaleWidth < scaleHeight && scaleHeight > 1){
    scale = scaleHeight;
    }
     
    //设置缩小比例
    opts.inSampleSize = scale;
    opts.inJustDecodeBounds = false;
    //获取缩小后的图片的像素信息
    Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
     
    ImageView iv = (ImageView) findViewById(R.id.iv);
    iv.setImageBitmap(bm);
  • 相关阅读:
    退休夫妇不顾反对坚持创业,把自己的品牌推向了市场
    年终将至,财务人如何做好数据分析?
    圣诞快乐:Oracle Database 19c 的10大新特性一览
    perl 获取表记录数
    rex 防止调度还没完成后又继续发起
    希腊女孩创办自媒体教希腊语,如今用户已达1000人
    在20天里赚三千多元,他靠创意经营商店,扩大了店面
    小杂货店的崛起,他坚信创新和拼搏是成功的两大法宝
    thinkphp
    thinkphp
  • 原文地址:https://www.cnblogs.com/SoulCode/p/6393350.html
Copyright © 2011-2022 走看看