简体   繁体   English

CollapsingToolbarLayout-暗化/模糊背景图片

[英]CollapsingToolbarLayout - Dim/Blur the background image

I've got a collapsing toolbar with an imageview. 我有一个带有imageview的折叠工具栏。 When extended, it looks like this: 扩展后,它看起来像这样:

扩大了

When collapsed, it looks like this: 折叠后,它看起来像这样:

崩溃了

I know that according to the guidelines, the toolbar should be of the primary color while collapsed, but i like the way i've set it up, and would like to keep it like that. 我知道,根据指南,工具栏在折叠时应为原色,但是我喜欢我的设置方式,并希望保持这种状态。

However, obviously, the toolbar title won't be visible if the image has a lot of white in it. 但是,很明显,如果图像中有很多白色,则工具栏标题将不可见。 So, is there a way i can blur/dim the background so that the toolbar title is visible at all times? 因此,有没有一种方法可以使背景模糊/变暗,以使工具栏标题始终可见?

Edit: The image in question, is loaded from the internet using Picasso. 编辑:有问题的图像是使用Picasso从Internet加载的。

If you are using Picasso to display the image, then you can leverage the Transformation object in Picasso's builder and create a Blur effect class with something like this: 如果要使用Picasso显示图像,则可以利用Picasso的构建器中的Transformation对象,并使用以下内容创建Blur效果类:

public class BlurEffect implements Transformation {
  private static final int UP_LIMIT = 25;
  private static final int LOW_LIMIT = 1;
  protected final Context context;
  private final int blurRadius;

  public BlurEffect(Context context, int radius) {
    this.context = context;

    if (radius < LOW_LIMIT) {
      this.blurRadius = LOW_LIMIT;
    } else if (radius > UP_LIMIT) {
      this.blurRadius = UP_LIMIT;
    } else {
      this.blurRadius = radius;
    }
  }

  @Override public Bitmap transform(Bitmap source) {

    Bitmap blurredBitmap;
    blurredBitmap = Bitmap.createBitmap(source);

    RenderScript renderScript = RenderScript.create(context);

    Allocation input =
        Allocation.createFromBitmap(renderScript, source, 
Allocation.MipmapControl.MIPMAP_FULL,
            Allocation.USAGE_SCRIPT);

    Allocation output = Allocation.createTyped(renderScript, input.getType());

    ScriptIntrinsicBlur script =
        ScriptIntrinsicBlur.create(renderScript, 
Element.U8_4(renderScript));

    script.setInput(input);
    script.setRadius(blurRadius);

    script.forEach(output);
    output.copyTo(blurredBitmap);

    return blurredBitmap;
  }

  @Override public String key() {
    return "blurred";
  }
}

Then you can use it from Picasso in this way, the more value in second parameter in the constructor, then blurer: 然后,您可以通过这种方式在Picasso中使用它,在构造函数中,第二个参数中的值越大,然后模糊化:

Picasso.with(appBarImage.getContext())
      .load(track.getUrl())
      .transform(new BlurEffect(this, 10))
      .into(appBarImage);

My not optimal solution to this problem was to overlap two images, the normal and the blurred one, and change the alpha of the blurred one. 对于这个问题,我的非最佳解决方案是重叠两个图像,即正常图像和模糊图像,然后更改模糊图像的alpha。 When expanded, the alpha is 0. When collapsed its alpha is one. 展开时,alpha为0。展开时,alpha为1。 I used 我用了

AppBarLayout.addOnOffsetChangedListener

to get the scroll event. 获取滚动事件。 In my case 就我而言

 appBarLayout.addOnOffsetChangedListener((view, verticalOffset) -> {
        if (mScrollRange == 0) {
              mScrollRange = appBarLayout.getTotalScrollRange();
        }
        ViewCompat.setAlpha(mBlurredImage,
                (float) (1 - Math.pow(((mScrollRange + verticalOffset) / (float) mScrollRange), 4)));
 });

It is not ideal but it does its work 这不是理想的,但是可以完成工作

You could use Blurry library 您可以使用模糊库

Blurry is an easy blur library for Android Blurry是适用于Android的简单模糊库

// from Bitmap 
Blurry.with(context).from(bitmap).into(imageView);

//use with Picasso //与毕加索一起使用

 Blurry.with(MainActivity.this)
                .radius(10)
                .sampling(8)
                .async()
                .capture(findViewById(R.id.image_veiw))
                .into((ImageView) findViewById(R.id.image_veiw));

Ok, So, i just ended up using android:tint in the layout file in order to add a grayish tint to the image.For reference, the tint value i set is #55000000 . 好的,所以我最终在布局文件中使用了android:tint以便为图像添加灰色调。作为参考,我设置的着色值为#55000000 This seems to make the title visible even on entirely white backgrounds. 这似乎使标题即使在完全白色的背景上也可见。

You can use scrim for this. 您可以为此使用稀松布。 create gradient file in drawable folder. 在可绘制文件夹中创建渐变文件。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="-90"
    android:startColor="#00000000"
    android:centerColor="#00000000"
    android:endColor="#4d000000"
    android:type="linear" />
</shape>

After creating the above file add an extra view to your layout like this 创建上述文件后,像这样向您的布局添加一个额外的视图

<FrameLayout>
<ImageView>
<View
android:background="@drawable/nameOfAboveFile"/>
<TexTView>
</FrameLayout>

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

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