2011-05-06 116 views
3

朋友,如何根據屏幕分辨率縮放圖像?

我使用下面的代碼resizeImage

private Bitmap resizeImage(final Bitmap image) { 
     Bitmap resizedImage = null; 
     if(image != null) 
     { 
     int maxHeight = 80; //actual image height coming from internet 
     int maxWidth = 150; //actual image width coming from internet 

     int imageHeight = image.getHeight(); 
     if (imageHeight > maxHeight) 
     imageHeight = maxHeight; 
     int imageWidth = (imageHeight*image.getWidth())/image.getHeight(); 
     if (imageWidth > maxWidth) { 
     imageWidth = maxWidth; 
     imageHeight = (imageWidth*image.getHeight())/image.getWidth(); 
     } 
     resizedImage = Bitmap.createScaledBitmap(image, imageWidth, imageHeight, true); 
     } 
     return resizedImage; 
     } 

來自互聯網的到來。 現在它在高分辨率屏幕上工作正常,但在小屏幕上它沒有任何人指導我該如何根據屏幕分辨率顯示圖像?

+0

背後是什麼計算imageHeigt和imageWidth邏輯??其實我在我的代碼中使用你的邏輯,但是當我在我的Galaxy 10.1上運行它時,圖像變得更小了.. – User42590 2013-01-01 08:27:02

回答

3
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
int screenWidth = display.getWidth(); 
int screenHeight = display.getHeight(); 

因此,您可以根據屏幕分辨率顯示圖像。

+0

我面臨着同樣的問題,我無法正確獲取屏幕寬度和高度如何使用..如果可以請分享一些更多的代碼給你的答案... – Sam 2012-11-07 05:57:23

+0

我們接下來要做的事..這是真正的問題..如何設置圖像符合上述計算的大小? – 2013-05-15 05:30:18

+0

以及它可能是你的問題,但人問這個問題已經知道下一步該怎麼做。這裏你去resizedImage = Bitmap.createScaledBitmap(image,imageWidth,imageHeight,true); – 2013-05-15 06:33:52

2

創建三種可繪製文件夾drawable-hdpi,drawable-mdpi,drawable-ldpi。在這些文件夾中放入具有相同名稱的圖像的差異分辨率。該應用程序將映射自己。另一種方法是創建9個補丁的圖像。 9幅圖像根據分辨率自行調整。並且在創建佈局時還要小心。你必須正確設置佈局。屏幕高度和寬度需要在屏幕的哪個部分設置圖像。

由於 迪帕克

+0

這是正確的解決方案。無需自行調整大小,只需將圖像放入正確的文件夾即可。 – async 2014-04-05 13:39:30