Android適配方案小結(二)

來源:互聯網
上載者:User

標籤:android 適配

該節主要記錄從代碼中擷取與螢幕適配相關的各個參數:

Java代碼如下

public class ScreenUtil {/** * Note: * 只有activity可以使用getWindowManager,否則應該使用 * Context.getResources().getDisplayMetrics()來擷取*//** * 擷取DisplayMetric相關參數 * @param context * @return */public static String getMetricParams(Activity context){DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);return "density:"+dm.density+";densityDpi:"+dm.densityDpi+";height:"+dm.heightPixels+";width:"+dm.widthPixels+";scaledDensity:"+dm.scaledDensity+";xdpi:"+dm.xdpi+";ydpi:"+dm.ydpi;}/** * 擷取螢幕尺寸,單位為像素 * @param context * @return */public static String getScreenSizeInInPixels(Activity context){DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);double heightInInPixels = (double)dm.heightPixels;double widthInInPixels = (double)dm.widthPixels;return "高:"+heightInInPixels+" 寬:"+widthInInPixels+" 單位(像素)";}/** * 擷取螢幕尺寸,單位為英寸 * 計算螢幕尺寸應該使用精確密度:xdpi ydpi來計算 * 使用歸一化密度:densitydpi是錯誤的,它是固定值, * 120 160 240 320 480,根據dp計算像素才使用它 * @param context * @return */public static String getScreenSizeInInch(Activity context){DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);double heightInInch = (double)dm.heightPixels / (double)dm.ydpi;double widthInInch = (double)dm.widthPixels / (double)dm.xdpi;double ScrrenSizeInInch = Math.sqrt(heightInInch*heightInInch+ widthInInch*widthInInch);return "高:"+heightInInch+" 寬:"+widthInInch+" 尺寸:"+ScrrenSizeInInch+" 單位(英寸)";}/** * 擷取螢幕尺寸,單位為dp * @param context * @return */public static String getScreenSizeInInDp(Activity context){DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);float heightInInDp = px2dip((Context)context, (float)dm.heightPixels);float widthInInDp = px2dip((Context)context, (float)dm.widthPixels);return "高:"+heightInInDp+" 寬:"+widthInInDp+" 單位(dp)";}/** * dp轉px * @param context * @param dpValue * @return */public static int dip2px (Context context, float dpValue){final float scale = context.getResources().getDisplayMetrics().density;return (int)(dpValue*scale+0.5f);}/** * px轉dp * @param context * @param pxValue * @return */public static int px2dip(Context context, float pxValue){final float scale = context.getResources().getDisplayMetrics().density;return (int)(pxValue/scale+0.5f);}}

XML的設定:

<support-screens
android:anyDensity = "true"
android:largeScreens = "true"
android:normalScreens = "true"
android:resizeable = "true"
android:smallScreens = "true
android:xlargeScreens = "true">
android:anyDensity = "true"時,應用程式安裝在不同的密度的終端上面,
程式分別會去載入xxhdpi, xhdpi, hdpi, mdpi,ldpi檔案夾中的資源。




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.