您的位置:首页 > 移动开发 > Android开发

android图片压缩

2016-01-26 17:03 513 查看
本宝宝第一次写博客,只为记录一些常用而忘记的东西。多的不说,上代码

package com.denny.xutiles.imagegradedemo.utils;

import android.content.res.Resources;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class BitmapUtils {

//从sd卡路径读取

public static Bitmap getFitSampleBitmap(String imgPath,int width,int height){

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(imgPath, options);

options.inSampleSize = getFitSampleSize(width,height,options);

options.inJustDecodeBounds = false;

return BitmapFactory.decodeFile(imgPath, options);

}

//读取mipmap下的资源图片

public static Bitmap getBitmapFromResource(Resources resources,int id,int width,int height){

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(resources,id,options);

options.inSampleSize = getFitSampleSize(width,height,options);

options.inJustDecodeBounds = false;

return BitmapFactory.decodeResource(resources,id,options);

}

private static int getFitSampleSize(int width, int height, BitmapFactory.Options options) {

int sampleSize = 1;

if (options.outHeight > height || options.outWidth > width) {

int widthRadio = Math.round((float)options.outWidth / (float) width);

int heightRadio = Math.round((float)options.outHeight / (float) height);

sampleSize = Math.min(widthRadio, heightRadio);

}

return sampleSize;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: