在适配器中加载图像的最佳方法

时间:2015-01-19 13:55:18

标签: java android

我目前对我的申请有点问题。它是一个音乐播放器,除了一个烦恼外,工作得很好。当我运行我的SongAdapter时,它将使用设备上的歌曲加载大量视图。但是,这在主线程上加载太重了,因此我尝试在后台运行它。但是,这还不够快。

主要问题: 有没有快速可靠的方法将图像加载到视图中。

以下是我的SongAdapter.java的代码:

public class SongAdapter extends BaseAdapter {

    //song list and layout
    private ArrayList<Song> songs;
    private LayoutInflater songInf;

    //constructor
    public SongAdapter(Context c, ArrayList<Song> theSongs){
        songs=theSongs;
        songInf=LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //map to song layout
        RelativeLayout songLay = (RelativeLayout) songInf.inflate(R.layout.song_list_item, parent, false);

        //get title and artist views
        TextView songView               = (TextView)songLay.findViewById(R.id.titleListTextView);
        TextView artistView             = (TextView)songLay.findViewById(R.id.artistListTextView);
        RelativeLayout relativeLayout   = (RelativeLayout)songLay.findViewById(R.id.layoutSelector);
        RoundedImageView albumView      = (RoundedImageView)songLay.findViewById(R.id.albumListImageView);

        //get song using position
        Song currSong = songs.get(position);

        //get title and artist strings
        albumView.setImageBitmap( getAlbumart(currSong, parent.getContext()) );
        songView.setText(currSong.getTitle());
        artistView.setText(currSong.getArtist());

        relativeLayout.setTag( currSong.getID());

        return songLay;
    }

    public Bitmap getAlbumart(Song currentSong, Context context ) {
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.album_default);

        long albumId = (long) currentSong.getAlbumId();

        try {
            final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtworkUri, albumId);

            ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");

            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                bm = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {
            Log.d("Var log", "Error:" + e);
        }
        return bm;
    }

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Universal Image Loader旨在为图像加载,缓存和显示提供功能强大,灵活且高度可定制的工具。它提供了许多配置选项,并且可以很好地控制图像加载和缓存过程。

它对我有用,希望它能解决你的问题。

答案 1 :(得分:0)

除了使用Picasso或任何其他库加载位图。 下面是回收视图的相同代码,只需将setImageBitmap()替换为您现在使用的那个(Picasso或其他)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHodler holder = null;

    if(convertView == null){
        holder = new ViewHodler();
        convertView = songInf.inflate(R.layout.song_list_item, parent, false);
        holder.songView = (TextView)convertView.findViewById(R.id.titleListTextView);
        holder.artistView = (TextView)convertView.findViewById(R.id.artistListTextView);
        holder.albumView = (RoundedImageView)convertView.findViewById(R.id.albumListImageView);
        holder.relativeLayout   = (RelativeLayout)convertView.findViewById(R.id.layoutSelector);
        convertView.setTag(holder);
    }else{
        holder =(ViewHodler) convertView.getTag();
    }

    //get song using position
    Song currSong = songs.get(position);

    //get title and artist strings
    holder.albumView.setImageBitmap( getAlbumart(currSong, parent.getContext()) ); // replace getAlbumart() with the new way you are using
    holder.songView.setText(currSong.getTitle());
    holder.artistView.setText(currSong.getArtist());

    holder.relativeLayout.setTag( currSong.getID());

    return convertView;
}

class ViewHodler{
    TextView songView;
    TextView artistView;
    RelativeLayout relativeLayout;
    RoundedImageView albumView;
}

PS:获取RelativeLayout我的意思是R.id.layoutSelector 如果它是一个大视图,它将加载更多的内存。