Android總結之drawable(hdpi,mdpi,ldpi)文件夾的使用



做Android有段時間了,但是好多細節還沒有深入理解,關於Android中drawable文件夾的使用理解的就不是很深入。

Android爲開發者提供了兩種解決適配問題的方法,第一種方式是使用dip的單位,另一種方法是使用不同的drawable文件夾,今天就詳細說下不同文件夾的區別

Android中提供了三個存放圖片的文件夾,分別爲hdpi,mdpi,ldpi,google文檔上說,Andriod系統會根據手機屏幕的大小及屏幕密度去選擇不同文件夾下的圖片資源,以此來實現在不同大小不同屏幕分辨率下適配的問題。

比如在一個低分辨率的手機上,Android就會選擇ldpi文件夾下的圖片,但是如果沒有在ldpi的文件夾下找見相關的資源文件,Android系統會首先從hdpi文件夾中選擇文件,然後對圖片資源進行縮放處理,顯示在屏幕上;如果hdpi文件夾下也沒有的話,會在默認的drawable文件夾中尋找。

明白以上內容,就很好解釋圖片放錯文件夾,顯示大小不同的問題了。如圖

圖1

圖2

出現圖二的原因是將放在hdpi下的圖片放到了默認的文件夾下

本身圖片是同一張圖片,由於沒有在hdpi文件夾中找到對應圖片,系統在默認文件夾下找見了圖片資源,但是這時系統會認爲改圖適用於中等分辨率的屏幕上,如果直接放到高分辨率的手機上不能適配,所以系統會自動的將圖片放大,所以雖然是同一張圖片,但是現實出來就一個正常效果,一個放大效果。

同理,如果同一張圖片,放置在ldpi的文件夾下,在低分辨率的手機上顯示正常,但是如果放在hdpi文件夾中,系統認爲該圖是爲高分辨率顯示的,要將圖片縮小處理,所以顯示出來的效果就是圖變小了。

所以我們在平時要注意,明明都是一張圖片,只是放在不同的文件夾中,顯示出來的效果就很不同,原因就是我們對Android還不夠了解。

 

對了,我還有個疑問,就是佈局文件中設置wrap_content時,view的大小的單位是dip還是px呢!!!

 

以下爲google文檔的描述

At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:

  1. The system uses the appropriate alternative resource

    Based on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with thehdpi qualifier (such as drawable-hdpi/) might be the best match, so the system uses the drawable resource from this directory.

  2. If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density

    The "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.

    However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章