【问题标题】:How do I set background image with picasso in code如何在代码中使用毕加索设置背景图像
【发布时间】:2015-06-28 22:01:53
【问题描述】:

我知道 picasso 会将图像加载到 imageview 等中,但是如何使用 picasso 设置我的布局背景图像?

我的代码:

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 和相关布局。

【问题讨论】:

    标签: java android picasso android-scrollview android-relativelayout


    【解决方案1】:

    使用毕加索的回调

        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");
      }      
    })
    

    更新:

    也请检查this。正如评论中提到的@OlivierH。

    【讨论】:

    • 小心:如this answerPicasso holds a weak reference to Target objects, hence it is likely to be garbage collected.中所述
    • @OlivierH 。我已经更新了答案。感谢您指出。
    • 另一个好的解决方案是将目标存储在视图标签中作为强引用。我在这里找到了解决方案,这对我有用:stackoverflow.com/questions/24180805/…
    • 什么是mainLayout?
    • @FernandoUrban 这是您要设置位图的布局。
    【解决方案2】:

    创建自定义转换的最佳方式,例如填充颜色:

    public class BackgroundColorTransform implements Transformation {
    
        @ColorInt int mFillColor;
    
        public BackgroundColorTransform(@ColorInt int color) {
            super();
            mFillColor = color;
        }
    
        @Override
        public Bitmap transform(Bitmap bitmap) {
            // Create another bitmap that will hold the results of the filter.
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Bitmap out = Bitmap.createBitmap(width, height, bitmap.getConfig());
            Canvas canvas = new Canvas(out);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            canvas.drawColor(mFillColor);
            canvas.drawBitmap(bitmap, 0f, 0f, paint);
            bitmap.recycle();
            return out;
        }
    
        @Override
        public String key() {
            return "BackgroundColorTransform:"+mFillColor;
        }
    
    }
    

    用法:

    mPicasso.load(imageUrl)
                   .transform(new BackgroundColorTransform(ContextCompat.getColor(getContext(),R.color.black)))
                    .into(mLogoImageView);
    

    如果您想添加 vectorDrawable 图像,请使用 Transformation :

    public class AddVectorDrawableTransform implements Transformation {
    
        private Drawable mDrawable;
        @ColorInt int mTintColor;
    
        public AddVectorDrawableTransform(Drawable drawable, @ColorInt int tintColor) {
            super();
            mDrawable = drawable;
            mTintColor = tintColor;
        }
    
        @Override
        public Bitmap transform(Bitmap bitmap) {
            // Create another bitmap that will hold the results of the filter.
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Bitmap out = Bitmap.createBitmap(width, height, bitmap.getConfig());
            Canvas canvas = new Canvas(out);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            canvas.drawBitmap(bitmap, 0f, 0f, paint);
            Drawable drawable = mDrawable.mutate();
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable, mTintColor);
            DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
    //        mDrawable.setColorFilter( mTintColor, PorterDuff.Mode.MULTIPLY);
            drawable.setBounds(width/4, height/4, 3*width/4, 3*height/4);
            drawable.draw(canvas);
            bitmap.recycle();
            return out;
        }
    
        @Override
        public String key() {
            return "AddDrawableTransform:"+mDrawable+", "+mTintColor;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多