woshidan's loose leaf

ぼんやり勉強しています

Improving Layout Performanceを読んだ

http://developer.android.com/intl/ja/training/improving-layouts/index.html

を読んだ雑なメモ。

Optimizing Layout Hierarchies

http://developer.android.com/intl/ja/training/improving-layouts/optimizing-layout.html

  • 基本的なレイアウトを使うことがもっとも効率的という誤解

    • しかしそれぞれのウィジェットやレイアウトは追加すると初期化/配置/描写を必要とする
    • LinearLayoutのネスト => どんどんViewの階層が深くなる
      • layout_weightを使ったらそれぞれの子要素は2回大きさの計算がなされる(onMeasure()?)
      • ListViewやGridViewで効く
  • Hierarchy ViewerLayoutoptの使い方

Inspect Your Layout

  • Hierarchy Viewerでレイアウトを分析できる
    • レイアウトツリーの表示
  • ツリーに三つの○(下図1)
    • 描写にかかった時間を下図2のように表示してくれる
  • LayoutWeightのLinearLayoutを止めてRelativeLayoutになって軽い!という話

こちらも読みたい

http://developer.android.com/intl/ja/tools/performance/hierarchy-viewer/index.html

Re-using Layouts with

  • カスタムビューよりレイアウトの再利用の方が簡単かもよ、という話
    • ここではパフォーマンスとかは触れられてない

Use the include Tag

  • The root View should be exactly how you'd like it to appear in each layout to which you add this layout.
  • includeタグではandroid:layout_*属性を上書き可能
  • 何か上書きしたらandroid:layout_height and android:layout_widthも指定

Use the merge Tag

  • Root Viewを無視して子要素の身を入れたい => mergeタグ

Loading Views On Demand

http://developer.android.com/intl/ja/training/improving-layouts/loading-ondemand.html

滅多に使わない重いViewをViewStubでstubしちゃう??

ViewStubはその要素がinflateしなさい!!っていわれたときだけ、android:layoutで指定した属性をimportするっぽい

<ViewStub
    android:id="@+id/stub_import"
    android:inflatedId="@+id/panel_import"
    android:layout="@layout/progress_overlay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

一度VISIBLEにするか、inflateしたらView階層からは外れるんだって。inflateされたViewのRootViewのidはandroid:inflatedIdっぽい

ViewStubはincludeと違って、mergeタグについてはサポートしていないので注意

Making ListView Scrolling Smooth

http://developer.android.com/intl/ja/training/improving-layouts/smooth-scrolling.html

ListViewをスムーズにスクロールさせるコツはUIスレッドで重い処理をしないこと

ViewHolderパターンを使って、findViewByIdの回数を減らそう??

感想

  • 恩恵を受けやすいのでListViewやRecyclierViewの要素から最適化する
  • Hierarchy Viewerは使う前にこちらも読む
  • ViewStubははじめて知った