添加背景图片使用毕加索的Andr​​oid的ListView [英] add background image to android ListView using Picasso

查看:206
本文介绍了添加背景图片使用毕加索的Andr​​oid的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个背景图像添加到ListView。通常我会叫 listview.setBackground(MYIMAGE)。但图像是由服务器来了,所以我需要用毕加索加载图像到我的ListView的背景。我该怎么办呢?

I need to add a background image to a ListView. Normally I would call listview.setBackground(myImage). But the image is coming from server and so I need to use Picasso to load the image into the background of my ListView. How do I do that?

推荐答案

定义的匿名子类 com.squareup.picasso.Target

Picasso.with(yourContext)
      .load(yourImageUri)
      .into(new Target() {
        @Override
        @TargetApi(16)
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                yourListView.setBackgroundDrawable(new BitmapDrawable(bitmap));
            } else {
                yourListView.setBackground(new BitmapDrawable(getResources(), bitmap));
            }
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            // use error drawable if desired
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            // use placeholder drawable if desired
        }
    });

方案二

子类的ListView 和实施 com.squareup.picasso.Target

public class PicassoListView extends ListView implements Target {

    public PicassoListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PicassoListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    @TargetApi(16)
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(new BitmapDrawable(bitmap));
        } else {
            setBackground(new BitmapDrawable(getResources(), bitmap));
        }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        // use error drawable if desired
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        // use placeholder drawable if desired
    }

}

你这样做可以让:

Which lets you do this:

Picasso.with(yourContext)
          .load(yourImageUri)
          .into(yourListView);

这篇关于添加背景图片使用毕加索的Andr​​oid的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