前面幾篇文章在前面幾篇文章當(dāng)中霹菊,
我們學(xué)習(xí)了如何通過合理管理內(nèi)存矮燎,app的優(yōu)化啟動的方式來提升應(yīng)用程序的性能。實際上界面布局也會對應(yīng)用程序的性能產(chǎn)生比較大的影響蚣常,如果布局寫得嵌套多,重復(fù)布局多次出現(xiàn),一個小的布局利用很多控件來實現(xiàn)的話,那么程序加載UI的速度就會非常慢奋渔,從而造成不好的用戶體驗。壮啊,如何通過優(yōu)化布局來提供應(yīng)用程序的性能嫉鲸??他巨?
本文通過示例,為大家講解一下减江。
總體原則:少用染突,復(fù)用,減少嵌套辈灼!
減少布局層次份企,加快渲染速度
View的數(shù)量減少伴隨著的就是層級的減少。從而達(dá)到結(jié)構(gòu)清晰巡莹,渲染速度快的效果司志。
當(dāng)線性布局LinearLayout和相對布局都能使用時,優(yōu)先使用線性布局LinearLayout降宅,因為RelativeLayout會讓子View調(diào)用至少2次onMeasure骂远,LinearLayout有weight時,才會讓子多次調(diào)用onMeasure腰根。Measure的耗時越長那么繪制效率就低激才。(下圖可以看打印日志,代碼是重寫控件的onMeasure,在super()下加句打尤衬铡)
盡量避免RelativeLayout嵌套RelativeLayout
重用布局文件
是有些時候我們可能需要反復(fù)利用某個已經(jīng)寫好的布局劣挫,總是使用復(fù)制粘貼的方式來進行布局重用,這顯然是一種很笨的做法东帅。而Android當(dāng)然也已經(jīng)充分考慮到了布局重用的重要性压固,于是提供了< include >和< merge >這兩個非常有用的標(biāo)簽,下面我們來學(xué)習(xí)一下靠闭。
重用< include >
新建一個hotanddown.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="213dp"
android:layout_height="284dp"
>
<TextView
android:id="@+id/hotplayer_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="@drawable/hotplayer"
android:clickable="false"
android:focusable="false"
android:gravity="bottom|center_horizontal"
android:paddingBottom="15dp"
android:textColor="@color/colorWhite"
android:textSize="24sp"/>
<TextView
android:id="@+id/hotplayer_ione"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/hotplayer_image"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/first"
android:drawablePadding="10dp"
android:maxLines="1"
android:ellipsize="end"
android:focusable="false"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="真正男子漢"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<ImageView
android:id="@+id/conner_one"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/hotplayer_ione"
android:clickable="false"
android:focusable="false"
android:src="@drawable/parting_line"/>
<TextView
android:id="@+id/hotplayer_itwo"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/conner_one"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/second"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="爸爸去哪兒"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<ImageView
android:id="@+id/conner_two"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/hotplayer_itwo"
android:clickable="false"
android:focusable="false"
android:src="@drawable/parting_line"/>
<TextView
android:id="@+id/hotplayer_ithree"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_below="@+id/conner_two"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/third"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="如果窩啦牛有愛情"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
</RelativeLayout>
Activity布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shanlovana.rcimageview.Main2Activity">
<include
android:id="@+id/layoutone"
layout="@layout/hotanddown"/>
<!--如果我設(shè)置了id帐我,那么就會代替了里面layout的父布局的id,必須先獲得這個xml布局文件阎毅,
再通過布局文件findViewById來獲得其子控件
那么如何給里面的控件設(shè)置圖片文字呢焚刚?
View layout = getLayoutInflater().inflate(R.layout.head, null);
RelativeLayout head= (RelativeLayout)layout.findViewById(R.id.index_linear_foot);
//設(shè)置背景圖片
head.setBackgroundResource(R.drawable.head);
-->
</RelativeLayout>
< merge/>
< merge/>主要用來去除不必要的FrameLayout。它的使用最理想的情況就是你的根布局是FrameLayout扇调,同時沒有使用background等屬性矿咕。這時可以直接替換。因為我們布局外層就是FrameLayout狼钮,直接“合并”碳柱。
下面的圖片是對比include和merge的布局層數(shù)比對,可以看到merge去除不必要的最外層父布局熬芜。
TextView同時顯示文字和圖片
用TextView同時顯示圖片和文字
上面的例子基本全是莲镣,自己看嘍。
當(dāng)然EditView等也一樣的涎拉,還有屬性drawableBottom和drawableTop供你使用瑞侮。同時利用代碼setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)可以讓我們動態(tài)去設(shè)置圖片。
使用TextView的行間距和 \n
直接給布局文件和效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/hotplayer_ione"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/hotplayer_image"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/first"
android:drawablePadding="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:focusable="false"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:lineSpacingExtra="8dp"
android:text="真正男子漢:wo \n漢字南鄭振:ni \n得道者多助:還是我"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
</LinearLayout>
是不是能少用兩個TextView和一個imageview鼓拧?半火??
其中:lineSpacingExtra屬性代表的是行間距季俩,他默認(rèn)是0钮糖,是一個絕對高度值。同時還有l(wèi)ineSpacingMultiplier屬性酌住,它代表行間距倍數(shù)店归,默認(rèn)為1.0f,是一個相對高度值酪我。
使用Spannable或Html.fromHtml
如果實現(xiàn)上圖紅框中的效果消痛,笨辦法就是寫三個TextView,“¥”都哭,“價格”肄满,“門市價”分別實現(xiàn)谴古,其實用一個TextVIew就可以實現(xiàn).
大概的寫法就是這樣
- Spannable示例
String text = String.format("¥%1$s 門市價:¥%2$s", 18.6, 22);
int z = text.lastIndexOf("門"); SpannableStringBuilder
style = new SpannableStringBuilder(text);
style.setSpan(new AbsoluteSizeSpan(DisplayUtil.dip2px(mContext,14)), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//字號
style.setSpan(new ForegroundColorSpan(Color.parseColor("#afafaf")), z, text.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//顏色
style.setSpan(new AbsoluteSizeSpan(DisplayUtil.dip2px(mContext,14)), z, text.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//字號
tv.setText(style);
- Html.fromHtml示例
實現(xiàn)代碼:
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView1 = (TextView) findViewById(R.id.werq);
TextView textView2 = (TextView) findViewById(R.id.hjklfjh);
LinearLayout activityMain2LinearLayout = (LinearLayout) findViewById(R.id.activity_main2);
textView1.setText(Html.fromHtml("北京市發(fā)布霾黃色預(yù)警,<font color='#ff0000'><big><big>外出攜帶好</big></big></font>口罩"));
//設(shè)置字體大小為3級標(biāo)題稠歉,設(shè)置字體為紅色
textView2.setText(Html.fromHtml("北京市發(fā)布霾黃色預(yù)警掰担,<h3><font color='#ff0000'>外出攜帶好</font></h3>口罩"));
}
}
總結(jié):
//第一種方法:Spannable
//使用步驟:
SpannableString spannable = new SpannableString(str);
// SpannableStringBuilder spannable = new SpannableStringBuilder(str);
//創(chuàng)建各類Span
CharacterStyle span=new UnderlineSpan();
spannable.setSpan(span,start,end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//可以連續(xù)設(shè)置span
view.setText(spannable);
void android.text.SpannableString.setSpan(Object what, int start, int end, int flags)
//setSpan會將start到end這間的文本設(shè)置成創(chuàng)建的span格式。span可以是圖片格式怒炸。
各類Span示例
new URLSpan("http://www.baidu.com")
new BackgroundColorSpan(Color.RED)
new ForegroundColorSpan(Color.YELLOW)
new StyleSpan(android.graphics.Typeface.BOLD_ITALIC)
new UnderlineSpan();
new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
/*第二種方法:Html.fromHtml()
只顯示帶文本的html可以用下面的方法處理html文件带饱。*/
public static Spanned fromHtml (String source)
//顯示帶圖片的html要用下面的方法處理html文件。
public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
ImageGetter 為處理html中<img>的處理器阅羹,生成Drawable對象并返回勺疼。
//創(chuàng)建ImageGetter 主要實現(xiàn)下面的方法,source為<img>標(biāo)簽中src屬性的值捏鱼。
public Drawable getDrawable(String source)
ViewStub僅在需要時才加載布局
ViewStub是一個輕量級的View执庐,不占布局位置,占用資源非常小导梆。
mainlayout.xml布局:
Mainactivity中的寫法:
ViewStub stub = (ViewStub) findViewById(R.id.main_contain);
stub.inflate();
ImageView image = (ImageView) findViewById(R.id.viewstub_img);
image.setImageResource(R.drawable.happy_running_dog);
線性布局自帶的分割線
修改過后的hotanddown.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="213dp"
android:layout_height="284dp"
android:divider="@drawable/parting_line"
android:showDividers="middle"
android:orientation="vertical"
>
<TextView
android:id="@+id/hotplayer_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="@drawable/hotplayer"
android:clickable="false"
android:focusable="false"
android:gravity="bottom|center_horizontal"
android:paddingBottom="15dp"
android:textColor="@color/colorWhite"
android:textSize="24sp"/>
<TextView
android:id="@+id/hotplayer_ione"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/hotplayer_image"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/first"
android:drawablePadding="10dp"
android:maxLines="1"
android:ellipsize="end"
android:focusable="false"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="真正男子漢"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<TextView
android:id="@+id/hotplayer_itwo"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/conner_one"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/second"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="爸爸去哪兒"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<TextView
android:id="@+id/hotplayer_ithree"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_below="@+id/conner_two"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/third"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="如果窩啦牛有愛情"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
</LinearLayout>
相比文章開篇的那個布局文件實現(xiàn)的效果相同轨淌,但是減少了三個ImageView,少用即加速看尼。
Space控件
Space:空間的意思递鹉,表示該控件占據(jù)一定的空間,但是卻不顯示任何東西藏斩。
還是去優(yōu)化原來的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="213dp"
android:layout_height="284dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/hotplayer_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:background="@drawable/hotplayer"
android:clickable="false"
android:focusable="false"
android:gravity="bottom|center_horizontal"
android:paddingBottom="15dp"
android:textColor="@color/colorWhite"
android:textSize="24sp"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:id="@+id/hotplayer_ione"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/hotplayer_image"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/first"
android:drawablePadding="10dp"
android:maxLines="1"
android:ellipsize="end"
android:focusable="false"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="真正男子漢"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:id="@+id/hotplayer_itwo"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_below="@+id/conner_one"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/second"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="爸爸去哪兒"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:id="@+id/hotplayer_ithree"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_below="@+id/conner_two"
android:background="#1D4468"
android:clickable="false"
android:drawableLeft="@drawable/third"
android:drawablePadding="10dp"
android:focusable="false"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="如果窩啦牛有愛情"
android:textColor="@color/colorWhite"
android:textSize="18sp"/>
</LinearLayout>
效果圖奉上:
總結(jié):以上就是有關(guān)布局優(yōu)化的一些技巧躏结,具體的使用場景還要根據(jù)項目具體需求而定!不貼代碼了狰域,都是很簡單的實現(xiàn)