麻豆精品无码国产在线播放,国产亚洲精品成人AA片新蒲金,国模无码大尺度一区二区三区,神马免费午夜福利剧场

一種android軟件自適應(yīng)不同屏幕密度的方法

文檔序號:6427313閱讀:163來源:國知局
專利名稱:一種android軟件自適應(yīng)不同屏幕密度的方法
技術(shù)領(lǐng)域
本發(fā)明涉及一種android軟件自適應(yīng)不同屏幕密度的方法,屬于計算機軟件技術(shù)領(lǐng)域。
背景技術(shù)
Android是基于Linux內(nèi)核的操作系統(tǒng),是Google公司在2007年11月5日公布的手機操作系統(tǒng)。早期由原名為"Android"的公司開發(fā),谷歌在2005年收購"Android. Inc"后,繼續(xù)進行對Android系統(tǒng)開發(fā)運營,它采用了軟件堆層(software stack,又名軟件疊層)的架構(gòu),主要分為三部分。底層Linux內(nèi)核只提供基本功能,其他的應(yīng)用軟件則由各公司自行開發(fā),部分程序以Java編寫。2011年初數(shù)據(jù)顯示,僅正式上市兩年的操作系統(tǒng)Android已經(jīng)超越稱霸十年的塞班系統(tǒng),使之躍居全球最受歡迎的智能手機平臺?,F(xiàn)在,Android系統(tǒng)不但應(yīng)用于智能手機, 也在平板電腦市場急速擴張。Android無疑是當(dāng)下最炙手可熱的手機操作系統(tǒng),在優(yōu)勢方面,Android平臺首先就是其開放性,開發(fā)的平臺允許任何移動終端廠商加入到Android聯(lián)盟中來。但正是由于 Android的開放性,使得不同移動終端廠商生產(chǎn)出來的移動終端也就不盡相同,主要體現(xiàn)在屏幕的不同。Android主要有以下幾種屏QVGA 和 WQVGA 屏 density = 120 ;HVGA 屏 density = 160 ;WVGA 屏 density = 240 ;其中VGA(Video Graphics Array)是 IBM 在 1987 年隨 PS/2 機一起推出的一種視頻傳輸標(biāo)準(zhǔn),具有分辨率高、顯示速率快、顏色豐富等優(yōu)點,在彩色顯示器領(lǐng)域得到了廣泛的應(yīng)用。QVGA(Quarter VGA)。顧名思義即VGA的四分之一尺寸,亦即在液晶屏幕(IXD) 上輸出的分辨率是 240X320 像素。WQVGA(Wide Quarter Video Graphics Array)數(shù)碼產(chǎn)品屏幕分辨率的一種,代表480X272 (寬高比16 9)或者400XM0 (寬高比5 3)的屏幕分辨率,而不是國內(nèi)廠商標(biāo)稱的480XM0。HVGA (Half-size VGA)即VGA (640*480)的一半, 分辨率為(480*320),(3 2寬高比)。WVGA(Wide VGA),其分辯率為800X480象素,是擴大了 VGA (640 X 480)的分辨率,應(yīng)用于PDA和手機等,因為很多網(wǎng)頁的寬度都是800,所以 WVGA的屏幕會更加適合于瀏覽網(wǎng)頁,可以說是未來手持設(shè)備的分辨率的大趨勢。所以大多數(shù)Android應(yīng)用程序就必須面對一個問題,怎樣能自動適應(yīng)不同的屏幕密度(density)。針對這個問題,本發(fā)明提供了一種實現(xiàn)方法,使Android應(yīng)用程序可以自動適應(yīng)不同的屏幕密度(density),以合適的方式展示程序內(nèi)容,避免顯示出現(xiàn)問題。

