android开发 缩放到指定比例的尺寸
一种通过matrix矩阵缩放:
//使用Bitmap加Matrix来缩放
public static Drawable resizeImage(Bitmap bitmap, int w, int h)
{
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h; float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return new BitmapDrawable(resizedBitmap);
}
另一种,通过设置BitmapFactory.Options的inSampleSize参数来缩放
//使用BitmapFactory.Options的inSampleSize参数来缩放
public static Drawable resizeImage2(String path,
int width,int height)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//不加载bitmap到内存中
BitmapFactory.decodeFile(path,options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 1; if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)
{
int sampleSize=(outWidth/width+outHeight/height)/2;
Log.d(tag, "sampleSize = " + sampleSize);
options.inSampleSize = sampleSize;
} options.inJustDecodeBounds = false;
return new BitmapDrawable(BitmapFactory.decodeFile(path, options));
}
android开发 缩放到指定比例的尺寸的更多相关文章
- Android开发(十一)——ImageView的尺寸设置scaleType
ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType). android:scaleType是控制图片如 ...
- Android开发ImageView控件缩放图片
首先还是最基础的ImageView控件如何显示图片: <ImageView Android:id="@+id/imgView" ...
- Android开发 - 掌握ConstraintLayout(十)按比例设置视图大小
有时候在布局界面的时候,UI要求某个View或者某张图片按比例显示,以适应不同的屏幕分辨率. 通常我们时通过自定义View或者引入第三方的库来解决.现在我们既然已经使用了ConstraintLayou ...
- 《Android开发艺术探索》读书笔记 (6) 第6章 Android的Drawable
本节和<Android群英传>中的第六章Android绘图机制与处理技巧有关系,建议先阅读该章的总结 第6章 Android的Drawable 6.1 Drawable简介 (1)Andr ...
- Android开发随手记
本文是作者在Android开发实践中的随手速记,记录一些小问题的解决方案和注意事项,持续更新. 以下是速记内容,若有不严谨的地方,望小伙伴们指出. 1.Module 不生成R文件,可尝试取消对该Mod ...
- android开发分辨率适配总结
重要概念 什么是屏幕尺寸.屏幕分辨率.屏幕像素密度? 什么是dp.dip.dpi.sp.px?他们之间的关系是什么? 什么是mdpi.hdpi.xdpi.xxdpi?如何计算和区分? 在下面的内容中我 ...
- [置顶]
Android开发百科全书
友情提示根据目录 快速查找问题 %1$s %1$d Android string 1.整型,比如"我今年23岁了",这个23是整型的.在string.xml中可以这样写,<s ...
- Android开发——Android手机屏幕适配方案总结
)密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...
- android 开发 - 网络图片加载库 Fresco 的使用。
概述 Fresco 是 facebook 的开源类库,它支持更有效的加载网络图片以及资源图片.它自带三级缓存功能,让图片显示更高效. 介绍 Fresco 是一个强大的图片加载组件. Fresco 中设 ...
随机推荐
- MVC_学习笔记_2_Authorize
MVC5_学习笔记_2_Authorize/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Author: Nico ...
- oracle中=>是什么意思
=> 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...
- delphi构造&析构调用顺序
_ClassCreate ->Create ->AfterConstruction(->DoCreate / OnCreate) BeforeDestruction(->DoD ...
- linux积累
在多文件中批量替换字符串grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'
- DTAP street
一个网站程序的上线一般要经过开发[Development]测试[Testing]验收[Acceptance]生产[Production].所以又叫做DTAP street.对应有开发环境.测试环境.验 ...
- find your present
Description In the present, and their card numbers are , , , , .so your present will be the one with ...
- Linux下使用Magent+Memcached缓存服务器集群部署
1.编译安装libevent cd /root/soft_hhf/ wget http://cloud.github.com/downloads/libevent/libevent/libeven ...
- ociuldr 支持分多个数据文件
在审计工作,将几亿条的oracle数据通过sqlserver自带工具导入到sqlserver中,速度不是特别的理想,虽然通过视图方式能提高一些速度,但是既不简洁,也不方便. 用ociuldr工具,可以 ...
- AppCan应用开发之插件实践篇-支付插件
电商O2O无疑是目前最受用户欢迎的APP类型,而在这类APP中必不可少的就是在线支付功能了.在AppCan平台开发这类应用时,官方封装了微信支付.支付宝.银联等多种支付SDK,更接入了多家聚合支付平台 ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...