一种android软件自适应不同屏幕密度的方法

文档序号:6427313阅读:130来源:国知局
专利名称:一种android软件自适应不同屏幕密度的方法
技术领域
本发明涉及一种android软件自适应不同屏幕密度的方法,属于计算机软件技术领域。
背景技术
Android是基于Linux内核的操作系统,是Google公司在2007年11月5日公布的手机操作系统。早期由原名为"Android"的公司开发,谷歌在2005年收购"Android. Inc"后,继续进行对Android系统开发运营,它采用了软件堆层(software stack,又名软件叠层)的架构,主要分为三部分。底层Linux内核只提供基本功能,其他的应用软件则由各公司自行开发,部分程序以Java编写。2011年初数据显示,仅正式上市两年的操作系统Android已经超越称霸十年的塞班系统,使之跃居全球最受欢迎的智能手机平台。现在,Android系统不但应用于智能手机, 也在平板电脑市场急速扩张。Android无疑是当下最炙手可热的手机操作系统,在优势方面,Android平台首先就是其开放性,开发的平台允许任何移动终端厂商加入到Android联盟中来。但正是由于 Android的开放性,使得不同移动终端厂商生产出来的移动终端也就不尽相同,主要体现在屏幕的不同。Android主要有以下几种屏QVGA 和 WQVGA 屏 density = 120 ;HVGA 屏 density = 160 ;WVGA 屏 density = 240 ;其中VGA(Video Graphics Array)是 IBM 在 1987 年随 PS/2 机一起推出的一种视频传输标准,具有分辨率高、显示速率快、颜色丰富等优点,在彩色显示器领域得到了广泛的应用。QVGA(Quarter VGA)。顾名思义即VGA的四分之一尺寸,亦即在液晶屏幕(IXD) 上输出的分辨率是 240X320 像素。WQVGA(Wide Quarter Video Graphics Array)数码产品屏幕分辨率的一种,代表480X272 (宽高比16 9)或者400XM0 (宽高比5 3)的屏幕分辨率,而不是国内厂商标称的480XM0。HVGA (Half-size VGA)即VGA (640*480)的一半, 分辨率为(480*320),(3 2宽高比)。WVGA(Wide VGA),其分辩率为800X480象素,是扩大了 VGA (640 X 480)的分辨率,应用于PDA和手机等,因为很多网页的宽度都是800,所以 WVGA的屏幕会更加适合于浏览网页,可以说是未来手持设备的分辨率的大趋势。所以大多数Android应用程序就必须面对一个问题,怎样能自动适应不同的屏幕密度(density)。针对这个问题,本发明提供了一种实现方法,使Android应用程序可以自动适应不同的屏幕密度(density),以合适的方式展示程序内容,避免显示出现问题。

