繁体   English   中英

如何在Android中使用Picasso库将背景图像设置为活动布局

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

我正在使用picasso库从服务器加载图像。 我可以使用Picasso将背景图像设置为ImageView。 但是我不能使用相同的设置背景图像。 我也尝试了其他方法来将Image设置为活动布局背景。 朴素的帮助我。

我的代码如下:


Picasso.with(getApplicationContext()).load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg").into(new Target() {

@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 = 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM