Android之屏幕适配

    • Android之屏幕适配
      • 适配方式一图片适配
      • 适配方式二dimensxml文件适配
      • 适配方式三布局文件适配
      • 适配方式四java代码适配
      • 适配方式五权重适配

适配:即当前应用在相同的手机上面显示相同的效果。适配前需要首先确定当前手机所属像素密度类型(如:xhdpi、hdpi、mdpi等),以下已华为G700、模拟器为例,计算其像素密度。

案例一:

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

案例二:

手机型号:模拟器手机分辨率:800*480(注:手机两个直角边上分别放置了800及480个像素点)手机尺寸大小:3.7英寸(手机斜边大小)计算结果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi根据google官方文档(图1-1)得出,当前手机接近240dpi,则将其归纳在hdpi手机范围内,即1dp=1.5px。参照以上方式可将市场上大多数手机划分为5个像素密度等级,分别为:ldpi:120dpi,像素密度与dp转换关系为:1dp = 0.75pxmdpi:160dpi ,像素密度与dp转换关系为:1dp = 1pxhdpi:240dpi,像素密度与dp转换关系为:1dp = 1.5pxxhdpi:320dpi,像素密度与dp转换关系为:1dp = 2pxxxhdpi:480dpi,像素密度与dp转换关系为:1dp = 3px


Android之屏幕适配_第1张图片

(注:以下案例就当前两款手机进行屏幕适配测试)

适配方式一:图片适配

不同像素密度的手机加载工程资源文件(res)中不同资源图片,以上述两款手机为例。布局代码如下:

<RelativeLayout 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=".MainActivity" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/a" /></RelativeLayout>

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

Android之屏幕适配_第2张图片

模拟器(hdpi):加载a.jpg资源文件,位于res/drawable-hdpi文件夹下,显示效果如下:

Android之屏幕适配_第3张图片

适配方式二:dimens.xml文件适配

dimens.xml存在于工程资源(res)文件夹中不同values(如:value-1280x720、value-800x480)文件夹下,可用于指定控件大小,不同像素密度手机加载不同values文件夹下的dimens.xml文件,使用方式如下:

<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"    android:orientation="vertical"    tools:context=".MainActivity" >    <!-- 不同的手机加载不同的dp -->    <TextView        android:background="#987654"        android:layout_width="@dimen/width"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></LinearLayout>

模拟器(hdpi):加载dimens.xml资源文件,位于res/value-800x480文件夹下

<resources>    <dimen name="width">160dp</dimen></resources>根据上述hdpi dp和px的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。

G700(xhdpi):加载dimens.xml资源文件,位于res/value-1280x720文件夹下

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

G700(xhdpi)显示效果如下:

Android之屏幕适配_第4张图片

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

Android之屏幕适配_第5张图片

适配方式三:布局文件适配

不同分辨率的手机,加载不同的布局文件已达到适配效果。创建多个layout(如:layout-1280x720、layout-800x480)文件夹用于存放不同像素密度手机所需布局文件。

模拟器(hdpi):加载activity_main.xml布局文件,位于res/layout-800x480文件夹下:

    <RelativeLayout 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=".MainActivity" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="800*480手机会去加载的布局文件" />    </RelativeLayout>

G700(xhdpi):加载activity_main.xml布局文件,位于res/layout-1280x720文件夹下:

    <RelativeLayout 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=".MainActivity" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="1280*720手机会去加载的布局文件" />    </RelativeLayout>

G700(xhdpi)显示效果如下:

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

Android之屏幕适配_第6张图片

适配方式四:java代码适配

通过android相应api获取当前手机的宽高像素值,按比例分配屏幕中控件的宽高以达到适配效果。核心代码如下:

布局文件<RelativeLayout 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=".MainActivity" >    <TextView        android:id="@+id/tv"        android:background="#000000"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>activity中oncreate核心代码:    TextView tv  = (TextView) findViewById(R.id.tv);    //获取封装当前手机屏幕信息对象,用于存放宽高值    DisplayMetrics metrics  = new DisplayMetrics();    //给当前屏幕设置宽高    getWindowManager().getDefaultDisplay().getMetrics(metrics);    //获取高度    Constant.srceenHeight = metrics.heightPixels;    //获取宽度    Constant.srceenWidth = metrics.widthPixels;    Log.i(tag, "Constant.srceenHeight = "+Constant.srceenHeight);    Log.i(tag, "Constant.srceenWidth = "+Constant.srceenWidth);    //宽高各占50%    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(            (int)(Constant.srceenWidth*0.5+0.5),             (int)(Constant.srceenHeight*0.5+0.5));    tv.setLayoutParams(layoutParams);

G700(xhdpi)显示效果如下:

Android之屏幕适配_第7张图片

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

Android之屏幕适配_第8张图片

适配方式五:权重适配

通过android提供的(权重)剩余空间分配,已达到适配效果。显示界面加载布局文件如下:

<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"    android:orientation="horizontal"    tools:context=".MainActivity" >    <TextView         android:background="#000000"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"/>    <TextView         android:background="#123456"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"/></LinearLayout>

G700(xhdpi)显示效果如下:

Android之屏幕适配_第9张图片

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

更多相关文章

  1. Android手机直播(三)声音采集
  2. Android多媒体--访问网络上的Audio对应的M3U文件,实现网络音频流
  3. 文件操作二
  4. Android使用Itext生成pdf文件
  5. android下创建文件夹和修改其权限的方法
  6. Google Android会成为手机领域的微软Windows吗?
  7. Android中调用手机自带输入法比如三星s5830i,仅限制输入浮点数,崩
  8. ZDC:2011年Android手机用户使用行为研究报告

随机推荐

  1. WebRTC 的 Android(安卓)2 Android(安卓)
  2. Android(安卓)多渠道打包
  3. afinal logoAndroid的快速开发框架 afina
  4. Android横竖屏切换总结
  5. [android] Serializable 和 Parcelable
  6. How to create and use SDCard image for
  7. Android的SocketTCP客户端发送信息
  8. 1.1 Android(安卓)入门
  9. Android弹幕实现:基于B站弹幕开源系统(5)-抽
  10. Qt on Android:将Qt调试信息输出到logcat