获取图片工具类:BitmapUtil

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;
        }
    }
}

上一篇:UVa 10106 乘积


下一篇:Codeforces 387C George and Number(贪心)