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

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