Android圖片大小調整動態實現方法

來源:互聯網
上載者:User

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圖片大小調整的操作方法就為大家介紹到這裡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.