Android 像应用程序 Etsy 中的模糊效果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20273171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:33:38  来源:igfitidea点击:

Blur effect like in the app Etsy?

androidetsy

提问by Jakob

During this weeks Android Design in Action episode, Adam Koch talked about an app called Etsy, which featured this very cool blur effect applying to the main layout when the Navigation Drawer is pulled out.

在本周的Android Design in Action 一集中,Adam Koch 谈到了一个名为Etsy的应用程序,当导航抽屉被拉出时,它的特点是这种非常酷的模糊效果应用于主布局。

Does anyone know how the devs behind Etsy might have implemented this?

有谁知道 Etsy 背后的开发人员是如何实现这一点的?

Watch it here: http://youtu.be/GjUxEddmjFw?t=22m48s

在这里观看:http: //youtu.be/GjUxEddmjFw?t=22m48s

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by denizmveli

There's a few parts to this:

这有几个部分:

1. Creating the blurred bitmap:

1. 创建模糊位图:

We create a downscaled bitmap from the content view behind the drawer when the drawer is first opened (the downscaling makes the blur fast). Then we apply the blur to the downscaled image using RenderScript where available (even faster). For this you should read the blog already mentioned http://nicolaspomepuy.fr/?p=18and checkout the GlassActionBar project for reference code https://github.com/ManuelPeinado/GlassActionBar.

Note:RenderScript is now available in the support library... but it can't be built using Gradle (for now). Also there are issues with ScriptIntrinsicBluron some devices notably the Nexus 10 - see Roman Nurik's explanation

当抽屉第一次打开时,我们从抽屉后面的内容视图创建一个缩小的位图(缩小使模糊更快)。然后,我们使用可用的 RenderScript(甚至更快)将模糊应用于缩小的图像。为此,您应该阅读已经提到的博客http://nicolaspomepuy.fr/?p=18并查看 GlassActionBar 项目以获取参考代码https://github.com/ManuelPeinado/GlassActionBar

注意:RenderScript 现在在支持库中可用......但它不能使用 Gradle 构建(目前)。某些设备上的ScriptIntrinsicBlur也存在问题,尤其是 Nexus 10 - 请参阅Roman Nurik 的解释

2. Showing & Animating the Blur:

2. 显示和动画模糊:

Our nav drawer layout contains a hidden ImageView that sits on top of the content.

我们的导航抽屉布局包含一个隐藏在内容顶部的 ImageView。

<FrameLayout
    android:id="@+id/nav_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- our blur image -->
<ImageView
    android:id="@+id/blur_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:visibility="gone" />

In the Activities onCreate() you'll want to grab a reference to this ImageView and setup a custom drawer listener:

在活动 onCreate() 中,您需要获取对此 ImageView 的引用并设置自定义抽屉侦听器:

mBlurImage = (ImageView) findViewById(R.id.blur_image);
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer_layout);

mDrawerToggle = new EtsyNavActionBarDrawerToggle(
            this, 
            mDrawerLayout,
            R.drawable.ic_drawer, 
            R.string.nav_drawer_open, 
            R.string.nav_drawer_closed);

mDrawerLayout.setDrawerListener(mDrawerToggle);

Our drawer has custom scrim color (that's the overlay color) instead of the default transparent black. This also hides the fact that the blurred image is on a downscaled image.

我们的抽屉有自定义的稀松布颜色(即覆盖颜色),而不是默认的透明黑色。这也隐藏了模糊图像位于缩小图像上的事实。

mDrawerLayout.setScrimColor(getResources().getColor(R.color.background_main_v2_glass));

The drawer listener which sets, clears and animates the blurred image:

设置、清除和动画模糊图像的抽屉侦听器:

private class EtsyNavActionBarDrawerToggle extends ActionBarDrawerToggle {

    public EtsyNavActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, 
            int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        super(activity, drawerLayout, drawerImageRes, openDrawerContentDescRes, closeDrawerContentDescRes);
    }

    @Override
    public void onDrawerSlide(final View drawerView, final float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);
        if (slideOffset > 0.0f) {
            setBlurAlpha(slideOffset);
        }
        else {
            clearBlurImage();
        }
    }

    @Override
    public void onDrawerClosed(View view) {
        clearBlurImage();
    }

}

private void setBlurAlpha(float slideOffset) {
    if (mBlurImage.getVisibility() != View.VISIBLE) {
        setBlurImage();
    }
    ViewHelper.setAlpha(mBlurImage, slideOffset);
}

public void setBlurImage() {
    mBlurImage.setImageBitmap(null);
    mBlurImage.setVisibility(View.VISIBLE);
    Bitmap downScaled = ... // do the downscaling
    Bitmap blurred = ... // apply the blur
    mBlurImage.setImageBitmap(blurred);
}

public void clearBlurImage() {
    mBlurImage.setVisibility(View.GONE);
    mBlurImage.setImageBitmap(null);
}

Lastly the ViewHelper is from NineOldAndroidsso we can setAlpha() pre-honeycomb.

最后,ViewHelper 来自NineOldAndroids,所以我们可以 setAlpha() pre-honeycomb。

回答by M-WaJeEh

Have a look at this blog http://nicolaspomepuy.fr/?p=18

看看这个博客http://nicolaspomepuy.fr/?p=18

Basically you generate a blur image from original one. I guess in this case original one is generated using getDrawingCache()method, make sure to call setDrawingCacheEnabled()first.

基本上,您从原始图像生成模糊图像。我猜在这种情况下,原始一个是使用getDrawingCache()方法生成的,请确保先调用setDrawingCacheEnabled()