Android图片大小调整动态实现方法

移动开发 Android
如何才能实现动态的Android图片大小调整,以满足我们在实际应用中的需求呢?在这里就以代码的形式为大家详细介绍了相关操作方法。

Android操作系统中对于图片的操作我们在以前的文章中也有所介绍。不过对于图片的大小调整往往都局限于固定的调整。如何才能满足动态大小调整呢?我们在这里就为大家详细介绍有关Android图片大小调整的动态实现方法。

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

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

  1. view plaincopy to clipboardprint?  
  2. private Bitmap getImageFromAssetFile(String fileName,int how){   
  3. Bitmap image = null ;   
  4. try {   
  5. AssetManager am = game.getAssets();   
  6. InputStream is = am.open(fileName);   
  7. image = BitmapFactory.decodeStream(is);   
  8. is.close();   
  9. }catch (Exception e){   
  10. }   
  11. return zoomImage(image,how);   
  12. }   
  13. public Bitmap zoomImage(Bitmap bgimage,int how) {   
  14. int bmpwidth = bgimage.getWidth();   
  15. int bmpheight = bgimage.getHeight();   
  16. float scaleWidth=0;   
  17. float scaleHeight=0;   
  18. Matrix matrix = new Matrix();   
  19. if(how==0){   
  20. scaleWidth = ((float) width) / bmpwidth;   
  21. scaleHeight = ((float) height) / bmpheight;   
  22. }else{   
  23. scaleWidth=Math.min(width,height)/bmpwidth;   
  24. scaleHeight=Math.min(width, height)/bmpheight;   
  25. }   
  26. private Bitmap getImageFromAssetFile(String fileName,int how){   
  27. Bitmap image = null ;   
  28. try {   
  29. AssetManager am = game.getAssets();   
  30. InputStream is = am.open(fileName);   
  31. image = BitmapFactory.decodeStream(is);   
  32. is.close();   
  33. }catch (Exception e){   
  34. }   
  35. return zoomImage(image,how);   
  36. }   
  37. public Bitmap zoomImage(Bitmap bgimage,int how) {  
  38. int bmpwidth = bgimage.getWidth();  
  39. int bmpheight = bgimage.getHeight();  
  40. float scaleWidth=0;  
  41. float scaleHeight=0;  
  42. Matrix matrix = new Matrix();  
  43. if(how==0){  
  44. scaleWidth = ((float) width) / bmpwidth;  
  45. scaleHeight = ((float) height) / bmpheight;  
  46. }else{  
  47. scaleWidth=Math.min(width,height)/bmpwidth;  
  48. scaleHeight=Math.min(width, height)/bmpheight;  
  49. }  

其中,scaleWidth和scaleHeight是欲缩放后的大小,这里加个参数how是防止有不需要缩放的情况~

Android图片大小调整的操作方法就为大家介绍到这里。

【编辑推荐】

  1. Android界面互调具体应用方式解析 
  2. Android智能指针使用方法介绍 
  3. Android查询联系人信息正确操作方法 
  4. Android命令行启动程序正确使用技巧解析 
  5. 全方位解读Android多媒体框架源码 
责任编辑:曹凯 来源: CSDN
相关推荐

2009-06-09 10:44:36

JavaScript改

2016-05-18 14:20:12

LinuxgThumb图片

2009-07-23 15:24:37

ASP.NET Ima

2009-06-26 08:41:45

LinuxUbuntu通知机制

2018-02-09 12:46:50

MySQL优化InnoDB

2013-05-02 09:31:02

虚拟化服务器

2011-05-12 11:28:20

按比例缩放

2013-03-08 11:03:17

PowerShellVHDResizer虚拟磁盘

2024-03-15 09:32:47

线程池应用程序性能

2011-03-11 14:07:51

Ubuntu 11.0

2010-04-09 15:15:01

Oracle SGA

2011-06-27 22:45:48

笔记本评测

2010-01-27 17:53:18

Android显示网络

2009-09-29 17:27:22

Ubuntu 9.10显示效果操作系统

2021-01-11 13:20:06

Python图像大小编程语言

2009-02-20 11:01:38

Vista字体调整

2011-07-21 17:35:11

iPhone Table 图片

2011-07-25 15:32:06

iPhone Table 动态

2017-05-11 21:30:01

Android动态代理ServiceHook

2021-09-13 09:30:09

Linux调整图像ImageMagick
点赞
收藏

51CTO技术栈公众号