Android中怎么动态调整图片大小

发布时间:2021-06-26 15:00:07 作者:Leah
来源:亿速云 阅读:116

Android中怎么动态调整图片大小,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

昨天,动态获取图片资源获取的很爽啊,后来,换了一张png,128*128的,Run as android application,天哪,居然覆盖了我大半个屏幕,都不留一点情面给我展示了。。。。看来,必须要找个方法让图片自适应大小,于是修改了一下获取图片的代码,让图片能自适应。

一下就是Android图片大小调整的相关代码示例:

view plaincopy to clipboardprint?  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中怎么动态调整图片大小的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 将android中如何调整Toast位置?
  2. android   动态改变图片大小

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:uptimerobot监控的使用方法

下一篇:Vue中Axios从远程/后台读取数据的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》