自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android:一個高效的UI才是一個拉風(fēng)的UI(一)

移動開發(fā) Android
Android是一個運行在移動終端上的操作系統(tǒng),跟傳統(tǒng)PC最大的不同所在就是移動終端的資源緊缺問題“比較”明顯,當(dāng)然對于一些屌絲機型,應(yīng)該用“非?!皝硇稳莶趴孔V。所以經(jīng)常會出現(xiàn)在一些比較缺乏青春活力的老型機上,運行一些軟件被異常終止的情況;然而作為互聯(lián)網(wǎng)廠家來說,廣大的屌絲機用戶肯定是一大筆用戶資源,這是能放棄的市場嗎?

開篇

Android是一個運行在移動終端上的操作系統(tǒng),跟傳統(tǒng)PC最大的不同所在就是移動終端的資源緊缺問題“比較”明顯,當(dāng)然對于一些屌絲機型,應(yīng)該用“非常“來形容才靠譜。所以經(jīng)常會出現(xiàn)在一些比較缺乏青春活力的老型機上,運行一些軟件被異常終止的情況;然而作為互聯(lián)網(wǎng)廠家來說,廣大的屌絲機用戶肯定是一大筆用戶資源,這是能放棄的市場嗎?!當(dāng)然不行o(╯□╰)o,所以我們要盡可能得提高軟件的效率來贏取客戶的回眸一笑了,屌絲也是客戶!

這篇博客主要介紹如何在UI設(shè)計上提高效率,減少資源的利用,畢竟在終端資源短缺的今天,效率始終為王。我們評判一個UI界面不是認為有多復(fù)雜才給力,或者說有多炫才靠譜,一個簡約而又不平凡的高效UI界面才是一個灰常牛逼的界面設(shè)計。

引入

在android應(yīng)用中,采用硬編碼方式編寫界面并不是一個提倡的方法。當(dāng)然硬編碼編寫的界面比基于XML文件的軟編碼界面高效靈活一些,但是非常不容易維護,錯綜復(fù)雜的代碼更會讓程序埋雷重重,說不定哪天就把應(yīng)用炸的慘不忍睹。所以如果非常必要非常肯定要采用代碼編寫硬編碼界面之外,其他情況還是采用易于維護的XML來編寫比較好。

所以文中對于UI優(yōu)化設(shè)計歸結(jié)到底也就是對XML布局文件的優(yōu)化設(shè)計。

在谷歌給我們的開發(fā)環(huán)境中,存在這么一個非常好用的工具——hierarchyviewer,估計很多人都沒搭理過這個藏在偏僻角落的小工具吧;它能非常容易的幫我們分析UI界面的結(jié)構(gòu)和構(gòu)造效率,這個工具的位置就在sdk/tools/文件夾。

樓下上圖:

大家好,我是圖~

這是分析的是一個布局上只有一個TextView組件的XML界面,圖告訴我們,構(gòu)造這個界面總共用了四個組件,也就是需要繪制四次組件,自然每一次繪制組件都需要耗費資源。

下面步入狂拽酷炫吊炸天的主題部分。。。。

盡量用最少的步驟完成布局

我是社會好青年,我為國家省資源;當(dāng)然作為組件來說也需要這個覺悟,每個組件的繪制都會多多少少耗費終端的資源。所以我們在這里可不能聽老祖宗的話:韓信點兵多多益善了,精兵簡政才是UI設(shè)計的唯一出路。不相信?行!下面就開始給個對比的例子。

[[121125]]

這不簡單嗎?幾行代碼不是分分鐘的事情嗎?

  1. <RelativeLayout 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:gravity="center" > 
  5.     <Button 
  6.         android:id="@+id/button1" 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:background="@drawable/btn_backgroup" 
  10.          /> 
  11.     <ImageView 
  12.         android:id="@+id/imageView1" 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" 
  15.         android:layout_alignParentLeft="true" 
  16.         android:layout_centerVertical="true" 
  17.         android:src="@drawable/header_back" /> 
  18. </RelativeLayout> 

也別急著看代碼,多累多傷眼睛呀,直接上個hierarchyviewer里面的圖來瞧瞧唄

一個小小的按鈕就用了3個組件來繪制,這就是3N的復(fù)雜度了呀,如果有5個這樣的按鈕就要15個組件,如果有10個按鈕就要有30個,如果有N++個,哎 呀媽的,不敢想象下去了。既然這樣,我們是不是應(yīng)該考慮一下優(yōu)化優(yōu)化,翻翻資料我們發(fā)現(xiàn)原來是可以不用這么多組件來實現(xiàn)的這個按鈕的。

  1. Button 
  2.     android:id="@+id/button1" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:background="@drawable/btn_backgroup" 
  6.     android:drawableLeft="@drawable/header_back" 
  7.     android:gravity="center" 
  8.     android:padding="10dp" 
  9.     />

還是原來的按鈕,還是原來的味道,復(fù)雜度從3N降低到N!?。∧愀艺f這樣的效率你不想去提升????

小結(jié)一個:在我們設(shè)計UI布局時,應(yīng)該從使用盡量少的組件的前提下入手,由于系統(tǒng)組件的封裝比較完善,把多個簡單的組件交由一個復(fù)雜一點的組件來實現(xiàn),是可以得到比較好的效率的。因為每個組件都得需要獨自進行繪制過程,多個組件繪制浪費的資源不僅僅謀害了我們的應(yīng)用,更深深打擊了用不起高端機的屌絲用戶的自尊心——”他媽的,這軟件又不能用!“。

你不干活?把你辭了。

我們還記剛開始給的一個圖嗎?我們在布局中使用的到僅僅是一個TextView,而RelativeLayout貌似啥子活兒都沒干的樣子。。。。。。

