Android下屏幕适配

来源:互联网 发布:淘宝店铺装修素材包 编辑:程序博客网 时间:2024/05/23 10:36
什么是适配
适配即当前应用在相同的手机上面显示相同的效果。

适配前需要首先确定当前手机所属像素密度类型(如:xhdpi、hdpi、mdpi等),下面以华为G700、模拟器为例,讲解如何计算像素密度。

案例一:
  1.         手机型号:G700
  2.         手机分辨率:1280*720 (注:手机两个直角边上分别放置了1280及720个像素点)
  3.         手机尺寸大小:5英寸(手机斜边长度)
  4.         
  5.         假设a,b分别为两个直角边,c为斜边,由勾股定理可得出计算方式:sqrt(a*a+b*b)/c
  6.         计算结果:sqrt(1280*1280+720*720)/5 ≈ 293.72dpi
  7.         根据google官方文档说明得出,当前手机最接近320dpi,则将其归纳在xhdpi手机范围内,
  8.         即1dp=2px;
复制代码

案例二:
  1.         手机型号:模拟器
  2.         手机分辨率:800*480(注:手机两个直角边上分别放置了800及480个像素点)
  3.         手机尺寸大小:3.7英寸(手机斜边大小)
  4.         
  5.         计算结果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi
  6.         根据google官方文档(图1-1)得出,当前手机接近240dpi,则将其归纳在hdpi手机范围内,
  7.         即1dp=1.5px。
复制代码

按照以上计算方式,大致可以将市场上的手机划分为5个像素密度等级,具体如下:
(1)     ldpi:120dpi,像素密度与dp转换关系为:1dp = 0.75px
(2)     mdpi:160dpi ,像素密度与dp转换关系为:1dp = 1px
(3)     hdpi:240dpi,像素密度与dp转换关系为:1dp = 1.5px
(4)     xhdpi:320dpi,像素密度与dp转换关系为:1dp = 2px
(5)     xxhdpi:480dpi,像素密度与dp转换关系为:1dp = 3px

 
如何适配
下面以华为手机G700和模拟器的对比,讲解如何进行屏幕适配,具体方式如下:
   适配方式1:图片适配
不同像素密度的手机加载工程资源文件(res)中不同资源图片,以手机G700和模拟器为例,图片的布局代码如下所示:
  1.         <RelativeLayout 
  2.               xmlns:android="http://schemas.android.com/apk/res/android"
  3.               xmlns:tools="http://schemas.android.com/tools"
  4.               android:layout_width="match_parent"
  5.               android:layout_height="match_parent"
  6.              tools:context=".MainActivity" >
  7.                <ImageView
  8.                     android:layout_width="wrap_content"
  9.                     android:layout_height="wrap_content"
  10.                     android:background="@drawable/a"/>
  11.         </RelativeLayout>
复制代码

若使用G700(xhdpi)加载a.jpg文件,该文件位于res/drawable-xhdpi文件夹下,显示效果如下:

 
若使用模拟器(hdpi)加载a.jpg文件,该文件位于res/drawable-hdpi文件夹下,显示效果如下:
 
  适配方式2dimens.xml文件适配
dimens.xml存在于工程资源(res)文件夹中不同values(如:value-1280x720、value-800x480)文件夹下,可用于指定控件大小,不同像素密度手机加载不同values文件夹下的dimens.xml文件,使用方式如下:   
  1.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.             xmlns:tools="http://schemas.android.com/tools"
  3.             android:layout_width="match_parent"
  4.             android:layout_height="match_parent"
  5.             android:orientation="vertical"
  6.             tools:context=".MainActivity" >
  7.                 <!-- 不同的手机加载不同的dp -->
  8.             <TextView
  9.                 android:background="#987654"
  10.                 android:layout_width="@dimen/width"
  11.                 android:layout_height="wrap_content"
  12.                 android:text="@string/hello_world" />
  13.         </LinearLayout>
复制代码

模拟器(hdpi):加载dimens.xml资源文件,位于res/value-800x480文件夹下
  1.         <resources>
  2.             <dimen name="width">160dp</dimen>
  3.         </resources>
复制代码

根据上述hdpi dppx的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。
G700(xhdpi):加载dimens.xml资源文件,位于res/value-1280x720文件夹下
  1.         <resources>
  2.             <dimen name="width">180dp</dimen>
  3.         </resources>
复制代码

根据上述xhdpi 中dp和px的转换关系1dp = 2px,则180dp = 360px,当前控件宽度应该位于屏幕中间位置。

