码迷,mamicode.com
首页 > 其他好文 > 详细

获取图片工具类:BitmapUtil

时间:2017-01-04 10:22:38      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:资源   sample   ext   格式   nbsp   graphics   androi   set   cto   

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

标签:资源   sample   ext   格式   nbsp   graphics   androi   set   cto   

原文地址:http://www.cnblogs.com/ll-ouyang/p/6247409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!