2014-09-06 65 views
0

我的列表查看包含专辑封面的所有歌曲的速度非常慢,并且无论我是多么重视专辑封面的尺寸,请帮助我如何获取可索引列表以处理歌曲标题。 Android初学者和对不起英文不好。 感谢您的帮助使用albumart优化列表视图

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; 
} 
} 

回答

0

据我来说,问题就出在这里:

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

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

+0

谢谢您的帮助,我的名单是光滑犯规滞后了。 – 2014-09-07 09:39:59