發(fā)明內(nèi)容
針對現(xiàn)有技術(shù)中存在的問題,本發(fā)明的目的在于提供一種android軟件自適應(yīng)不同屏幕密度的方法。 本發(fā)明的技術(shù)方案為一種android軟件自適應(yīng)不同屏幕密度的方法,其步驟為1)判斷android程序布局配置文件中控件高和寬是否能設(shè)置為變量;如果能,則將android程序布局配置文件中控件高和寬分別設(shè)置為一變量,控件高和寬的變量值設(shè)置為填充所在容器全部空間時的值或能夠容納控件內(nèi)部內(nèi)容大小時的值;然后根據(jù)該控件寬和高的值,以及設(shè)置的該控件比重,確定該控件的位置和大??;如果不能,則在android程序的資源文件夾res下設(shè)置若干布局文件夾,每一布局文件夾對應(yīng)存儲一屏幕密度值的布局配置文件;然后根據(jù)當(dāng)前屏幕密度值,選擇匹配的布局配置文件;2)獲取當(dāng)前屏幕密度值,計算android程序所需動態(tài)生成控件的大小,在當(dāng)前屏幕上生成所需控件。進一步的,所述資源文件夾res下設(shè)置若干圖片文件夾,每一圖片文件夾對應(yīng)存儲同一屏幕密度規(guī)格要求的圖片;然后根據(jù)當(dāng)前屏幕密度值,選擇匹配的圖片。進一步的,所述生成所需控件的方法為首先,獲取當(dāng)前屏幕密度值,根據(jù)密度值計算密度比例因子;然后,根據(jù)密度比例因子和所需控件的設(shè)置尺寸,計算得到所需控件在當(dāng)前屏幕上的控件實際大小。進一步的,利用當(dāng)前屏幕密度值/160,計算得到所述密度比例因子。進一步的,根據(jù)公式控件實際大小=控件大小/密度比例因子,計算控件實際大小。進一步的,通過android程序自身提供的類和方法獲取當(dāng)前屏幕密度值。進一步的,所述所需動態(tài)生成控件包括圖標(biāo)。本發(fā)明的主要內(nèi)容包括一、在編寫應(yīng)用程序的布局配置文件時(XXX. xml),避免去使用數(shù)字去設(shè)置 android—height 禾口 android—width (android—height一帛^牛白勺高,android—width 代表某一控件的寬),使用 android_height 禾口 android_width 等于 fill_parent 或 wrap_ content (艮口, 奪 android—height 禾口 android—width 值設(shè)為 fill—parent 或 wrap—content, fill_parent是強制性地使控件擴展以填充所在容器的全部空間。wrap^ontent是控件根據(jù)內(nèi)部內(nèi)容自動擴展以適應(yīng)其大小。),再與android_weight (android_weight代表控件所占的比重,就是lay0ut_Weight的值越大,所占比例也越大。Iayoutjeight的設(shè)置和 layout_height, layout_width不同的組合得出來不同的結(jié)果)配合實現(xiàn)控件的位置與大小。二、如果在布局配置文件中必須要使用數(shù)字去設(shè)置android_height和andr0id_ width,或android_textSize (android_textSize代表控件的字體大小,對于有文字顯示的控件才有此屬性),我們可以在res目錄下創(chuàng)建不同的layout文件夾(在android項目里, 主要的資源文件是放在res文件夾下的,在res文件夾下的不同文件夾存放不同的資源文件,如res/drawable下存放的是圖片文件,res/layout下存放的是屏幕布局文件等。在 java代碼中實現(xiàn)對資源的訪問),比如layout-hdpi對應(yīng)density = 240、layout-mdpi對應(yīng) density = 160、layout-1 對應(yīng) dpi = 120 (dpi 是“dot per inch” 的縮寫,每英寸像素數(shù),一般情況下的普通屏幕=Idpi是120,mdpi是160,hdpi是240。android系統(tǒng)自身很智能, 它會根據(jù)屏幕的density去選擇不同的layout-xxx文件夾下的布局配置文件。),系統(tǒng)會根據(jù)屏幕的大小自己選擇合適的layout進行使用,即當(dāng)啟動應(yīng)用程序的時候,系統(tǒng)會自己根據(jù)當(dāng)前屏幕的密度,選擇res下不同的目錄下的資源文件,例如,如果density = 160,就會選擇layout-mdpi下的對應(yīng)xml文件,如果res下沒有l(wèi)ayout-mdpi文件夾或layout-mdpi 下不存在java代碼中需要調(diào)用的xml文件,會選擇layout文件夾下的xml文件,如果還沒有l(wèi)ayout文件夾,程序會報錯。layout文件夾是系統(tǒng)默認讀取的文件夾。三、在程序代碼中動態(tài)生成的控件,我們通過android系統(tǒng)自身提供的類和方法在代碼中獲取當(dāng)前屏幕的密度比例因子(公式denSity/160),通過計算得到應(yīng)設(shè)置的大小,公式控件實際大小=控件大小/密度比例因子。運用以上三個步驟可實現(xiàn)程序自適應(yīng)不同屏幕密度(density),注意在res目錄下創(chuàng)建不同的 drawable 文件夾,比如 drawable—hdpi、drawable—mdpi、drawable—ldpi,在文件夾下放不同規(guī)格的圖片(即,將不同密度的圖片分別存在不同文件夾名下,然后由應(yīng)用程序調(diào)取并顯示在屏幕上),保證不同屏幕密度下顯示出來的圖片效果不模糊失真。與現(xiàn)有技術(shù)相比,本發(fā)明的積極效果為本發(fā)明可以使Android應(yīng)用程序自動適應(yīng)不同的屏幕密度(density),以合適的方式展示程序內(nèi)容,避免顯示出現(xiàn)問題。


