Android屏幕适配怎么分析

发布时间:2022-01-15 09:13:51 作者:柒染
来源:亿速云 阅读:115

Android屏幕适配怎么分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

为什么要屏幕适配?

Android屏幕适配怎么分析

碎片化

为了保证用户获得一致的用户体验效果,使得某一元素在Android不同尺寸、不同分辨率的手机上具备相同的显示效果,则需要我们进行屏幕适配。

基础概念

屏幕尺寸

屏幕尺寸是指屏幕对角线的长度,单位是英寸,1 inch=2.54 cm

屏幕分辨率

手机在横向和纵向上的像素点数总和,单位是像素(pixel),1px =  1像素点,举个栗子,1080x1920,即宽度方向上有1080个像素点,在高度方向上有1920个像素点。

屏幕像素密度

每英寸像素点个数,单位是dpi,dots per inch。为简便起见,Android 将所有屏幕密度分组为六种通用密度:  低、中、高、超高、超超高和超超超高。

Android屏幕适配怎么分析

dpi_example

屏幕密度无关像素dp(dip)

Density Independent Pixels,即密度无关像素。

Android屏幕适配怎么分析

density-test-bad

Android屏幕适配怎么分析

dimen_example2

独立比例像素sp

Scale Independent Pixels, 即sp或sip。

Android开发时用此单位设置文字大小,可根据字体大小***项进行缩放,推荐使用12sp、14sp、18sp、22sp作为字体设置的大小,不推荐使用奇数和小数,容易造成精度的丢失问题,小于12sp的字体会太小导致用户看不清。

屏幕适配之图片适配

Android屏幕适配怎么分析

在设计图标时,对于5种主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)应按照2:3:4:6:8的比例进行缩放。例如一个启动图片ic_launcher.png,它在各个像素密度文件夹下大小为:

存在的问题

解决方法

Android SDK加载图片流程

根据加载图片的流程,可以得出理论上提供一套图片就可以了。

那么应该提供哪种分辨率规格呢?

原则上越高越好,同时结合当前主流分辨率屏幕

自动拉伸图片

Android屏幕适配怎么分析

ninepatch_raw

Android屏幕适配怎么分析

ninepatch_examples

屏幕适配之布局适配

布局参数

使用wrap_content, match_parent, layout_weight。

weight的使用

Android屏幕适配怎么分析

weight_examples

<LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="horizontal">        <Button           android:layout_width="0dp"           android:layout_height="wrap_content"           android:layout_weight="1"           android:text="weight = 1"/>        <Button           android:layout_width="0dp"           android:layout_height="wrap_content"           android:layout_weight="2"           android:text="weight = 2"/>   </LinearLayout>

当layout_width为match_parent,layout_weight分别为1和2

<LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="horizontal">        <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:layout_weight="1"           android:text="weight = 1"/>        <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:layout_weight="2"           android:text="weight = 2"/>   </LinearLayout>

weight的计算

宽度 = 原来宽度 + 权重比值 * 剩余宽度

***个按钮:宽度 = 0 + 1/3 * 屏宽 = 1/3屏宽

第二个按钮:宽度 = 0 + 2/3 * 屏宽 = 2/3屏宽

***个按钮:宽度 = 屏宽 + 1/3 (屏宽 - 2 屏宽) = 2/3屏宽

第二个按钮:宽度 = 屏宽 + 2/3 (屏宽 - 2 屏宽) = 1/3屏宽

布局使用

使用相对布局,禁用绝对布局。

限定符

尺寸限定符

最小宽度限定符

布局别名

***的两个文件的xml内容是完全相同的,这会带来:文件名的重复从而带来一些列后期维护的问题,修改一个文件,可能忘记修改另外一个。于是为了要解决这种重复问题,我们引入了布局别名。

<?xml version="1.0" encoding="utf-8"?>  <resources>      <item name="main" type="layout">@layout/activity_main</item>  </resources>
<?xml version="1.0" encoding="utf-8"?>   <resources>       <item name="main" type="layout">@layout/activity_twopanes</item>   </resources>
<?xml version="1.0" encoding="utf-8"?>   <resources>       <item name="main" type="layout">@layout/activity_twopanes</item>   </resources>

屏幕方向限定符

屏幕适配之dimen适配

Android屏幕适配怎么分析

Nexus S (4英寸 480x800:hdpi)

Android屏幕适配怎么分析

dimen_example2

即使使用dp,依然不能解决屏幕分辨率的适配问题,我们可以针对不同的屏幕创建不同的dimen值。

<resources>  <dimen name="button_length_1">180dp</dimen>  <dimen name="button_length_2">160dp</dimen>  </resources>
<resources>       <dimen name="button_length_1">113dp</dimen>       <dimen name="button_length_2">100dp</dimen>   </resources>

屏幕适配之百分比布局

<?xml version="1.0" encoding="utf-8"?>   <android.support.percent.PercentRelativeLayout       xmlns:android="http://schemas.android.com/apk/res/android"       xmlns:app="http://schemas.android.com/apk/res-auto"       android:layout_width="match_parent"       android:layout_height="wrap_content">        <Button           android:layout_width="0dp"           android:layout_height="wrap_content"           android:text="30%"           app:layout_widthPercent="30%"/>        <Button           android:layout_width="0dp"           android:layout_height="wrap_content"           android:layout_alignParentRight="true"           android:text="20%"           app:layout_widthPercent="20%"/>    </android.support.percent.PercentRelativeLayout>

屏幕适配之自适应用户界面

Android屏幕适配怎么分析

newsreader_land

Android屏幕适配怎么分析

newsreader_port

当NewsReader在横屏时是双面板,左侧是HeadLinesFragment, 右侧是ArticleFragment, 点击新闻标题,  切换ArticleFragment的内容。当NewsReader在竖屏时是单面板,只有个HeadLinesFragment,  点击新闻标题,跳转到ArticleActivity去显示新闻内容。所以,要实现这样的横竖屏适配,只是通过布局是完成不了的,不同业务逻辑的处理,还需要写代码来完成,这就是我们的自适应用户界面。

使用布局别名

<resources>  <item name="main_layout" type="layout">@layout/onepane_with_bar</item>  <bool name="has_two_panes">false</bool>  </resources>
<resources>  <item name="main_layout" type="layout">@layout/twopanes</item>  <bool name="has_two_panes">true</bool>  </resources>
<resources>      <item name="main_layout" type="layout">@layout/onepane</item>      <bool name="has_two_panes">false</bool>  </resources>

判断是单面板还是双面板

View articleView = findViewById(R.id.article); mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;//如果能够找到ArticleFragment则是双面板

单双面板的不同业务逻辑

public void onHeadlineSelected(int index) {     mArtIndex = index;    if (mIsDualPane) {          // display it on the article fragment         mArticleFragment.displayArticle(mCurrentCat.getArticle(index));     }    else {          // use separate activity         Intent i = new Intent(this, ArticleActivity.class);         i.putExtra("catIndex", mCatIndex);         i.putExtra("artIndex", index);         startActivity(i);     } }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 分享一点关于Android屏幕适配的种种
  2. 腾讯优测| 让Android屏幕适配开发更简单-Google百分比布

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:网页视觉设计如何理解

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》