Android ListViewを最適化し、Androidの応用効率を向上させる。


ListViewはよく使われるコントロールです。ListViewの中の各サブアイテムItemは文字列を作ってもいいし、グループコントロールでもいいです。Adapterはlistviewとデータソースの間の中間者です。
各データが可視領域に入ると、adapperのgetview()が呼び出され、具体的なデータを表すビューを返します。スクロールをタッチすると、頻繁に呼び出します。百数千のデータをサポートします。
次は各データを表示するxmlファイルです。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<ImageView android:id="@+id/icon"
android:layout_width="48dip"
android:layout_height="48dip" />
<TextView android:id="@+id/text"
android:layout_gravity="center_vertical"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content" />
</LinearLayout>
1です。最も簡単な方法は、最も遅くて実用的ではない。

public View getView(int pos, View convertView,
ViewGroup parent){
View item = mInflater.inflate(R.layout.list_item, null);
((TextView) item.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) item.findViewButId(R.id.icon)).
setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return item;
}
2です。convertviewのリサイクルビューを利用すると、効率が200%向上します。

public View getView(int pos, View convertView,
ViewGroup parent){
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.list_item, null);
}
((TextView) convertView.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) convertView.findViewButId(R.id.icon)).
setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
3です。viewholderモードを利用すると、効率が50%アップします。

static class ViewHolder {
TextView text;
ImageView icon;
}
public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(
R.id.text));
holder.icon = (ImageView) convertView.findViewButId(
R.id.icon));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
adapper更新効率比較:
1の更新は10 frames/secondに満たないです。
2の更新は30 frames/secondに近いです。
3の更新は40 frames/secondに近いです。
背景と画像
ビューの背景画像を常に全体に塗りつぶします。
1です。画像のサイズが合わないと自動的に拡大縮小されます。
2です。リアルタイムの拡大・縮小を避ける
3です。あらかじめビューのサイズに縮小しておくと良いです。

originalImage = Bitmap.createScaledBitmap(
originalImage, // 􂿕    
view.getWidth(), //     
view.getHeight(), //     
true); // 􀽮     
1の効率は25 frames/secondに近いです。
2の効率は50 frames/secondに近い。
デフォルトでは、ウィンドウに不透明な背景があります。
いらなくてもいいです。
    -一番上の階のビューは不透明です。
    - _;一番上のビューはウィンドウ全体をカバーします。

layout_width = fill_parent
layout_height = fill_parent
見えない背景を更新するのは時間の無駄です。
ウィンドウの背景を削除:
1です。エンコーディングの変更

public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.mainview);
//       
getWindow().setBackgroundDrawable(null);
...
}
2です。修正xml
まずあなたのres/values/stylis.xmlがあることを確認します。

<resources>
<style name="NoBackgroundTheme" parent="android:Theme">
<item name="android:windowBackground">@null</item>
</style>
</resources>
そしてandroid manfest.xmlを編集します。

<activity android:name="MyApplication"
android:theme="@style/NoBackgroundTheme">
...
</activity>
更新要求
画面の更新が必要な場合は、メソッドを呼び出すと、簡単で便利ですが、全体のビューを更新し、価格が高すぎます。
無効な領域を先に見つけて呼び出したほうがいいです。

invalidate(Rect dirty);
invalidate(int left, int top, int right, int
bottom);
表示とレイアウト
ウィンドウに複数のビューが含まれている場合、起動が遅すぎて、描画時間が長くなり、ユーザーインターフェースの反応速度が遅くなります。
解決方法:
1です。textviewを使う複合drawableはレベルを減らします。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:drawableLeft="@drawable/icon"/>
2です。ビューstufを使ってビューを展開します。
    xmlファイルでview stufを定義する

<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"/>
  ビューを展開する必要がある場合、

findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
//   
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();
3です。「merge」を使って中間ビューをマージします。
デフォルトでは、レイアウトファイルのルートをノードとして、親ビューに追加します。

<merge xmlns:android =
"http://schemas.android.com/apk/res/android">
<! -- Content -->
</merge>
4です。lativelayoutを使ってレベルを減らす

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="@+id/icon"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text_line1"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/icon"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text_line2"
android:layout_toRightOf="@id/icon"
android:layout_below="@id/text_line1"/>
<Checkbox android:id="@+id/star"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
5.カスタムビューを使う

class CustomView extends View {
@Override
protected void onDraw(Canvas canvas) {
//         
}
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
//        
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
6カスタムレイアウトを使う

class GridLayout extends ViewGroup {
@Override
protected void onLayout(boolean changed, int l, int t,
int r, int b) {
final int count = getChildCount();
for (int i=0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
//         
child.layout(left, top, right, bottom);
}
}
}
}
 メモリ割り当て
性能に敏感なコードの中で、javaオブジェクトの作成を避ける。
1です。測定onmeasre()
2です。レイアウトonlayout()
3です。描画ondraw()dispatch draw()
4です。事件処理ontouchevent()dispatch touchevent()
5です。adapper:getview()bindview()
強制規制(デバッグモード適用)

int prevLimit = -1;
try {
prevLimit = Debug.setAllocationLimit(0);
//           
} catch (dalvik.system.AllocationLimitError e) {
//         , Java         
Log.e(LOGTAG, e);
} finally {
Debug.setAllocationLimit(prevLimit);
}
管理されたオブジェクト:
1です。適用ソフト参照:メモリキャッシュの最適選択
2です。弱引用を適用する:メモリリークを避ける
メモリキャッシュ:

private final HashMap<String, SoftReference<T>> mCache;
public void put(String key, T value) {
mCache.put(key, new SoftReference<T>(value));
}
public T get(String key, ValueBuilder builder) {
T value = null;
SoftReferece<T> reference = mCache.get(key);
if (reference != null) {
value = reference.get();