圖1、本發(fā)明方法流程圖;圖2、res目錄下創(chuàng)建的兩個layout文件夾示意圖。
具體實施例方式本發(fā)明的方法流程如圖1所示,下面以應(yīng)用倉庫app來闡述本發(fā)明的實現(xiàn)方法。一、在應(yīng)用倉庫的布局配置文件中除了文本的大小以外,避免使用了數(shù)字,使用 android_height 禾口 android_width 等于 fill_parent 5 wrap_content, # 與 android—
weight配合實現(xiàn)控件的位置與大小。以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>二、在以上的代碼中可以看出,字體的大小還是使用了數(shù)字,所以為了自適應(yīng)不同的屏幕密度,在res目錄下創(chuàng)建了兩個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>兩個文件夾下的字體大小設(shè)置不同。 三、在應(yīng)用倉庫程序代碼中要動態(tài)生成一些控件,比如圖標(biāo),圖標(biāo)的大小值通過計算得到,首先獲取當(dāng)前屏幕的密度比例因子,代碼如下DisplayMetrics dm = new DisplayMetrics ();getWindowManager (). getDefaultDisplay (). getMetrics (dm);density = dm. density ;然后通過計算得到圖標(biāo)的大小,代碼如下
resizedBitmap = ImageUtil.resizedBitmap(ImageUtil .returnBitMap{StoreRest.APP_H0ST + app.getString("logol“)),
(int) (60 / density) , (int) (60 / density));以上就是本發(fā)明在應(yīng)用倉庫app中的實際運用,通過此方法解決了 mdroid程序在多個不同終端設(shè)備上顯示的問題。
權(quán)利要求
1.一種android軟件自適應(yīng)不同屏幕密度的方法,其步驟為1)判斷android程序布局配置文件中控件高和寬是否能設(shè)置為變量;如果能,則將android程序布局配置文件中控件高和寬分別設(shè)置為一變量,控件高和寬的變量值設(shè)置為填充所在容器全部空間時的值或能夠容納控件內(nèi)部內(nèi)容大小時的值;然后根據(jù)該控件寬和高的值,以及設(shè)置的該控件比重,確定該控件的位置和大小;如果不能,則在android程序的資源文件夾res下設(shè)置若干布局文件夾,每一布局文件夾對應(yīng)存儲一屏幕密度值的布局配置文件;然后根據(jù)當(dāng)前屏幕密度值,選擇匹配的布局配置文件;2)獲取當(dāng)前屏幕密度值,計算android程序所需動態(tài)生成控件的大小,在當(dāng)前屏幕上生成所需控件。
2.如權(quán)利要求1所述的方法,其特征在于在所述資源文件夾res下設(shè)置若干圖片文件夾,每一圖片文件夾對應(yīng)存儲同一屏幕密度規(guī)格要求的圖片;然后根據(jù)當(dāng)前屏幕密度值,選擇匹配的圖片。
3.如權(quán)利要求1或2所述的方法,其特征在于所述生成所需控件的方法為首先,獲取當(dāng)前屏幕密度值,根據(jù)密度值計算密度比例因子;然后,根據(jù)密度比例因子和所需控件的設(shè)置尺寸,計算得到所需控件在當(dāng)前屏幕上的控件實際大小。
4.如權(quán)利要求3所述的方法,其特征在于利用當(dāng)前屏幕密度值/160,計算得到所述密度比例因子。
5.如權(quán)利要求4所述的方法,其特征在于根據(jù)公式控件實際大小=控件大小/密度比例因子,計算控件實際大小。
6.如權(quán)利要求3所述的方法,其特征在于通過android程序自身提供的類和方法獲取當(dāng)前屏幕密度值。
7.如權(quán)利要求3所述的方法,其特征在于所述所需動態(tài)生成控件包括圖標(biāo)。
全文摘要
本發(fā)明公開了一種android軟件自適應(yīng)不同屏幕密度的方法,屬于計算機軟件技術(shù)領(lǐng)域。本方法為首先判斷程序布局配置文件中控件高和寬是否能設(shè)置為變量;如果能,則將程序布局配置文件中控件高和寬分別設(shè)置為一變量,控件高和寬的變量值設(shè)置為填充所在容器全部空間時的值或能夠容納控件內(nèi)部內(nèi)容大小時的值;然后確定該控件的位置和大?。蝗绻荒?,則在程序的資源文件夾下設(shè)置若干布局文件夾,每一布局文件夾對應(yīng)存儲一屏幕密度值布局配置文件;然后根據(jù)當(dāng)前屏幕密度值選擇匹配的布局配置文件;最后,獲取當(dāng)前屏幕密度值,計算程序所需動態(tài)生成控件的大小,在當(dāng)前屏幕上生成所需控件。本發(fā)明可使Android程序自動適應(yīng)不同的屏幕密度。
文檔編號G06F3/048GK102298495SQ201110176278
公開日2011年12月28日 申請日期2011年6月27日 優(yōu)先權(quán)日2011年6月27日
發(fā)明者賈澎 申請人:上海北大方正科技電腦系統(tǒng)有限公司
網(wǎng)友詢問留言 已有0條留言
  • 還沒有人留言評論。精彩留言會獲得點贊!
1
主站蜘蛛池模板: 图木舒克市| 宜宾市| 黎平县| 延庆县| 呼图壁县| 古蔺县| 五家渠市| 石棉县| 应用必备| 兴海县| 小金县| 勐海县| 龙江县| 桂平市| 林州市| 天台县| 连江县| 鹤岗市| 福海县| 万宁市| 江陵县| 连云港市| 文安县| 中西区| 上犹县| 北宁市| 类乌齐县| 永州市| 林口县| 定边县| 永丰县| 裕民县| 大余县| 永胜县| 印江| 曲周县| 永顺县| 无极县| 诏安县| 南江县| 清远市|