package com.example.administrator.filemanager.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

/**
 * Created by Administrator on 2016/12/30.
 */

public class BitmapUtil {

    /**
     * 图片路径为字符串格式时
     */
    public static Bitmap loadBitmap(String pathName,SizeMessage sizeMessage){
        //获取图片大小
        int imgWeight=sizeMessage.getWidth();
        int imgHeight=sizeMessage.getHeight();
        Context context=sizeMessage.getContext();
        //图像处理
       Options options=new Options();
        options.inJustDecodeBounds=true;//打开图片边缘
        BitmapFactory.decodeFile(pathName,options);
        int imgW = options.outWidth;//处理后拿到的宽
        int imgH = options.outHeight;//处理后拿到的高
        if (imgW <= imgWeight && imgH<=imgHeight){
            //设置加载图片时的比例
            options.inSampleSize = 1;
        }else{
            //按比例计算宽高
            int scaleW = imgW/imgWeight;
            int scaleH = imgH/imgHeight;
            //比较大小
            int scale = scaleW > scaleH ? scaleW:scaleH;
            //按比例加载资源
            options.inSampleSize = scale;
        }
        options.inJustDecodeBounds = false;//关闭图片边缘
        Bitmap bitmap = BitmapFactory.decodeFile(pathName,options);
        return bitmap;
    }
    /**
     * 图片路径为int类型时
     */
    public  static Bitmap loadBitmap(int redId,SizeMessage sizeMessage) {
        //获取图片大小
        int intweight=sizeMessage.getWidth();
        int intheight=sizeMessage.getHeight();
        Context context=sizeMessage.getContext();

        //图像处理
        Options options=new Options();

        options.inJustDecodeBounds = true;//打开图片边缘 拿到信息;
        BitmapFactory.decodeResource(context.getResources(),redId,options);
        int imgW = options.outWidth;//处理后拿到的宽
        int imgH = options.outHeight;//处理后拿到的高

        if (imgW <= intweight && imgH<=intheight){
            //设置加载图片时的比例
            options.inSampleSize = 1;
        }else{
            //按比例计算宽高
            int scaleW = imgW/intweight;
            int scaleH = imgH/intheight;
            //比较大小
            int scale = scaleW > scaleH ? scaleW:scaleH;
            //按比例加载资源
            options.inSampleSize = scale;
        }
        options.inJustDecodeBounds = false;//关闭图片边缘
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),redId,options);
        return bitmap;

    }
    /**
     * 内部类
     */
    public static class SizeMessage{
        private Context context;
        private int width;
        private  int height;

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public SizeMessage(Context context,boolean isPX, int width, int height) {
            this.context = context;
            if(!isPX){//如果不是,转换成像素格式
                width=DeviceUtil.dp2px(context,width);
                height=DeviceUtil.dp2px(context,height);
            }
            this.width = width;
            this.height = height;
        }
    }
}

获取图片工具类:BitmapUtil的更多相关文章

  1. Java图片工具类,完成图片的截取和任意缩放

    package com.common.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Renderin ...

  2. 拍照、本地图片工具类(兼容至Android7.0)

    拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...

  3. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  4. Android--很实用的图片工具类

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; imp ...

  5. Android 调节图片工具类

    package com.base.changeimage; import android.graphics.Bitmap; import android.graphics.Canvas; import ...

  6. 系统获取 IP 工具类

    系统获取 IP 工具类 import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterf ...

  7. 如何在SpringBoot当中上传多个图片或者上传单个图片 工具类

    如何在SpringBoot当中上传多个图片[上传多个图片 ] 附赠工具类 1.SpringBoot 上传图片工具类 public class SpringUploadUtil { /*** * 上传图 ...

  8. 压缩图片工具类,压缩100KB以内拿走直接用

    最近遇到自拍上传图片过大问题,很烦恼,所以自己写了一个压缩图片的工具类使用,自测效果很不错,可以压缩到KB以内,像素还可以分辨清晰 下面Java代码奉上: import lombok.extern.s ...

  9. Android自定义圆形图片工具类(CTRL+C加CTRL+V直接使用)

    先贴一下工具类的代码!可直接复制粘贴 public class RoundImageView extends ImageView { private Paint mPaint; //画笔 privat ...

随机推荐

  1. ADXL3xx: 读取 ADXL3xx 加速度传感器

    原文链接:https://www.arduino.cc/en/Tutorial/ADXL3xx ADXL3xx加速度传感器 本教程将为你展示如何读取Analog Devices的ADXL3xx系列加速 ...

  2. C# char 和string之间转换

    har数组要转换成string可没想象的那么容易.需要使用到System.Text.StringBuilder!实例如下: char[] temp={a,b,c};System.Text.String ...

  3. 翻箱倒柜,《Delphi中建议使用的语句》

    (*//标题:Delphi中建议使用的语句整理:Zswang连接:http://www.csdn.net/Expert/TopicView1.asp?id=724036日期:2002-06-22支持: ...

  4. Business Unit Lookup in Form

    Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...

  5. (转载)FT232RL通信中断问题解决办法总结

    原文地址:http://cuiweidabing.blog.163.com/blog/static/66631928201101514021658/ FT232RL是FTDI(www.ftdichip ...

  6. VS 远程调试之 “The visual studio remote debugger does not support this edition of windows”

    The error message "The visual studio remote debugger does not support this edition of windows&q ...

  7. Django HTML 显示文章摘要

    在用Django写个人博客,发现一般都是标题加上文章摘要,然后点击标题可以看详细内容.这样主页就可以多显示几篇文章. 那么就要用到文章摘要功能. 比如要100个字的文章摘要,就可以这样写: {{art ...

  8. KindEditor 编辑器使用方法

    http://kindeditor.net/docs/usage.html 编辑器使用方法 1. 下载编辑器 下载 KindEditor 最新版本,下载之后打开 examples/index.html ...

  9. 第一个ruby程序

    老实说不是很喜欢去讨论ruby和python的对比,似乎总是把两个语言放在对立的位置上,我觉得没有必要,同样是动态语言,同样是解释型脚本语言,很多特性都是互相影响的,语言本身也在不断进化,我们更应该关 ...

  10. Pycharm注册码

    name : newasp===== LICENSE BEGIN =====09086-1204201000001EBwqd8wkmP2FM34Z05iXch1AkKI0bAod8jkIffywp2W ...