1

アプリを再設計した後、新しいビューの作成時に読み込み時間が長くなりました (古いバージョンには画像がほとんどなく、はるかに高速でした)。ViewFlipper を使用してビューをナビゲートします。作成される最初の 2 つのビューは、ListViews を含む 2 つのレイアウトであり、その要素には背景画像とその他のグラフィックスがあります。偶然にも、新しいビューが作成されてビュー スタックに配置されるたびに、アプリは既存の各ビューのサイズを計算することがわかりました (とりわけ、ListView アダプターの getView() メソッドを数回呼び出します)。各呼び出しのトレース ログは次のようになります。

LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) 行: 1369 LinearLayout.measureVertical(int, int) 行: 660 LinearLayout.onMeasure(int, int) 行: 553 LinearLayout(View).measure(int, int) 行: 12937 TabHost(ViewGroup).measureChildWithMargins(View, int, int, int, int) 行: 5045 TabHost(FrameLayout).onMeasure(int, int) 行: 293 TabHost(View).measure(int, int)行: 12937 RelativeLayout.measureChildHorizo​​ntal(View, RelativeLayout$LayoutParams, int, int) 行: 594 RelativeLayout.onMeasure(int, int) 行: 376 RelativeLayout(View).measure(int, int) 行: 12937 FrameLayout(ViewGroup). measureChildWithMargins(View, int, int, int, int) 行: 5045 FrameLayout.onMeasure(int, int) 行: 293 FrameLayout(View).measure(int, int) 行: 12937 LinearLayout(ViewGroup).measureChildWithMargins(View, int) 、int、int、int) 行:5045 LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) 行: 1369 LinearLayout.measureVertical(int, int) 行: 660 LinearLayout.onMeasure(int, int) 行: 553 LinearLayout(View).measure(int , int) 行: 12937 PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) 行: 5045 PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) 行: 293 PhoneWindow$DecorView.onMeasure( int, int) 行: 2180 PhoneWindow$DecorView(View).measure(int, int) 行: 12937onMeasure(int, int) 行: 293 PhoneWindow$DecorView.onMeasure(int, int) 行: 2180 PhoneWindow$DecorView(View).measure(int, int) 行: 12937onMeasure(int, int) 行: 293 PhoneWindow$DecorView.onMeasure(int, int) 行: 2180 PhoneWindow$DecorView(View).measure(int, int) 行: 12937

などなど… アプリが遅くなったのはそれが原因だと思います。アプリがこれらの対策を実行しないようにする、またはアプリを高速化するにはどうすればよいですか?

ListAdapter getView():

    public View getView(int pos, View convertView, ViewGroup parent) {

    View row;
    Entry entry = null;
    if (objects.get(pos) instanceof Entry) {
        entry = (Entry) objects.get(pos) ;

        Boolean isGroupedEntry;
        if (entry.getGroup() != null && entry.isGroupedEntry()) {
            isGroupedEntry = true;
        } else {
            isGroupedEntry = false;
        }

        if (convertView != null) {
            row = convertView;
        } else {
            row = new EntryBoxFrameLayout(getContext());
        }

        ((EntryBoxFrameLayout) row).configureFor(entry);

    } else {
        row = new View(ContentManager.getInstance().getContext());
    }

    row.setSelected(false);
    row.setTag(pos);
    return row;
}

configureFor() で、いくつかのテキストを設定してから:

        if (entry.getUserIsSignedUp() > 0) {
        this.setBackgroundColor("4A4A4A");
        this.findViewById(R.id.star).setVisibility(View.VISIBLE);
        this.findViewById(R.id.entry_face).setBackgroundResource(R.drawable.entry_face_loggedin);
        fulldate.setTextColor(Color.BLACK);
    } else {
        this.setBackgroundColor(entry.getColorHex());
        this.findViewById(R.id.star).setVisibility(View.INVISIBLE);
        this.findViewById(R.id.entry_face).setBackgroundResource(R.drawable.entry_face);
        fulldate.setTextColor(Color.WHITE);
    }

    if (entry.getGroup() != null) {
        this.findViewById(R.id.imgGroupArrow).setVisibility(View.VISIBLE);
        if (entry.isGroupExpanded()) {
            ((ImageView)this.findViewById(R.id.imgGroupArrow)).setImageResource(R.drawable.entry_up_arrow);
        } else {
            ((ImageView)this.findViewById(R.id.imgGroupArrow)).setImageResource(R.drawable.entry_down_arrow);
        }
        this.setGroupContainer(true);
    } else {
        this.findViewById(R.id.imgGroupArrow).setVisibility(View.INVISIBLE);
        this.setGroupContainer(false);
    }

    String filename = entry.getCacheFilename();
    Drawable entryimage = null;
    if (filename != null) {
        try {
            FileInputStream input = ContentManager.getInstance().getContext().openFileInput(filename);
            entryimage = new BitmapDrawable(ContentManager.getInstance().getContext().getResources(), input);
            input.close();
        } catch (Exception e) {
            Log.e(e.getLocalizedMessage());
        }
    }

    if (entryimage != null) {
        this.getImageView().setImageDrawable(entryimage);
    } else {
        this.getImageView().setImageResource(R.drawable.entry_placeholder);
    }
4

1 に答える 1

1

getView() メソッドがアプリケーションの速度を低下させています。listView は、パフォーマンスのためにビュー オブジェクトをリサイクルします。

これをカバーする多くの学習リソースがあります。

Google I/O カンファレンスと listView の作成者から: Romain Guy

画像に関する最適化

その他のコード例:

  • 最適化のための Google リストビュー
  • リストビューの遅延読み込みのための Google
  • コンバートビュー、ビューホルダーリストビューのGoogle
于 2013-01-15T15:58:07.663 に答える