Android图片大小调整动态达成方法
发布时间:2021-12-18 18:39:58 所属栏目:PHP教程 来源:互联网
导读:Android操作系统中对于图片的操作我们在《Android图片浏览源码解读》文章中也有所介绍。不过对于图片的大小调整往往都局限于固定的调整。如何才能满足动态大小调整呢?我们在这里就为大家详细介绍有关Android图片大小调整的动态实现方法。 昨天,动态获取图片
Android操作系统中对于图片的操作我们在《Android图片浏览源码解读》文章中也有所介绍。不过对于图片的大小调整往往都局限于固定的调整。如何才能满足动态大小调整呢?我们在这里就为大家详细介绍有关Android图片大小调整的动态实现方法。 昨天,动态获取图片资源获取的很爽啊,后来,换了一张png,128*128的,Run as android application,天哪,居然覆盖了我大半个屏幕,都不留一点情面给我展示了。。。。看来,必须要找个方法让图片自适应大小,于是修改了一下获取图片的代码,让图片能自适应。 一下就是Android图片大小调整的相关代码示例: private Bitmap getImageFromAssetFile(String fileName,int how){ Bitmap image = null ; try { AssetManager am = game.getAssets(); InputStream is = am.open(fileName); image = BitmapFactory.decodeStream(is); is.close(); }catch (Exception e){ } return zoomImage(image,how); } public Bitmap zoomImage(Bitmap bgimage,int how) { int bmpwidth = bgimage.getWidth(); int bmpheight = bgimage.getHeight(); float scaleWidth=0; float scaleHeight=0; Matrix matrix = new Matrix(); if(how==0){ scaleWidth = ((float) width) / bmpwidth; scaleHeight = ((float) height) / bmpheight; }else{ scaleWidth=Math.min(width,height)/bmpwidth; scaleHeight=Math.min(width, height)/bmpheight; } private Bitmap getImageFromAssetFile(String fileName,int how){ Bitmap image = null ; try { AssetManager am = game.getAssets(); InputStream is = am.open(fileName); image = BitmapFactory.decodeStream(is); is.close(); }catch (Exception e){ } return zoomImage(image,how); } public Bitmap zoomImage(Bitmap bgimage,int how) { int bmpwidth = bgimage.getWidth(); int bmpheight = bgimage.getHeight(); float scaleWidth=0; float scaleHeight=0; Matrix matrix = new Matrix(); if(how==0){ scaleWidth = ((float) width) / bmpwidth; scaleHeight = ((float) height) / bmpheight; }else{ scaleWidth=Math.min(width,height)/bmpwidth; scaleHeight=Math.min(width, height)/bmpheight; } 其中,scaleWidth和scaleHeight是欲缩放后的大小,这里加个参数how是防止有不需要缩放的情况~ Android图片大小调整的操作方法就为大家介绍到这里。 ![]() (编辑:云计算网_泰州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |