2012-08-01 120 views
2

让我解释一下我做的第一之前,我到一切列表视图滚动是在ListView加载图像后很慢

基本上我给用户一个选项,选择从SD卡中的图片,存储在乌里在我的数据库中选择图像并具有作为头像列表项,但有头像多个列表项有较慢的列表的滚动是

我有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"); 
} 
加载列表

我有一个自定义广告更易,调整图像大小设置textviews等

@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持有的形象和我有,如果没有图像设置的默认图像设置。

那么我该如何解决这个问题?

我试图做的一个例子是谷歌音乐应用程序,其中专辑列表具有所有这些专辑头像,并且滚动没有问题,它们似乎根据需要似乎但显然它有点不同因为图像可能是从服务器上下载的

+0

这是一个常见的问题。你需要做一些懒加载和缓存图像以获得良好的性能。 – 2012-08-01 23:35:48

回答

1

您正在流式传输bindView线程中的图像,这通常不是一个好主意。我通常会在asyncTask中下载和构建位图 - 或者说,在RoboAsyncTask中。我将ImageView与图像URL一起传递,因此在下载或位图创建完成后,在asynctask的onSuccess()中设置图像。您在屏幕上看到的图像是在其他内容出现后不久出现的图像。您还可以将占位符资源传递给异步任务,以在下载完成时显示。

在您的活动或片段:

mediaManager.setImage(imageUrl, imageView, R.drawable.placeholder_image); 
在媒体管理

@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); 
} 
+0

我可以看看这个可能的例子吗? – tyczj 2012-08-01 22:46:49