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. Junit4参数化测试实现程序与用例数据分离

    http://touchfu.iteye.com/blog/732930 现状:你是不是还在为自己的TestCase代码杂乱无章而苦恼,咎其根本还在于针对不同的用例,输入参数和mock信息的组装全部作 ...

  2. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  3. 【HOW】如何限制Reporting Services报表导出功能中格式选项

    Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...

  4. java写入文件之txt文本

    FileWriter fw=new FileWriter("D:\\test.txt") fw.write("文本值") fw.flush() fw.close ...

  5. 【摘】crontab 各时间含义

    HELL=/bin/bash <==使用哪種 shell 介面 PATH=/sbin:/bin:/usr/sbin:/usr/bin <==執行檔搜尋路徑 MAILTO=root < ...

  6. 总结一下 input propertychange

    最近做的一些项目,经常遇到输入框检查的需求,最常见的是即时搜索,今天好好小结一下. ========================================================== ...

  7. vi和vim 的常用操作

    到文件末尾: ESC + shift G : 到文件头: G + G: 整块模式 快捷键  [不使用鼠标,来选择块] v                   字符选择,会将光标经过的地方反白选择! V ...

  8. c# 获取某日期所在周的第一天和最后一天

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WyfC ...

  9. mysqldump的使用

    mysqldump按导入:mysqldump -u用户名 -p密码 -h主机 数据库 < 路径eg:mysql -uroot -p1234 db1 < c:\a.txtmysqldump导 ...

  10. django--app(六)

    1.应用加载 服务启动后,再新添加应用,是不会自动加载的,需要重启服务.