Android适配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lalate/article/details/54598970

所有单位使用单位px


0.美术切图按固定比例。如1080*1920


一:XML配置

1.根目录设置固定宽高(美术切图比例)。如:1080*1920

2.图片引用使用src,防止自动拉伸。如:android:src = "@drawable/loading_bg"

3. 图片宽高使用切图原始大小,如:loading_bg 的尺寸为1080*1920 

例:

	android:layout_width="1080px"
	android:layout_height="1920px"
	android:src="@drawable/loading_bg"

二:Activity控制根据屏幕大小缩放

getWindow().getDecorView().setBackgroundColor(Color.BLACK);

WindowManager wm = (WindowManager) FolderListActivity.this.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
float scaleNum = (float)height/1920f;

rootLayout.setPivotY(0);
rootLayout.setScaleY(scaleNum);
rootLayout.setScaleX(scaleNum);
if (scaleNum * 1080 < width) {
   int offsetX = (int)((width - scaleNum * 1080) / 2);
   ViewGroup.LayoutParams params = rootLayout.getLayoutParams();
   if (params instanceof FrameLayout.LayoutParams) {
      ((FrameLayout.LayoutParams)params).setMargins(offsetX, 0, 0, 0);
   }
   if (params instanceof LinearLayout.LayoutParams) {
      ((LinearLayout.LayoutParams)params).setMargins(offsetX, 0, 0, 0);
   }
   if (params instanceof RelativeLayout.LayoutParams) {
      ((RelativeLayout.LayoutParams)params).setMargins(offsetX, 0, 0, 0);
   }
   rootLayout.setLayoutParams(params);
}
Log.e("当前手机分辨率:","宽:"+width+"..高:"+height);
Log.e("当前手机分辨率:","缩放系数:"+scaleNum);


三:颤抖吧,凡人!!!

xml布局样例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/mainMenuLayout"
                android:layout_width="1080px"
                android:layout_height="1920px"
                android:background="#000"
                android:orientation="vertical">

    <ImageView
        android:layout_width="1080px"
        android:layout_height="1920px"
        android:src="@drawable/loading_bg"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--标题-->
        <ImageView
            android:layout_width="884px"
            android:layout_height="338px"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/menu_sudoku"/>

        <!--上按钮-->
        <!--两圈-->
        <RelativeLayout
            android:id="@+id/menu_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical">

            <ImageView
                android:id="@+id/er_1"
                android:layout_width="1035px"
                android:layout_height="1102px"
                android:layout_centerInParent="true"
                android:src="@drawable/er_1"/>

            <ImageView
                android:id="@+id/er"
                android:layout_width="1035px"
                android:layout_height="1102px"
                android:layout_centerInParent="true"
                android:scaleType="fitCenter"
                android:src="@drawable/er"/>

            <ImageView
                android:id="@+id/classicBtn"
                android:layout_width="313px"
                android:layout_height="87px"
                android:layout_alignLeft="@+id/er"
                android:layout_alignTop="@+id/er"
                android:layout_marginLeft="150px"
                android:layout_marginTop="400px"
                android:src="@drawable/btn_classic"/>

            <ImageView
                android:id="@+id/popularBtn"
                android:layout_width="332px"
                android:layout_height="87px"
                android:layout_alignBottom="@+id/er"
                android:layout_alignRight="@+id/er"
                android:layout_marginBottom="300px"
                android:layout_marginRight="180px"
                android:src="@drawable/btn_popular"/>
        </RelativeLayout>
        <!--三圈-->
        <RelativeLayout
            android:id="@+id/menu_three"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal|center_vertical"
            android:visibility="gone">

            <ImageView
                android:id="@+id/san_1"
                android:layout_width="350px"
                android:layout_height="350px"
                android:layout_marginTop="20px"
                android:background="@drawable/san_1"/>

            <ImageView
                android:id="@+id/san"
                android:layout_width="350px"
                android:layout_height="350px"
                android:layout_marginTop="20px"
                android:background="@drawable/san"/>

            <ImageView
                android:id="@+id/easyBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="53px"
                android:layout_marginTop="145px"
                android:background="@drawable/btn_easy"/>

            <ImageView
                android:id="@+id/mediumBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="168px"
                android:layout_marginTop="250px"
                android:background="@drawable/btn_medium"/>

            <ImageView
                android:id="@+id/hardBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="205px"
                android:layout_marginTop="115px"
                android:background="@drawable/btn_hard"/>
        </RelativeLayout>

        <!--下按钮-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10px"
            android:layout_marginRight="30px"
            android:gravity="right"
            android:orientation="horizontal"
            >

            <ImageView
                android:id="@+id/statisticsBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_static"/>

            <TextView
                android:layout_width="20px"
                android:layout_height="wrap_content"/>

            <ImageView
                android:id="@+id/soundBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/sound_open"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="40px"
            android:gravity="right"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/menu_back"
                android:layout_width="50px"
                android:layout_height="30px"
                android:layout_alignParentRight="true"
                android:layout_marginRight="30px"
                android:background="@drawable/button_out"
                android:visibility="gone"
                />
        </LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/gameContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RelativeLayout>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/lalate/article/details/54598970