发明内容
针对现有技术中存在的问题,本发明的目的在于提供一种android软件自适应不同屏幕密度的方法。 本发明的技术方案为一种android软件自适应不同屏幕密度的方法,其步骤为1)判断android程序布局配置文件中控件高和宽是否能设置为变量;如果能,则将android程序布局配置文件中控件高和宽分别设置为一变量,控件高和宽的变量值设置为填充所在容器全部空间时的值或能够容纳控件内部内容大小时的值;然后根据该控件宽和高的值,以及设置的该控件比重,确定该控件的位置和大小;如果不能,则在android程序的资源文件夹res下设置若干布局文件夹,每一布局文件夹对应存储一屏幕密度值的布局配置文件;然后根据当前屏幕密度值,选择匹配的布局配置文件;2)获取当前屏幕密度值,计算android程序所需动态生成控件的大小,在当前屏幕上生成所需控件。进一步的,所述资源文件夹res下设置若干图片文件夹,每一图片文件夹对应存储同一屏幕密度规格要求的图片;然后根据当前屏幕密度值,选择匹配的图片。进一步的,所述生成所需控件的方法为首先,获取当前屏幕密度值,根据密度值计算密度比例因子;然后,根据密度比例因子和所需控件的设置尺寸,计算得到所需控件在当前屏幕上的控件实际大小。进一步的,利用当前屏幕密度值/160,计算得到所述密度比例因子。进一步的,根据公式控件实际大小=控件大小/密度比例因子,计算控件实际大小。进一步的,通过android程序自身提供的类和方法获取当前屏幕密度值。进一步的,所述所需动态生成控件包括图标。本发明的主要内容包括一、在编写应用程序的布局配置文件时(XXX. xml),避免去使用数字去设置 android—height 禾口 android—width (android—height一帛^牛白勺高,android—width 代表某一控件的宽),使用 android_height 禾口 android_width 等于 fill_parent 或 wrap_ content (艮口, 夺 android—height 禾口 android—width 值设为 fill—parent 或 wrap—content, fill_parent是强制性地使控件扩展以填充所在容器的全部空间。wrap^ontent是控件根据内部内容自动扩展以适应其大小。),再与android_weight (android_weight代表控件所占的比重,就是lay0ut_Weight的值越大,所占比例也越大。Iayoutjeight的设置和 layout_height, layout_width不同的组合得出来不同的结果)配合实现控件的位置与大小。二、如果在布局配置文件中必须要使用数字去设置android_height和andr0id_ width,或android_textSize (android_textSize代表控件的字体大小,对于有文字显示的控件才有此属性),我们可以在res目录下创建不同的layout文件夹(在android项目里, 主要的资源文件是放在res文件夹下的,在res文件夹下的不同文件夹存放不同的资源文件,如res/drawable下存放的是图片文件,res/layout下存放的是屏幕布局文件等。在 java代码中实现对资源的访问),比如layout-hdpi对应density = 240、layout-mdpi对应 density = 160、layout-1 对应 dpi = 120 (dpi 是“dot per inch” 的缩写,每英寸像素数,一般情况下的普通屏幕=Idpi是120,mdpi是160,hdpi是240。android系统自身很智能, 它会根据屏幕的density去选择不同的layout-xxx文件夹下的布局配置文件。),系统会根据屏幕的大小自己选择合适的layout进行使用,即当启动应用程序的时候,系统会自己根据当前屏幕的密度,选择res下不同的目录下的资源文件,例如,如果density = 160,就会选择layout-mdpi下的对应xml文件,如果res下没有layout-mdpi文件夹或layout-mdpi 下不存在java代码中需要调用的xml文件,会选择layout文件夹下的xml文件,如果还没有layout文件夹,程序会报错。layout文件夹是系统默认读取的文件夹。三、在程序代码中动态生成的控件,我们通过android系统自身提供的类和方法在代码中获取当前屏幕的密度比例因子(公式denSity/160),通过计算得到应设置的大小,公式控件实际大小=控件大小/密度比例因子。运用以上三个步骤可实现程序自适应不同屏幕密度(density),注意在res目录下创建不同的 drawable 文件夹,比如 drawable—hdpi、drawable—mdpi、drawable—ldpi,在文件夹下放不同规格的图片(即,将不同密度的图片分别存在不同文件夹名下,然后由应用程序调取并显示在屏幕上),保证不同屏幕密度下显示出来的图片效果不模糊失真。与现有技术相比,本发明的积极效果为本发明可以使Android应用程序自动适应不同的屏幕密度(density),以合适的方式展示程序内容,避免显示出现问题。


