如何使用 android 中的毕加索库将背景图片设置为 activity 布局

How to set background image to activity layout using Picasso Library in android

我正在使用 picasso 库从服务器加载图像。我可以使用 Picasso 将背景图像设置为 ImageView。但我无法使用相同的设置背景图像。我也尝试了不同的方法将图像设置为 activity 布局背景。请帮助我。

我的代码如下:

 
Picasso.with(getApplicationContext()).load("<a href="https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg" rel="nofollow">https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg</a>").into(new Target() {</p>

        @Override
        public void onPrepareLoad(Drawable arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Start Loading", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
            // TODO Auto-generated method stub
            loginLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Failed Loading", Toast.LENGTH_SHORT).show();
        }
    });

尝试使用 setBackgroundDrawable 而不是 setBackground

@Override
    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
        // TODO Auto-generated method stub
        loginLayout.setBackgroundDrawable(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
    }

尝试使用 Target

Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                activityLayout.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bitmap));
            }
        }

        @Override
        public void onBitmapFailed(final Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(final Drawable placeHolderDrawable) {
        }
    };
    Picasso.with(getActivity()).load(url).into(target);