博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android开发 缩放到指定比例的尺寸
阅读量:6904 次
发布时间:2019-06-27

本文共 1720 字,大约阅读时间需要 5 分钟。

一种通过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));         }

 

转载地址:http://ghldl.baihongyu.com/

你可能感兴趣的文章
使用python进行文件备份
查看>>
《数据结构与抽象:Java语言描述(原书第4版)》一JI2.2.1 延缓处理:throws子句...
查看>>
看,那人好像一个产品狗,对,这就是产品狗
查看>>
《 Java并发编程从入门到精通》 Java线程池的监控
查看>>
《Ansible权威指南》一1.8 Python多环境扩展管理
查看>>
《全栈性能测试修炼宝典 JMeter实战》—第1章 1.5节从招聘要求看岗位价值
查看>>
Gartner2017年十大技术趋势
查看>>
sum() 函数性能堪忧,列表降维有何良方?
查看>>
fastreport 导出图片并打印
查看>>
学习html我们从百度百科开始
查看>>
如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上
查看>>
实现一个炫酷的随机标签排列效果(颜色随机,大小随机,成菱形排列的列表)...
查看>>
flex 布局
查看>>
数字资产交易所开发:平台币快速吸金的背后
查看>>
小程序自定义音频组件,带滚动条,IOS循环失效问题
查看>>
Swift开发之粒子动画的实现
查看>>
我学Java我傲娇
查看>>
挖矿蠕虫肆虐,详解云防火墙如何轻松“制敌”
查看>>
Linux -- Samba之客户端访问(Linux和windows)
查看>>
八个Docker的真实应用场景
查看>>