標籤:isp systems android tool 個數 pack 開發 ice XML
1. 圖片適配(根據手機螢幕的像素密度載入不同檔案夾中的圖片)
手機螢幕的像素密度:一英寸中包含的像素點的個數
例如:480x800 的像素密度 = 480^2+800^2 開方 = 932.95 / 4 = 233.23dpi
在開發的時候一般不會在每個檔案夾中放置一套圖片,一般是確定一個解析度製作一套圖,然後按照 android 開發規則,讓 android 自動適配其他分辨,如果有圖片無法適配,再去製作相應的圖片到不同的解析度對應的目錄中進行圖片適配
2.dimens.xml 檔案適配(根據手機螢幕的像素密度載入不同檔案夾中的 dimens.xml )
values -> dimens.xml
主要用來適配,在 app 中適配固定寬高控制項,也可以設定固定的距離操作
res-> values-1280x720 -> dimens.xml
<resources>
<dimen name="viewpagerheight">200dp</dimen>
</resources>
布局檔案使用
<com.itheima.zhbj98.view.RoolViewPager
android:id="@+id/menunewsitempager_vp_viewpager"
android:layout_width="match_parent"
android:layout_height="@dimen/viewpagerheight"
></com.itheima.zhbj98.view.RoolViewPager>
3.layout 布局檔案適配(根據手機螢幕的像素密度載入不同 layout 檔案夾中的布局檔案)
根據手機螢幕的像素密度載入不同 layout 檔案夾中的布局檔案
res -> layout-1280x720 -> 布局檔案(名稱保持一致)
4. 代碼適配
1. 按比例適配
根據比例,設定在不同解析度中比例顯示的寬度(寬度在每個解析度中都是不一樣,但是佔用的比例是一樣的)
首先請求 200 在 320 解析度中佔用的比例,再去 1280x720 解析度中求出在 720 中相應的比例佔用的寬度
// 先求出 200 佔用 320 的比例,然後求出比例在 720 中的寬度
//200/320 * 手機介面的寬度
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
slidingMenu.setBehindOffset(200 * width / 320);// 單位是 px
2. dp和px相互轉換
建立工具類
public class DensityUtil {
/**
* 根據手機的解析度從 dip 的單位 轉成為 px( 像素 )
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density; // 擷取手機的螢幕的密度
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據手機的解析度從 px( 像素 ) 的單位 轉成為 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
5. 權重適配
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="horizontal"
android:weightSum="10"
>
<!-- weightSum : 將控制項分成幾份 -->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#FF0000" />
<TextView
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:background="#00FF00" />
</LinearLayout>
螢幕適配方式的介紹