【问题标题】:optimize list view with albumart [closed]使用相册优化列表视图 [关闭]
【发布时间】:2014-10-31 08:35:57
【问题描述】:

我的 ListView 包含专辑封面的所有歌曲非常慢,无论我调整专辑封面的大小,它都会滞后 请帮助以及如何获得可索引列表以处理歌曲标题。 Android 初学者,抱歉英语不好。 感谢您的帮助

public class SongAdapter extends CursorAdapter implements SectionIndexer{
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";



private final LayoutInflater mInflater;


 public SongAdapter(Context context, Cursor c, int textViewResourceId,
        List<String> objects) {
    super(context, c,textViewResourceId);

    mInflater=LayoutInflater.from(context);

}
 private Bitmap bitmap = null;
 private BitmapDrawable drawable = null;



@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView title1 = (TextView) view.findViewById(R.id.titlelist);
    TextView artist1 = (TextView) view.findViewById(R.id.artistlist);
    ImageView album1 = (ImageView) view.findViewById(R.id.iconlist);

    String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
    String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
    String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
    long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
      StringBuilder titleBuild = new StringBuilder();
      titleBuild.append(title);
      if(titleBuild.length() > 35)
      {
      titleBuild.setLength(32);
      title = titleBuild.toString()+"...";
      }
      else
      {
          title = titleBuild.toString();
      }
      StringBuilder artistBuild = new StringBuilder();
      artistBuild.append(artist);
      if(artistBuild.length() > 35)
      {
      artistBuild.setLength(32);
      artist = artistBuild.toString()+"...";
      }
      else
      {
      artist = artistBuild.toString();
      }




      final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);
        ContentResolver res = context.getContentResolver();
          InputStream in;

            try {
                if(bitmap != null)
                {
                    bitmap = null;
                    if(drawable != null)
                    {
                        drawable = null;
                    }
                }
                in = res.openInputStream(albumArtUri);
                bitmap = BitmapFactory.decodeStream(in);
                Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
                // bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
                drawable = new BitmapDrawable(context.getResources(), resizedBitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.default_artwork);
            }

album1.setImageDrawable(drawable);
title1.setText(title);
artist1.setText(artist);
}







@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater)context.getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);
    return inflater.inflate(R.layout.rowlayout, parent, false);
}@Override
public int getPositionForSection(int section) {
    // If there is no item for current section, previous section will be selected
    for (int i = section; i >= 0; i--) {
        for (int j = 0; j < getCount(); j++) {
            if (i == 0) {
                // For numeric section
                for (int k = 0; k <= 9; k++) {
                    if (StringMatcher.match(String.valueOf(( getItem(j))), String.valueOf(k)))
                        return j;
                }
            } else {
                if (StringMatcher.match(String.valueOf(getItem(j)), String.valueOf(mSections.charAt(i))))
                    return j;
            }
        }
    }
    return 0;
}

@Override
public int getSectionForPosition(int position) {
    return 0;
}

@Override
public Object[] getSections() {
    String[] sections = new String[mSections.length()];
    for (int i = 0; i < mSections.length(); i++)
        sections[i] = String.valueOf(mSections.charAt(i));
    return sections;
}
}

【问题讨论】:

    标签: android listview optimization bitmap


    【解决方案1】:

    在我看来,问题出在:

     in = res.openInputStream(albumArtUri);
     bitmap = BitmapFactory.decodeStream(in);
     Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
    

    您应该在后台线程中而不是在主线程中执行上述操作,否则它们会阻塞主 (UI) 线程并使一切无响应。

    另外,不要将全分辨率位图直接加载到内存中,而是考虑先使用BitmapFactory.Options 对其进行下采样。

    在此处阅读更多信息:http://developer.android.com/training/displaying-bitmaps/index.html

    你也可以考虑使用这个很棒的图片加载库(支持内容提供者 uris):https://github.com/nostra13/Android-Universal-Image-Loader

    【讨论】:

    • 感谢您的帮助,我的列表很流畅,不再滞后。
    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多