我們從來都不提倡吃空餉不干活,軟件界的潛規(guī)則也是這樣的。出于構(gòu)建和諧社會的正義感,我們當(dāng)然不能坐視RelativeLayout這種站著茅坑不拉屎的流氓行為,所以我們就需要借助一個解決措施——<merge>標(biāo)簽,它能幫我們干掉一些不需要的根節(jié)點。為了擁有更好的即視感,所以我用了一個更為復(fù)雜點的布局(其實一點都不復(fù)雜)、、

主布局XML文件:

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:id="@+id/layout1" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     > 
  6.     <ImageView android:id="@+id/image1" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:src="@drawable/bg" 
  10.         /> 
  11.     <com.net168.text.MyLayout  
  12.         android:id="@+id/layout2" 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="match_parent" 
  15.         > 
  16.     </com.net168.text.MyLayout> 
  17. </FrameLayout> 

組合控件布局XML文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:orientation="horizontal" 
  5.     > 
  6.     <Button android:id="@+id/button2" 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:text="button2" 
  10.         /> 
  11.     <TextView android:id="@+id/text1" 
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" 
  14.         android:text="text1" 
  15.         android:textColor="#ff0000" 
  16.         /> 
  17. </LinearLayout> 

這個界面很丑的,不忍直視:

丑歸丑,我們還是需要繼續(xù)用神器hierarchyviewer看看這個XML生成的界面結(jié)構(gòu)圖來探索一下丑女內(nèi)心豐富多彩的世界~~~~~~~

我靠。。。。三個組件的布局竟然用了六層嵌套布局,瞬間有了一種大花姑娘嫁給老光棍的一種深深的浪費感。我們開始看圖說話,第一層和第二層的組件是系統(tǒng)都會自動生成的,這個是板上釘釘沒法商量的事情,除非你去底層跟他們好好談?wù)劇?span style="color: #ff0000;">但是~但是這個第三層的FrameLayout和第五層的LinearLayout完完全全是在自我秀存在感而已,所以我們要狠下心做掉他們,怎么來呢?用<merge>標(biāo)簽。

由于<merge>標(biāo)簽只能作為根元素,所以我們可以將這兩個根元素都稍加修改,如下:

主布局XML文件:

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:id="@+id/layout1" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     > 
  6.     <ImageView android:id="@+id/image1" 
  7.         android:layout_width="match_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:src="@drawable/bg" 
  10.         /> 
  11.     <com.net168.text.MyLayout  
  12.         android:id="@+id/layout2" 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="match_parent" 
  15.         > 
  16.     </com.net168.text.MyLayout> 
  17. </merge> 

組合控件布局XML文件:

  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     > 
  5.     <Button android:id="@+id/button2" 
  6.         android:layout_width="wrap_content" 
  7.         android:layout_height="wrap_content" 
  8.         android:text="button2" 
  9.         /> 
  10.     <TextView android:id="@+id/text1" 
  11.         android:layout_width="wrap_content" 
  12.         android:layout_height="wrap_content" 
  13.         android:text="text1" 
  14.         android:textColor="#ff0000" 
  15.         /> 
  16. </merge> 

PS:注意需要在組合控件的類中加上一句setOrientation(LinearLayout.HORIZONTAL)來保證自組件的水平排列。

繼續(xù)用神器看看結(jié)構(gòu):

呼呼呼~~是不是從六層降低到了四層結(jié)構(gòu),好一股小清新的感覺呀,我都感覺飄飄然了,自然效率的提升是毋容置疑滴。。。。。

小結(jié)一個:<merge>標(biāo)簽?zāi)馨俜职俅?lt;FrameLayout>這個布局組件,對于不復(fù)雜的其他布局組件如線性布局等組合組件中,可以在繼承子類中對其屬性進行設(shè)置后也可以使用<merge>標(biāo)簽,<merge>標(biāo)簽不占資源,自然在生成界面時也不會生成對應(yīng)的組件。另外需要注意一點是<merge>只能作為根元素,對于需要用inflate生成布局文件時,必須指定一個ViewGroup作為其父元素,并且要設(shè)置inflate的attachToRoot參數(shù)為true。(參照inflate(int, ViewGroup, boolean))。

本文鏈接:http://www.cnblogs.com/net168/archive/2014/10/09/4004950.html

責(zé)任編輯:chenqingxiang 來源: cnblogs
相關(guān)推薦

2014-10-14 10:01:10

UIAndroid

2020-04-17 10:58:12

UI設(shè)計師按鈕

2016-10-20 19:27:00

開源項目bootstrapcss框架

2021-01-19 13:10:29

ZshLinuxUbuntu

2017-09-15 15:13:33

效果設(shè)備UI

2016-09-26 17:26:20

2016-05-18 15:13:49

快碼CTO互聯(lián)網(wǎng)創(chuàng)業(yè)

2013-10-18 10:10:23

2023-01-03 12:30:25

架構(gòu)CPUGPU

2012-05-28 15:31:57

App-UI

2014-07-17 15:38:43

UI設(shè)計師移動端

2013-03-04 16:45:49

2024-04-10 12:39:08

機器學(xué)習(xí)python

2024-11-29 12:51:18

2022-04-28 09:05:41

網(wǎng)絡(luò)爬蟲Python

2013-02-22 18:37:50

容錯服務(wù)器

2024-02-07 08:52:07

VueUI 庫Vue.js

2016-03-01 14:37:47

華為

2023-01-12 08:47:26

二項式楊輝斐波那契

2013-09-11 16:02:00

Spark分布式計算系統(tǒng)
點贊
收藏

51CTO技術(shù)棧公眾號