首页 > 解决方案 > 在 Android 中对从网络下载的图像进行着色

问题描述

我必须下载图像,将图像着色,使其变暗,然后将其设置为LinearLayout. 我尝试了以下代码:

Picasso.get().load(url).into(new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
    bitmapDrawable.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN));
    linearLayout.setBackground(bitmapDrawable);
  }

  @Override
  public void onBitmapFailed(Exception e, Drawable errorDrawable) {

  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {

  }
});

问题是看不到图像,背景已经完全变黑。如何显示变暗的图像?

标签: androidimagepicasso

解决方案


Use the PorterDuff.Mode.OVERLAY:

bitmapDrawable.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.OVERLAY));

You can read how it works exactly here. Hope this helps!


推荐阅读