mmin18 / RealtimeBlurView

A realtime blurring overlay for Android (like iOS UIVisualEffectView)
Other
3.17k stars 343 forks source link

Home Fragment will not show any more when back #40

Open KunMinX opened 4 years ago

KunMinX commented 4 years ago

使用遇到问题,请教一下作者:

项目目前使用的是 单 Activity 架构,所有页面均在 Fragment 中实现。

我在 MainFragment 的底栏使用了这个 blurView,在 MainFragment 去往 二级页面,并返回的时候,Main 页面不再显示任何内容。用手滑动屏幕,会看到内容隐约在闪烁,松开手则什么也不能看见。

看 Profiler,一切似乎正常,没有什么波动。看 logcat,也没有什么错误信息。

KunMinX commented 4 years ago
public void draw(Canvas canvas) {
        if (mIsRendering) {
            // Quit here, don't draw views above me
            throw STOP_EXCEPTION;
        } else if (RENDERING_COUNT > 0) {
            // Doesn't support blurview overlap on another blurview
        } else {
            super.draw(canvas);
        }
    }

从 二级页面返回一级的时候,会走 这个,然后 throw STOP_EXCEPTION。这是唯一能发现的线索,但 logcat 本身没任何消息。

KunMinX commented 4 years ago

经过排查,和 fragment 转场动画有关,关掉转场动画即可。

KunMinX commented 4 years ago

目前试了一下,除了渐变的 fade 动画,其他转场动画都会导致返回时不显示内容。 所以,有没有提供 api,使得 blur view 可以手动关闭和恢复毛玻璃呢?

KunMinX commented 4 years ago

现在改用 多 Activity 架构了。就是统一地在原来 fragment 的基础上套一层通用的 Activity,来独占 window 资源。

nukix commented 4 years ago

我使用的时候, 在页面A使用, 跳转页面B, 再回来页面A模糊效果也没了。 后来排查了一下, 是 RealtimeBlurView 里面的 blur 方法有问题, 继承 RealtimeBlurView, 重写 blur 方法, 就解决了, 不知道你的问题是不是和我的一样。

KunMinX commented 4 years ago

@nukix 哈哈,倒不是,我是 页面A 的内容全部不显示了。我通过为每个 fragment 套一个 Activity(申请到独立的 window)解决了。

JackieHou commented 4 years ago

@nukix 重写 blur 方法, 就解决了,重写的方法是怎么样写的?能贴一下代码吗

JackieHou commented 4 years ago

没有找到原因,我这只能简单粗暴的去解决了 @Override public void showBlurView() { if (blurView == null) { //blurView.setVisibility(View.VISIBLE); blurView = new RealtimeBlurView(this,null); blurView.setBlurRadius(20); blurView.setOverlayColor(Color.TRANSPARENT); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); mainFrameLayout.addView(blurView,params); }

}

@Override
public void hideBlurView() {
    if (blurView != null) {
        //blurView.setVisibility(View.GONE);
        mainFrameLayout.removeView(blurView);
        blurView = null;
    }
}
nukix commented 4 years ago

我不清楚你遇到什么问题, 我只是重写 blur 用高斯模糊。 因为我 Debug 的时候, 发现回去原本的页面, 这个方法获得的模糊图变成了全0的透明图, 导致模糊失效了。

    @Override
    protected void blur(Bitmap bitmapToBlur, Bitmap blurredBitmap) {
        // super.blur(bitmapToBlur, blurredBitmap);
        RenderScript renderScript =  RenderScript.create(getContext());

        // Allocate memory for Renderscript to work with
        final Allocation input = Allocation.createFromBitmap(renderScript, bitmapToBlur);
        final Allocation output = Allocation.createTyped(renderScript,input.getType());
        // Load up an instance of the specific script that we want to use.
        ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        scriptIntrinsicBlur.setInput(input);
        // Set the blur radius
        scriptIntrinsicBlur.setRadius(25);
        // Start the ScriptIntrinisicBlur
        scriptIntrinsicBlur.forEach(output);
        // Copy the output to the blurred bitmap
        output.copyTo(blurredBitmap);
        renderScript.destroy();
    }