2015年05月27日 22:21:25 缥缈孤鴻影_love 閱讀數:10287
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/tung214/article/details/46054233
Android應用裡裡,大家一定飽受各種layout-sw480dp layout-sw600dp-land layout-sw720dp-port之苦,往往不知道自己的平闆加載的是哪個Layout而不得不一個個嘗試,網上一大堆說不清的教程。本人查閱很多資料,給大家分享一種計算方法确定自己平闆所加載哪個dp的資源。
事實上,sw不是software的,意思,我猜是shortest width,最短的寬度。
sw-xxxx-dp的計算公式是 sw *160/dpi
比如 1280*800, sw 是800
480*800, sw 是480
如下範例,相信可以更好了解計算方法,其中dpi其實是人為在Android/device/softwinner/astar-common/astar-y3.mk中設定的,
沒有Android源碼的可以通過 adb shell getprop ro.sf.lcd_density 獲得。
機型 | 分辨率 | sw | dpi= ro.sf.lcd_density | sw *160/dpi = dp |
H7(Android5.1) | 1280*720 | 720 | 213 | 720*160/213=540.84 = sw340dp |
H7(Android4.4) | 1280*720 | 720 | 160 | 720*160/160=720= sw720dp |
A33 astar-y2 | 1024*768 | 768 | 160 | 768*160/160 =768 = sw720dp |
雜牌 | 800*480 | 480 | 120 | 480*160/120 =640= sw600dp |
雜牌 | 800*480 | 480 | 160 | 480*160/160 =480= sw480dp |
通過公式 sw*160/dpi 計算出結果之後,選擇一個比這個結果小,而又最接近這個值的dp。
比如,範例中H7(Android 5.1)算出來是540.84,如果你的layout下存在一個 sw480dp,那這裡dp肯定是sw480dp,如果沒有sw480dp,隻能找到sw340dp.
land是橫屏,port是豎屏。
關于dip定義:
frameworks\base\core\java\android\util\DisplayMetrics.java
/**
* Standard quantized DPI for high-density screens.
*/
public static final int DENSITY_HIGH = 240;
/**
* Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
* {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
* instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
*/
public static final int DENSITY_260 = 260;
/**
* Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
* {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
* instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
*/
public static final int DENSITY_280 = 280;
/**
* Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
* {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
* instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
*/
public static final int DENSITY_300 = 300;
/**
* Standard quantized DPI for extra-high-density screens.
*/
public static final int DENSITY_XHIGH = 320;