IT박스

0dp가 성능 향상으로 간주되는 이유는 무엇입니까?

itboxs 2020. 10. 25. 11:49
반응형

0dp가 성능 향상으로 간주되는 이유는 무엇입니까?


답변 이 질문의 끝은 말과 솔루션을 결합하여 작성되었습니다.

질문

주변을 검색했지만 Android Lint 와 일부 Eclipse 힌트에서 일부 layout_heightlayout_width값을 0dp.

예를 들어, ListView변경 제안이 있습니다.

전에

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

마찬가지로 ListView 항목 에 대한 변경을 제안했습니다 . 이것들은 변경 전후에 모두 동일하게 보이지만 이것이 성능 향상 요인 인 이유를 이해하는 데 관심이 있습니다.

누구든지 이유에 대한 설명이 있습니까? 도움이된다면 여기에 ListView.

<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>

대답

실제로 답변과 아래 참조 링크의 조합이기 때문에 여기에 답변을 넣었습니다. 내가 뭔가 잘못하면 알려주세요.

에서 0dip layout_height 또는 layouth_width와 트릭은 무엇인가?

너비높이로 작동하는 3 가지 일반적인 레이아웃 속성이 있습니다.

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

a LinearLayoutvertical 이면 자식 s ( ) layout_weight높이영향을줍니다 . to 설정 하면이 속성이 무시됩니다.ViewListViewlayout_height0dp

<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 the width of the child Views (ListView). Setting the layout_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 use 0dip, 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 to wrap_content.

Using wrap_content like I was using above will cause all child Views to be measured which will cause further CPU time. This measurement will cause your getView(...) to be called. I've now tested this and the number of times getView(...) is called is reduced dramatically.

When I was using wrap_content on two ListViews, 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 avoiding wrap_content on a ListView than it does the 0dp.

However the suggestion of 0dp does substantially improve performance because of this.


First of all you have this,

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>

Never take the ListView's height as wrap_content, that will lead into troubles. Here is the reason for that and this answer.

Further more,

I searched around but haven't found anything that really explains why Android Lint as well as some Eclipse hints suggests replacing some layout_height and layout_width values with 0dp.

Its because you are using layout_weight = "1" that means your ListView with take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp" and ListView's height will be managed by layout_weight = "1".


So when android:layout_weight is used on View X and LinearLayout is horizontal, then X's android:layout_width is simply ignored.

Similar, when android:layout_weight is used on View X and LinearLayout is vertical, then X's android:layout_height is ignored.

This actually means, that you can put anything in those ignored fields: 0dp or fill_parent or wrap_content. It doesn't matter. But it's recommended to use 0dp so View's do not do extra calculation of their height or width (which is then ignored). This small trick simply saves CPU cycles.

from :

What is the trick with 0dip layout_height or layouth_width?


as far as i know , there is a difference between using 0dp (or 0px btw, it's the same since 0 is 0 no matter what is the unit here ) and the wrap_content or fill_parent (or match_parent, it's the same).

it depends on the weight you use . if you only use weight of 1 , they all look the same , but the meaning is always different , and it is important for performance.

in order to show this , try the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/textView1" android:layout_width="match_parent"
    android:layout_height="0px" android:text="1" android:background="#ffff0000"
    android:layout_weight="1" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView2" android:layout_width="match_parent"
    android:layout_height="0px" android:text="2" android:background="#ff00ff00"
    android:layout_weight="2" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView3" android:layout_width="match_parent"
    android:layout_height="0px" android:text="3" android:background="#ff0000ff"
    android:layout_weight="3" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

</LinearLayout>

and then try to replace the 0px with match_parent . you will see that the result is very different.

usually , for both better understanding and for better performance , you would want to use 0px.


LinearLayout measures all the children according to the layout_width/layout_height values, then divides up the leftover space (which may be negative) according to the layout_weight values.

0dp is more efficient than wrap_content in this case because it's more efficient to just use zero for the original height and then split the parent's full height based on the weight than to measure the child first and then split the remainder based on the weight.

So the efficiency comes from not measuring the child. 0dp should be exactly as efficient (and produce exactly the same result) as match_parent, or 42px, or any other fixed number.


Caution re using android:layout_height="0dp"

I have found that in a ListView (with the recommended View recycling using convertView, see e.g. http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/), setting android:layout_height="0dp" for the row TextView can lead to text truncation for multi-line text content.

Whenever a TextView object that was previously used to display a text that fitted in a single line is recycled to display a longer text that needs more than one line, that text is truncated to a single line.

The problem is cured by using android:layout_height="wrap_content"

참고URL : https://stackoverflow.com/questions/12016781/why-is-0dp-considered-a-performance-enhancement

반응형