当前位置: 首页 > 面试题库 >

如何使用毕加索加载布局背景

高茂
2023-03-14
问题内容

谁能给我一个例子,说明如何使用Picasso来以编程方式更改XML布局的背景吗?我发现的所有示例都可以使用Picasso更新ImageView,但不能更新布局背景。


问题答案:

您可以使用毕加索的目标:

         Picasso.with(this).load("http://imageUrl").into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
               mYourLayout.setBackground(new BitmapDrawable(bitmap));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

更新

正如@BladeCoder在评论中提到的那样,毕加索对目标对象的引用很弱,因此很可能会被垃圾回收。

因此,在杰克·沃顿就其中一个问题发表评论之后,我认为这可能是一个不错的选择:

  CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
  Picasso.with(this).load("http://imageUrl").into(mCustomLayout);

CustomLayout.java:

public class CustomLayout extends LinearLayout implements Target {

    public CustomLayout(Context context) {
        super(context);
    }

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

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        setBackground(new BitmapDrawable(getResources(), bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        //Set your error drawable
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        //Set your placeholder
    }
}


 类似资料:
  • 问题内容: 我的API具有针对每个HTTP请求的某种验证机制。端点之一具有使用HTTP post方法加载图像的功能。发布请求主体将包含一个从服务器端验证的JSON对象。 为此,我需要在http post请求正文中包含这样的JSON。 我该如何用毕加索做到这一点? 问题答案: 我从杰克逊·成加莱先生的暗示中得到了解决方案。 创建一个Okhttp请求拦截器 创建一个Okhttp客户端,添加此拦截器 使

  • 正如你可以看到的使用链接,毕加索只有3个选项加载一个图像。我的问题是,如果您有一个从API的JSON响应解析的base64字符串,那么如何加载图像?

  • 我有下面的代码来加载毕加索的图像,在下载图像时使用可绘制的占位符来显示图像。不过,我想要的是一个动画旋转进度条样式的微调器,它可以在图像加载时不断地旋转,就像我在大多数专业应用程序中看到的那样。毕加索似乎不支持这一点,只支持静态图像绘制。有没有办法让它与毕加索合作,或者我必须做些不同的事情?

  • 从internet加载文本数据并链接到图像。我无法理解如何在listview中加载图像。阅读后,它需要使用毕加索或通用图像加载器,但不明白如何使用。请更正我的代码 }

  • 问题内容: 我知道毕加索将图像加载到imageview等中,但是如何使用毕加索设置布局背景图像? 我的代码: 我在这里遇到的任何错误都表明它无法解决。我有一个ScrollView和相对布局。 问题答案: 使用毕加索的回调 更新: 也请检查此内容。如评论中提到的@OlivierH。

  • 问题内容: 我想使用Picasso在列表视图中一个接一个地加载三个连续的图像。使用毕加索提供的方法可以轻松做到这一点。但是,由于这些图像在不同的时间加载,因此在图像进入时会产生闪烁效果。例如,有时图像2出现在图像1之前,而当图像1加载时会导致不自然的结结。如果可以将listview的可见性设置为不可见,直到可以显示所有图像,那将更好。但是,我找不到用于毕加索的回调方法,该方法会在加载图像时发出信号