Why is 0dp considered a performance enhancement?
来源:互联网 发布:企业信息化管理 知乎 编辑:程序博客网 时间:2024/05/16 23:40
From: http://stackoverflow.com/questions/12016781/why-is-0dp-considered-a-performance-enhancement
An answer at the end of this question has been filled out, combining remarks and solutions.
Question
I searched around but haven't found anything that really explains why Android Lint as well as someEclipse hints suggest replacing some layout_height
and layout_width
values with 0dp
.
For example, I have a ListView
that was suggested to be changed
Before
<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"></ListView>
After
<ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></ListView>
Similarly, it suggested changes to a ListView item. These all look the same before and after the changes, but I'm interested in understanding why these are performance boosters.
Anyone have an explanation of why? If it helps, here is general layout with theListView
.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/logo_splash" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageView> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@color/background" android:layout_below="@id/logo_splash"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </ListView> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_upcoming" /> </LinearLayout> </RelativeLayout>
Answer
I'm putting in an answer here because it's really a combination of answers and referenced links below. If I'm wrong on something, do let me know.
From What is the trick with 0dip layout_height or layouth_width?
There are 3 general layout attributes that work with width and height
android:layout_height
android:layout_width
android:layout_weight
When a LinearLayout
is vertical, then the layout_weight
will effect theheight of the child View
s (ListView
). Setting thelayout_height
to 0dp
will cause this attribute to be ignored.
Example
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </ListView></LinearLayout>
When a LinearLayout
is horizontal, then the layout_weight
will effect thewidth of the child View
s (ListView
). Setting thelayout_width
to 0dp
will cause this attribute to be ignored.
Example
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ListView android:id="@android:id/list" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </ListView></LinearLayout>
The reason to want to ignore the attribute is that if you didn't ignore it, it would be used to calculate the layout which uses more CPU time.
Additionally this prevents any confusion over what the layout should look like when using a combination of the three attributes. This is highlighted by@android developer in an answer below.
Also, Android Lint and Eclipse both say to use 0dip
. From that answer below, you can use0dip
, 0dp
, 0px
, etc since a zero size is the same in any of the units.
Avoid wrap_content on ListView
From Layout_width of a ListView
If you've ever wondered why getView(...)
is called so many times like I have, it turns out to be related towrap_content
.
Using wrap_content
like I was using above will cause all child View
s to be measured which will cause further CPU time. This measurement will cause yourgetView(...)
to be called. I've now tested this and the number of timesgetView(...)
is called is reduced dramatically.
When I was using wrap_content
on two ListView
s, getView(...)
was called 3 times for each row on one ListView
and 4 times for each row on the other.
Changing this to the recommended 0dp
, getView(...)
was called only once for each row. This is quite an improvement, but has more to do with avoidingwrap_content
on a ListView
than it does the 0dp
.
However the suggestion of 0dp
does substantially improve performance because of this.
- Why is 0dp considered a performance enhancement?
- why c++ considered a bad language?为什么c++被认为是一门糟糕的语言?
- Why is a different operating system needed?
- Why is everyone in such a rush?
- Why is Linux called a monolithic kernel?
- Why is 0 < -0x80000000?
- why performance testing?
- Enhancement--Finding a project and enhancement
- Enhancement--Finding a project and enhancement
- Enhancement--Finding a project and enhancement
- WHY IS A BYTE 8 BITS? OR IS IT?
- What is a magic number? And why is it bad?
- Could not find a valid gem 'rails' (>= 0), here is why
- Why Domain Sharding is Bad News for Mobile Performance and Users
- Use a layout_height of 0dip instead of 700dp for better performance
- Use a layout_height of 0dp instead of match_parent for better performance问题说明
- Why Learning Assembly Language is Still a Good Idea
- Why Huckleberry Finn Is a Great World Novel
- Android之TabHost布局
- Matlab基础学习--------多项式及其操作
- 第9周项目5-程序填充题(2)
- autoit对PCAP包的解析
- lua调用C
- Why is 0dp considered a performance enhancement?
- Ubuntu remote login
- Tomcat-maven-plugin插件使用
- HTML——使用表格进行页面布局
- 思想者:漫谈大学生的四个LEARN
- 选择排序
- Bluetooth a2dp Codec service caps 配置信息
- Spring源代码解析(收藏)
- 第九周 项目三 输出星号图