图1、本发明方法流程图;图2、res目录下创建的两个layout文件夹示意图。
具体实施例方式本发明的方法流程如图1所示,下面以应用仓库app来阐述本发明的实现方法。一、在应用仓库的布局配置文件中除了文本的大小以外,避免使用了数字,使用 android_height 禾口 android_width 等于 fill_parent 5 wrap_content, # 与 android—
weight配合实现控件的位置与大小。以main, xml文件为例,代码如下< xml version="1. O“ encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ android:lay〇ut_width= "fill—parent“ android:layout_height="fill—parent“ android:background="Qdrawable/background">
<LinearLayout android:orientation="horizontal“
android:layout width="fill parent“ android:layout height="wrap content"> <TextView android:id="@+id/title“ android:layout—width="wrap_content“
android:layout height="wrap content“ android:textColor="Qcolor/black“ android:text="Qstring/app name“ android:textSize="4O.Osp" android:gravity="centsr" android:layout weight="1.O“ /> <Button android: id= "@+id/login ” android: text= rr@string/login “ android:layout—width="wrap—content“ android:layout height="wrap content“
android: textSi ze= "18 . Osp “ android: layout gravity= 〃ce_n 亡ej “ /> <Button android: id= 〃(^+土d/reg土Stern android: text= "0string/register" android:layout—width="wrap—content“ android:layout height="wrap content“
android:textSi ze="18.Osp“ android:layout gravity="center“ /> </LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout width="fill parent” android:layout height="fill parent“ android:layout weight="1. O">
<LinearLayout android:orientation="vertical“ android:layout—width="wrap_content“ android:layout height="wrap content“
android: id= "Q-hid/category" android: gravity= "center"
android: background= "Qdrawable/border “ android: layout gravity= "ceil亡er "> </LinearLayout> <Button android:id="@+id/pre “ android:background="Qdrawable/forward backgroud” android:layout height="wrap content“android:layout width="wrap content“
android:layout gravity="center vertical“ /> <GridView android:id="@+id/mainGrid" android:IayoutAnimation="Qanim/layout wave scale"
android: layout width= "fill parent “ android: layout height= "jfill parent “ android:numColumns="5” android:layout—weight="1.O“ android:verticalSpacing="25. Odip “ /> <Button android: id= 〃{ "/"jid/iiex亡〃 android: background= rfQdra wabl e/ η ex t_ba ckground ” android:layout height="wrap content “ android:layout width="wrap content “
android:layout—gravity="center—vertical" /> </LinearLayout>
<LinearLayout android:orientation= "horizontal“
android:layout—width="fill—parent“ android:gravity="center—horizontal“ android:layout height="wrap content “ android:background="Qdrawable/bottom">
〈Button android:id="@+id/newest” android:text="Qstring/newest” android:layout width="wrap content “ android:layout height="wrap conten亡”
android: text Size= "18 . Osprr android: layout_weight= "1.0" /> 〈Button android: id= "Q-hid/nottest “ android: text= " Qstring/hottes t “ android:layout width="wrap content“ android:lay〇ut_height= ”wr彐 p—content“
android:textSize="18.Osp“ android:layout weight="1.0" /> 〈Button android: id= "β-hid/ favorite “ android: text= "0string/favorite “ android: layout—width=content “
android:layout height="wrap content “
android:textSize="18. Osp“ android:layout weight="1.O“ /> 〈Button android:id="@+id/reconunend" android:text="@string/recommend“ android:layout width="wrap content“ android:layout height="wrap content “
android: text Size= "18 . Osp'' android: layout_weight= "1. O “ /> </LinearLayout> </LinearLayout>二、在以上的代码中可以看出,字体的大小还是使用了数字,所以为了自适应不同的屏幕密度,在res目录下创建了两个layout文件夹,分别为layout和layout-hdpi,如图 2所示。layout文件夹下的main, xml文件,代码同上layout-hdpi文件夹下的main, xml文件,代码如下
< xml version="1.O“ encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ android:layout width="fill parent “android:layout_height="fill—parent“ androidbackground=“Qdrawable/background">
<LinearLayout android:orientation="horizontal“
android:layout_width="fill—parent“ android:lay〇ut_height= "wrap—content"> <TextView android: id="@+id/title" android:lay〇ut_width="wrap_content“
android:layout_height="wrap_content“ android:textColor= "Qcolor/black“ android: text="Qstring/app_name“ android: textSize="27.Osp“ android: gravity="center" android : layout weight="1.0" /> <Button android: id="Q+id/login“ android: text="Qstring/1ogin“ android:layout width="wrap content“ android:layout_height="wrap_content“
android: textSize="12.Osp“ android:layout_gravity="center" /> <Button android: id= "0+id/register" android: text= "(^string/register “ android: layout_width= ''wrap_c〇ntent “ android : layout height="wrap content"
android:textSize=.Osp“ android : layout gravity="center" /> </LinearLay〇ut>
<LinearLayout android:orientation="horizontal“
android: layout—width="fill—parent“ android:lay〇ut_height="fill—parent“ android: layout—weight="1. O">
〈LinearLayout android: orientation="vertical“ android: layout width="wrap contcnt“ android : layout height="wrap content"
android: id= "Θ+id/category1' android: gravity= "center"
android:background= 11 Qdrawable/border“ android: layout—gravity= "center"> </LinearLayout> 〈Button android: id="Q+id/pre“ android: background="Qdrawable/forward_backgroud"
android: layout height="wrap contcnt “ android : layout width="wrap content“
android: layout—gravity="center—vertical" /> <GridView android:id="@+id/mainGrid" android:IayoutAnimation="Qanim/1ayοu t_ wave_ scale"
android:layout_width="fill—parent“ android: layout—height="fill—parent“ android:numColumns="5“ android:layout_weight="1. O“ android:verticalSpacing="16. Odip " /> <Button android: id="Q+id/next“ android: background="@drawable/next_background" android:layout_height="wrap—content“ android: layout—width= 彐p_c〇nt&nt 〃
android:layout—gravity="center—vertical“ /> </LinearLayout〉
<LinearLayout android: orientation="horizontal“
android : layout width="fill parent" android : gravity="center horizontal" android:layout_height="wrap_content“ android: background= 11Qdra wable/bo ttom "><Button android:id="@+id/newest“ android:text="@string/newest“ android:layout width="wrap content“ android:layout height="wrap content“
android:textSize="12.Osp“ android:layout weight="1.0" /> <Button android:id="@+id/hottest“ android:text="@string/hot test“ android:layout width="wrap content“ android:layout height="wrap content“
android:textSize="12.Osp“ android:layout weight="1.0" /> <Button android:id="@+id/favorite“ android:text="@string/favorite“ android:layout width="wrap content“ android:layout height="wrap content“
android:textSize="12.Osp“ android:layout weight="1.0" /> <Button android:id="@+id/recommend“ android:text="@string/recommend“ android:layout width="wrap content“ android:layout height="wrap content “
android:textSize="12.Osp“ android:layout weight="1.0" /> </LinearLayout> </LinearLayout>两个文件夹下的字体大小设置不同。 三、在应用仓库程序代码中要动态生成一些控件,比如图标,图标的大小值通过计算得到,首先获取当前屏幕的密度比例因子,代码如下DisplayMetrics dm = new DisplayMetrics ();getWindowManager (). getDefaultDisplay (). getMetrics (dm);density = dm. density ;然后通过计算得到图标的大小,代码如下
resizedBitmap = ImageUtil.resizedBitmap(ImageUtil .returnBitMap{StoreRest.APP_H0ST + app.getString("logol“)),
(int) (60 / density) , (int) (60 / density));以上就是本发明在应用仓库app中的实际运用,通过此方法解决了 mdroid程序在多个不同终端设备上显示的问题。
权利要求
1.一种android软件自适应不同屏幕密度的方法,其步骤为1)判断android程序布局配置文件中控件高和宽是否能设置为变量;如果能,则将android程序布局配置文件中控件高和宽分别设置为一变量,控件高和宽的变量值设置为填充所在容器全部空间时的值或能够容纳控件内部内容大小时的值;然后根据该控件宽和高的值,以及设置的该控件比重,确定该控件的位置和大小;如果不能,则在android程序的资源文件夹res下设置若干布局文件夹,每一布局文件夹对应存储一屏幕密度值的布局配置文件;然后根据当前屏幕密度值,选择匹配的布局配置文件;2)获取当前屏幕密度值,计算android程序所需动态生成控件的大小,在当前屏幕上生成所需控件。
2.如权利要求1所述的方法,其特征在于在所述资源文件夹res下设置若干图片文件夹,每一图片文件夹对应存储同一屏幕密度规格要求的图片;然后根据当前屏幕密度值,选择匹配的图片。
3.如权利要求1或2所述的方法,其特征在于所述生成所需控件的方法为首先,获取当前屏幕密度值,根据密度值计算密度比例因子;然后,根据密度比例因子和所需控件的设置尺寸,计算得到所需控件在当前屏幕上的控件实际大小。
4.如权利要求3所述的方法,其特征在于利用当前屏幕密度值/160,计算得到所述密度比例因子。
5.如权利要求4所述的方法,其特征在于根据公式控件实际大小=控件大小/密度比例因子,计算控件实际大小。
6.如权利要求3所述的方法,其特征在于通过android程序自身提供的类和方法获取当前屏幕密度值。
7.如权利要求3所述的方法,其特征在于所述所需动态生成控件包括图标。
全文摘要
本发明公开了一种android软件自适应不同屏幕密度的方法,属于计算机软件技术领域。本方法为首先判断程序布局配置文件中控件高和宽是否能设置为变量;如果能,则将程序布局配置文件中控件高和宽分别设置为一变量,控件高和宽的变量值设置为填充所在容器全部空间时的值或能够容纳控件内部内容大小时的值;然后确定该控件的位置和大小;如果不能,则在程序的资源文件夹下设置若干布局文件夹,每一布局文件夹对应存储一屏幕密度值布局配置文件;然后根据当前屏幕密度值选择匹配的布局配置文件;最后,获取当前屏幕密度值,计算程序所需动态生成控件的大小,在当前屏幕上生成所需控件。本发明可使Android程序自动适应不同的屏幕密度。
文档编号G06F3/048GK102298495SQ201110176278
公开日2011年12月28日 申请日期2011年6月27日 优先权日2011年6月27日
发明者贾澎 申请人:上海北大方正科技电脑系统有限公司
网友询问留言 已有0条留言
  • 还没有人留言评论。精彩留言会获得点赞!
1