2

すべてに入る前に、まず何をしているのか説明させてください

基本的に、SDカードから画像を選択し、選択した画像のUriをデータベースに保存し、それをリスト項目のアバターとして使用するオプションをユーザーに提供しますが、アバターを持つリスト項目が多いほど、スクロールが遅くなりますリストは

リストをロードしますLoader

    @Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

    return new CursorLoader(getActivity(),BowlersDB.CONTENT_URI,
            new String[] {BowlersDB.ID,BowlersDB.FIRST_NAME,BowlersDB.LAST_NAME,BowlersDB.PHOTO_URI},null,null,BowlersDB.LAST_NAME + " COLLATE LOCALIZED ASC");
}

画像サイズを調整してテキストビューなどを設定するカスタムアダプターがあります。

@Override
    public void bindView(final View view,final Context context,final Cursor cursor){
        final int id = cursor.getInt(0);;
        final QuickContactBadge image = (QuickContactBadge)view.findViewById(R.id.quickContactBadge1);
        image.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if(!fromGame){
                    bowlerID = id;
                    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i,1);
                }
            }

        });
        String uri = cursor.getString(3);
        if(uri != null){

                    InputStream input=null;
                    try {
                        input = getActivity().getContentResolver().openInputStream(Uri.parse(cursor.getString(3)));
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = false;
                        Bitmap og = BitmapFactory.decodeStream(input,null,options);
                        int height = options.outHeight;
                        int width = options.outWidth;
                        BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options);

                        int imageHeight = options.outHeight;
                        int imageWidth = options.outWidth;
                        try {
                            input.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        float scaledHeight = ((float)imageHeight)/height;
                        float scaledWidth = ((float)imageWidth)/width;

                        Matrix matrix = new Matrix();
                        matrix.postScale(scaledWidth, scaledHeight);

                        final Bitmap resized = Bitmap.createBitmap(og, 0, 0, width, height, matrix, true);
                        try {
                            input.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                                image.setImageBitmap(resized);


                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }else{
                    image.setImageResource(R.drawable.ic_contact_picture);
                }

        TextView first = (TextView)view.findViewById(R.id.bListTextView);
        TextView last = (TextView)view.findViewById(R.id.bListTextView2);
        first.setText(cursor.getString(1));
        last.setText(cursor.getString(2));
    }

}

LoaderManagerおそらく画像のサイズ変更が非常に遅くなっていることはわかっていますが、コードのそのセクションを別のスレッドに配置しても効果はありませんでしたが、とにかくバックグラウンドですべてをロードすると思いました

ここに私の行xmlがあります

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="60dp"
android:id="@+id/names_layout"
android:background="@drawable/list_item_state">

<QuickContactBadge
    android:id="@+id/quickContactBadge1"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_contact_picture"
    style="?android:attr/quickContactBadgeStyleWindowLarge" />



<TextView
    android:id="@+id/bListTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/quickContactBadge1"
    android:background="@drawable/list_item_state"
    android:paddingLeft="20dp"
    android:paddingTop="2dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="20dp"
    android:textColor="?android:attr/textColorPrimary" />

<TextView
    android:id="@+id/bListTextView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/quickContactBadge1"
    android:layout_below="@+id/bListTextView"
    android:paddingLeft="20dp"
    android:background="@drawable/list_item_state"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="15dp"
    android:textColor="?android:attr/textColorSecondary" 
    android:layout_alignBottom="@+id/quickContactBadge1"/>

</RelativeLayout>

Quick Contact Badge画像を保持し、画像が設定されていない場合のデフォルトの画像を設定しています。

では、どうすればこれを修正できますか?

私がやろうとしていることの例は、アルバムリストにすべてのアルバムアバターがあり、スクロールに問題がないGoogleミュージックアプリです。それらは必要に応じて表示されますが、画像があるため明らかに少し異なりますサーバーからダウンロードされた可能性があります

4

2 に答える 2

2

リスト ビューの画像を遅延ロードできます。完全な例を次に示します。

http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/

于 2012-08-01T23:27:36.027 に答える
1

bindView スレッドで画像をストリーミングしていますが、これは一般的には良い考えではありません。通常、ビットマップのダウンロードとビルドは asyncTask で行います。むしろ、RoboAsyncTask で行います。イメージの URL と共に ImageView を渡すので、ダウンロードまたはビットマップの作成が完了した後、asynctask の onSuccess() でイメージが設定されます。画面に表示されるのは、他のコンテンツの直後に表示される画像です。プレースホルダー リソースを非同期タスクに渡して、ダウンロード中に表示することもできます。

アクティビティまたはフラグメントで:

    mediaManager.setImage(imageUrl, imageView, R.drawable.placeholder_image);

MediaManager で:

    @Override
public void setImage(String imageUrl, ImageView imageView, int placeholderResource) {
    new SetImageTask(context, imageUrl, imageView, placeholderResource).execute();
}

@Override
public Bitmap getImage(String imageUrl) throws IllegalStateException, IOException, SQLException {

    // put your code to fetch image here....

}

SetImageTask (RoboAsyncTask を拡張) 内:

    @Override
public void onPreExecute() {
    imageView.setImageResource(placeholderResource);
}

@Override
public Bitmap call() throws Exception {
    return mediaManager.getImage(imageUrl);
}

@Override
public void onSuccess(Bitmap bitmap) {
    imageView.setImageBitmap(bitmap);
}
于 2012-08-01T22:40:15.170 に答える