ListView Android中的调色板与毕加索

ListView Android中的调色板与毕加索,android,listview,picasso,palette,Android,Listview,Picasso,Palette,我有一个列表视图,正在使用支持库中的调色板。使用BitmapFactory.decodeStream从url生成位图时,会引发异常(UI线程上的网络)或可能非常昂贵。如何使其异步?我想不出任何有效的方法来做这件事。最好的方法是什么 @Override public View getView(int position, View convertView, ViewGroup viewGroup) { final ViewHolder holder;

我有一个
列表视图
,正在使用支持库中的
调色板
。使用
BitmapFactory.decodeStream
从url生成位图时,会引发异常(UI线程上的网络)或可能非常昂贵。如何使其异步?我想不出任何有效的方法来做这件事。最好的方法是什么

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_grid, null);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.img_photo);
            holder.bg = (LinearLayout) convertView.findViewById(R.id.bg_title);
            holder.text = (TextView) convertView.findViewById(R.id.txt_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Picasso.with(mContext)
                .load(mShows.get(position).poster)
                .into(holder.image);

        try {
            URL url = new URL(mShows.get(position).poster);
            Bitmap bitmap = BitmapFactory.decodeStream(
                    url.openConnection().getInputStream()); // Too expensive!!!
            Palette palette = Palette.generate(bitmap);

            holder.text.setText(mShows.get(position).title);
            holder.text.setTextColor(palette.getVibrantColor().getRgb());

            holder.bg.setAlpha(0.4f);
            holder.bg.setBackgroundColor(palette.getDarkMutedColor().getRgb());

        } catch (IOException e) {
            e.printStackTrace();
        }

        return convertView;
    }

尝试在工作线程中执行网络和位图解码工作,并在获得位图时发出异步通知,同时还需要位图缓存以避免重复解码工作。 一个简单的实现如下所示:

Interface IBitmapCache {
    Bitmap get(String key);
    put(String key, Bitmap map);
}

Class YourAdapter {

    Class ViewHolder {
        //your other views
        //use url as key.
        String url;
    }

    IBitmapCache mCache;

    //you need to hold an instance of your listview here.
    WeakReference<ListView> mAttachedListView;

    View getView() {
        //... handle other things
        Bitmap bitmap = mCache.get(url);
        if (bitmap == null) {
            //retrieve bitmap asynchronous
        }
    }

    //callback when bitmap is retrieved
    void onBitmapRetrived(String url, Bitmap bitmap) {
        if (mAttachedListView.get() != null) {
            final ListView list = mAttachedListView.get();
            final int count = list.getLastVisiblePosition() - list.getFirstVisiblePosition + 1;
            for (int i = 0; i < count; i++) {
                View v = lv.getChildAt(i);
                if (v == null) {
                    continue;
                }
                ViewHolder holder = (ViewHolder) v.getTag();
                if (url.equals(holder.url)) {
                    //do your Palette related things here.
                    break;
                }
            }
        }
    }
}
接口IBitMacache{
位图获取(字符串键);
put(字符串键、位图映射);
}
类适配器{
类视图持有者{
//你的其他观点
//使用url作为键。
字符串url;
}
IBitmapCache-mCache;
//您需要在此处保存listview的一个实例。
WeakReference mAttachedListView;
查看getView(){
//…处理其他事情
位图Bitmap=mCache.get(url);
如果(位图==null){
//异步检索位图
}
}
//检索位图时回调
void onBitMapRetrieve(字符串url、位图){
if(mAttachedListView.get()!=null){
final ListView list=MattCachedListView.get();
最终整数计数=list.getLastVisiblePosition()-list.getFirstVisiblePosition+1;
for(int i=0;i

您还需要做两件事,将解码后的位图放入位图缓存,设置对工作线程的回调,并确保在UI线程上调用它,该线程可由
处理程序轻松实现

尝试在工作线程中执行网络和位图解码,并在获取位图时发出异步通知,此外,还需要位图缓存以避免重复解码工作。 一个简单的实现如下所示:

Interface IBitmapCache {
    Bitmap get(String key);
    put(String key, Bitmap map);
}

Class YourAdapter {

    Class ViewHolder {
        //your other views
        //use url as key.
        String url;
    }

    IBitmapCache mCache;

    //you need to hold an instance of your listview here.
    WeakReference<ListView> mAttachedListView;

    View getView() {
        //... handle other things
        Bitmap bitmap = mCache.get(url);
        if (bitmap == null) {
            //retrieve bitmap asynchronous
        }
    }

    //callback when bitmap is retrieved
    void onBitmapRetrived(String url, Bitmap bitmap) {
        if (mAttachedListView.get() != null) {
            final ListView list = mAttachedListView.get();
            final int count = list.getLastVisiblePosition() - list.getFirstVisiblePosition + 1;
            for (int i = 0; i < count; i++) {
                View v = lv.getChildAt(i);
                if (v == null) {
                    continue;
                }
                ViewHolder holder = (ViewHolder) v.getTag();
                if (url.equals(holder.url)) {
                    //do your Palette related things here.
                    break;
                }
            }
        }
    }
}
接口IBitMacache{
位图获取(字符串键);
put(字符串键、位图映射);
}
类适配器{
类视图持有者{
//你的其他观点
//使用url作为键。
字符串url;
}
IBitmapCache-mCache;
//您需要在此处保存listview的一个实例。
WeakReference mAttachedListView;
查看getView(){
//…处理其他事情
位图Bitmap=mCache.get(url);
如果(位图==null){
//异步检索位图
}
}
//检索位图时回调
void onBitMapRetrieve(字符串url、位图){
if(mAttachedListView.get()!=null){
final ListView list=MattCachedListView.get();
最终整数计数=list.getLastVisiblePosition()-list.getFirstVisiblePosition+1;
for(int i=0;i

您还需要做两件事,将解码后的位图放入位图缓存,设置对工作线程的回调,并确保在UI线程上调用它,该线程可由
处理程序轻松实现
您可以将毕加索的
应用到(…)
带有
回调
参数,用于图像成功加载时:

Picasso.with(mContext)
            .load(mShows.get(position).poster)
            .into(holder.image, new Callback() {

    @Override public void onSuccess() {
        Bitmap bitmap = ((BitmapDrawable)holder.image.getDrawable()).getBitmap();
        // do your processing here....
    }

    @Override public void onError() {
        // reset your views to default colors, etc.
    }

});

您可以使用毕加索的
进入(…)
并使用
回调
参数,用于图像成功加载时:

Picasso.with(mContext)
            .load(mShows.get(position).poster)
            .into(holder.image, new Callback() {

    @Override public void onSuccess() {
        Bitmap bitmap = ((BitmapDrawable)holder.image.getDrawable()).getBitmap();
        // do your processing here....
    }

    @Override public void onError() {
        // reset your views to default colors, etc.
    }

});