如何在代码中使用picasso设置背景图像 [英] How do i set background image with picasso in code

查看:158
本文介绍了如何在代码中使用picasso设置背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道毕加索将图像加载到imageview等,但我如何使用毕加索设置我的布局背景图像?请提供任何帮助。

I know picasso loads image into imageview etc but how do i set my layout background image using picasso? Please any help will be helpful.

我的代码:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);
        relativeLayout.setBackgroundResource(R.drawable.table_background);
        Picasso.with(MainActivity.this)
                .load(R.drawable.table_background)
                .resize(200, 200)
                .into(relativeLayout);
        return relativeLayout;
    }

我在这里给出了任何错误,说它无法解决。我有一个ScrollView和相对布局

What i have here gives any error saying it cannot be resolved. I have a ScrollView and relative layouts

推荐答案

使用Picasso的回调

Use callback of Picasso

    Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){

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

  @Override
  public void onBitmapFailed(final Drawable errorDrawable) {
      Log.d("TAG", "FAILED");
  }

  @Override
  public void onPrepareLoad(final Drawable placeHolderDrawable) {
      Log.d("TAG", "Prepare Load");
  }      
})

更新:

请检查这也是。@OlivierH在评论中提到。

Please check this also .As @OlivierH mentioned in the comment.

这篇关于如何在代码中使用picasso设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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