G700(xhdpi)显示的图片效果如下所示:
模拟器(hdpi)显示的图片效果如下所示:
   适配方式3:布局文件适配
不同分辨率的手机,加载不同的布局文件已达到适配效果。创建多个layout(如:layout-1280x720、layout-800x480)文件夹用于存放不同像素密度手机所需布局文件。

模拟器(hdpi):加载activity_main.xml布局文件,位于res/layout-800x480文件夹下:
  1.    <RelativeLayout 
  2.              xmlns:android="http://schemas.android.com/apk/res/android"
  3.             xmlns:tools="http://schemas.android.com/tools"
  4.             android:layout_width="match_parent"
  5.             android:layout_height="match_parent"
  6.             tools:context=".MainActivity" >
  7.                     <TextView
  8.                         android:layout_width="wrap_content"
  9.                         android:layout_height="wrap_content"
  10.                         android:text="800*480手机会去加载的布局文件" />
  11.                 </RelativeLayout>
复制代码

G700(xhdpi):加载activity_main.xml布局文件,位于res/layout-1280x720文件夹下:
  1.           <RelativeLayout 
  2.                    xmlns:android="http://schemas.android.com/apk/res/android"
  3.                     xmlns:tools="http://schemas.android.com/tools"
  4.                     android:layout_width="match_parent"
  5.                     android:layout_height="match_parent"
  6.                     tools:context=".MainActivity" >
  7.                       <TextView
  8.                             android:layout_width="wrap_content"
  9.                             android:layout_height="wrap_content"
  10.                             android:text="1280*720手机会去加载的布局文件" />
  11.                 </RelativeLayout>
复制代码

G700(xhdpi)显示的图片效果如下:
 
模拟器(hdpi)显示的图片效果如下所示:
 
   适配方式4java代码适配
      通过android相应api获取当前手机的宽高像素值,按比例分配屏幕中控件的宽高以达到适配效果,下面是布局和实现功能的核心代码:
      布局文件
  1.         <RelativeLayout 
  2.            xmlns:android="http://schemas.android.com/apk/res/android"
  3.             xmlns:tools="http://schemas.android.com/tools"
  4.             android:layout_width="match_parent"
  5.             android:layout_height="match_parent"
  6.             tools:context=".MainActivity" >
  7.             <TextView
  8.                 android:id="@+id/tv"
  9.                 android:background="#000000"
  10.                 android:layout_width="wrap_content"
  11.                 android:layout_height="wrap_content"
  12.                 android:text="@string/hello_world" />
  13.         </RelativeLayout>
复制代码

      activityoncreate核心代码:
  1. TextView tv  = (TextView) findViewById(R.id.tv);
  2.                 //获取封装当前手机屏幕信息对象,用于存放宽高值
  3.                 DisplayMetrics metrics  = new DisplayMetrics();
  4.                 //给当前屏幕设置宽高
  5.                 getWindowManager().getDefaultDisplay().getMetrics(metrics);
  6.                 //获取高度
  7.                 Constant.srceenHeight = metrics.heightPixels;
  8.                 //获取宽度
  9.                 Constant.srceenWidth = metrics.widthPixels;
  10.                 Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);
  11.                 Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);
  12.                 //宽高各占50%
  13.                 RelativeLayout.LayoutParams layoutParams = new 
  14.                 RelativeLayout.LayoutParams(
  15.                                 (int)(Constant.srceenWidth*0.5+0.5), 
  16.                                 (int)(Constant.srceenHeight*0.5+0.5));
  17.                 tv.setLayoutParams(layoutParams);
复制代码
G700(xhdpi)显示效果如下:

 

模拟器(hdpi)显示效果如下:

 
  适配方式5:权重适配
    通过android提供的(权重)剩余空间分配,达到适配效果。布局文件如下所示:
  1.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.             xmlns:tools="http://schemas.android.com/tools"
  3.             android:layout_width="match_parent"
  4.             android:layout_height="match_parent"
  5.             android:orientation="horizontal"
  6.             tools:context=".MainActivity" >
  7.                 <TextView 
  8.                     android:background="#000000"
  9.                     android:layout_width="0dp"
  10.                     android:layout_weight="1"
  11.                  android:layout_height="match_parent"/>
  12.                 <TextView 
  13.                     android:background="#123456"
  14.                     android:layout_width="0dp"
  15.                     android:layout_weight="1"
  16.                     android:layout_height="match_parent"/>
  17.         </LinearLayout>
复制代码

G700(xhdpi)显示的图片效果如下所示:
 
模拟器(hdpi)显示的图片效果如下所示:
 

0 0
原创粉丝点击