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;
}
}
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;
}
}
相关文章推荐
- android开发中遇到的问题汇总
- B4A 或其它Android开发平台连接USB设备进行调试
- 如何实现android中监听来电并生成悬浮窗体提示
- Android中notifyDataSetInvalidated()和notifyDataSetChanged()
- 外层竖向ScrollView,里层横向ScrollView滑动冲突
- Android Gradle打包签名的方式
- 优化Android Studio/Gradle构建
- Android4.0以上机器的虚拟按键中显示menu键
- Android菜鸟进阶之路4.2-音频文件每隔一段时间存储一次
- Android中的错误解决。Plugin is too old
- Android 多渠道打包之混淆文件ProGuard技术详解-特别篇(打包系列教程之三)
- android之播放视频
- [原] Jenkins Android 自动打包配置
- Android XML解析
- Android textView复制,popupwindow显示在文字上方
- android keystore 找回密码
- android之播放多媒体文件一(播放音频)
- Android万能适配器基类
- android 组合控件-属性获取
- android 性能分析工具、布局分析工具、monkey自动化测试的简单使用