android開發圖片解析度問題解決方案

來源:互聯網
上載者:User

dpi是什麼呢?

dpi是“dot per inch”的縮寫,每英吋像素數。
四種密度分類: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
一般情況下的普通螢幕:ldpi是120,mdpi是160,hdpi是240,xhdpi是320。

dpi計算公式
DPI=對角線的像素值/尺寸

手機螢幕解析度和螢幕密度是兩碼事!並不是800*480的解析度手機圖片就應該放在hdpi檔案夾中。5.0英寸 800*480屬於mdpi

也可以通過代碼擷取:

 
  1. DisplayMetrics metric = new DisplayMetrics(); 
  2.        getWindowManager().getDefaultDisplay().getMetrics(metric); 
  3.        int width = metric.widthPixels;  // 螢幕寬度像素) 
  4.        int height = metric.heightPixels;  // 螢幕高度像素) 
  5.        float density = metric.density;  // 螢幕密度0.75 / 1.0 / 1.5) 
  6.        int densityDpi = metric.densityDpi;  // 螢幕密度DPI120 / 160 / 240) 

android會根據螢幕本身的尺寸與密度特性,自動載入對應的資源,並把它們從邏輯像素DIP,用於定義介面布局)轉換成螢幕上的物理像素。

首先看看 系統是如何通過Resources的getDrawable(int id)方法找圖片的

 
  1. public Drawable getDrawable(int id) throws NotFoundException { 
  2.         TypedValue value; 
  3.         synchronized (mAccessLock) { 
  4.             value = mTmpValue; 
  5.             if (value == null) { 
  6.                 value = new TypedValue(); 
  7.             } else { 
  8.                 mTmpValue = null; 
  9.             } 
  10.             getValue(id, value, true); 
  11.         } 
  12.         Drawable res = loadDrawable(value, id); 
  13.         synchronized (mAccessLock) { 
  14.             if (mTmpValue == null) { 
  15.                 mTmpValue = value; 
  16.             } 
  17.         } 
  18.         return res; 
  19.     } 
  20.   

TypedValue 我們可以理解為儲存資料的類型,主要被Resouces使用於持有的資源值
通過getValue(id,value,true)方法去得到該id的資源的屬性

 
  1. public void getValue(int id, TypedValue outValue, boolean resolveRefs) 
  2.             throws NotFoundException { 
  3.         boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs); 
  4.         if (found) { 
  5.             return; 
  6.         } 
  7.         throw new NotFoundException("Resource ID #0x" 
  8.                                     + Integer.toHexString(id)); 
  9.     } 
  10.   

最後通過loadDrawable(value, id)得到drawable,該方法到了底層的C代碼,大致意思就是通過TypedValue中的方法和屬性就可以獲得我們想要的屬性值,然後載入圖片

OK,下面來做個實驗.

實驗一: 手機是1280*720 4.3英寸 屬於xdpi 圖片解析度為960*640 按正常圖片不縮放 圖片放在手機裡面 應該不會充滿 整個手機) 我把圖片放在不

檔案夾 載入時間(ms) 圖片顯示 說明

drawable 311 充滿螢幕 圖片有展開

drawable-nodpi 130 未充滿螢幕 圖片顯示正常

drawable-ldpi 442 充滿螢幕 圖片有展開

drawable-mdpi 383 充滿螢幕 圖片有展開

drawable-hdpi 226 充滿螢幕 圖片有展開

drawable-xhdpi 109 未充滿螢幕 圖片顯示正常

實驗二: 手機是800*480 4.3英寸 屬於hdpi 圖片解析度為960*640 按正常圖片不縮放 圖片放在手機裡面 應該充滿 整個手機) 我把圖片放在不同drawable檔案夾中

檔案夾 載入時間(ms) 圖片顯示 說明

drawable 290 充滿螢幕 圖片展開

drawable-nodpi 127 充滿螢幕 圖片顯示正常

drawable-ldpi 369 充滿螢幕 圖片展開

drawable-mdpi 346 充滿螢幕 圖片展開

drawable-hdpi 124 充滿螢幕 圖片顯示正常

drawable-xhdpi 241 未充滿螢幕 圖片縮放

得到結論:

drawable-nodpi 中 圖片不會被展開

系統在得到圖片時候,會先到裝置對應的dpi的檔案夾下去去找資源檔,找到後應該不會做縮放直接返回圖片。

如果沒有在對應的dpi檔案夾中找到,回去其他檔案夾中尋找,找到後會做相應的縮放。

在高dpi找到的圖片會縮放,在低dpi的會展開

還有就是 源碼中可以看出 loadDrawable的過程實在C層做的。通過系統資源id載入會比java層直接載入圖片更便捷。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.