模糊线性布局背景 [英] Blur linear layout background

查看:63
本文介绍了模糊线性布局背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 LinearLayout ,它是透明的,并附加到 WindowManager 上.我想使该 LinearLayout 模糊,以便使 LinearLayout 后面的所有内容看起来都模糊.我从各种资料中获得了如何使 ImageView 模糊的方法,成功使用 RenderScript Api 的代码如下:

In my app I have a LinearLayout which is transparent and it is attached to WindowManager .I want to make that LinearLayout Blur so that everything behind the LinearLayout will look blurred.I had gone through various sources from that I had got how to make a ImageView blur and I had done that successfully using RenderScript Api here is the code:

 private static final float BLUR_RADIUS = 25f;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}


and finally adding it with `ImageView`

    ImageView imageView=(ImageView)findViewById(image-id);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
    Bitmap blurredBitmap = blur(bitmap);
    imageView.setImageBitmap(blurredBitmap);

但是可悲的是,它仅模糊了 ImageView .任何用于模糊布局或修改上述代码以将其与Layout结合使用的解决方案.

but sadly it blurs ImageView only.Any solution for blurring a layout or modifiying the above code to use it with Layout.

大家好,我必须知道如何成功地模糊 ImageView ,但是如何模糊透明的LinearLayout.对此是否有解决方案,请帮助我

Hi everyone I had got to know how to blur an ImageView successfully but how can I blur an transparent LinearLayout.Is there any solution for this please help me

推荐答案

我创建了一个示例项目以给出可能使用的过程的示例(

I created an example project to give an example of a possible process to use (main code).

此示例的目的是演示如何使用RenderScript来模糊通用视图.该示例的功能(当单击"Blur it!"按钮时)是:

The purpose of this example is to show how RenderScript can be used to blur a generic view. What the example does (when the button "Blur it!" gets clicked) is:

1)模糊选定的视图

对于此示例,所选视图必须包含在更大的容器(例如LinearLayout)中.

The chosen view, for this example, must be contained inside a bigger container (ex. a LinearLayout).

模糊过程:

  • 获取原始视图的屏幕截图.

  • Gets a screenshot of the original view.

Bitmap getViewScreenshot(View v) {
    v.setDrawingCacheEnabled(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    return b;
}

  • 实例化所有RenderScript分配(一个用于输入,一个用于模糊输出).

  • Instantiates all RenderScript allocations (one for input and one for blur output).

    allocOriginalScreenshot = Allocation.createFromBitmap(mRS, viewScreenshot);
    // Creates an allocation where to store the blur results
    allocBlurred = Allocation.createTyped(mRS, allocOriginalScreenshot.getType(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
    

  • 创建一个TextureView以显示模糊结果.
  • 将新的TextureView替换为原始视图.在此过程中,新视图将保存在原始视图的标签"字段中,以便以后可以用新的TextureView替换回原始视图.

  • Creates a TextureView to display the blur result.
  • Substitutes the new TextureView with the original view. In this process, the new view gets saved inside the "tag" field of the original view, so that later the original view can be substituted back with the new TextureView.

    void replaceView(View originalView, View newView) {
        originalView.setTag(newView);
    
        newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
    
        ViewGroup parent = (ViewGroup) originalView.getParent();
        int index = parent.indexOfChild(originalView);
        parent.removeView(originalView);
    
        parent.addView(newView, index);
    }   
    

  • 使用ScriptIntrinsicBlur类对屏幕截图进行模糊处理.

  • Blurs the screenshot using the ScriptIntrinsicBlur class.

    void executeBlur() {
        Log.d(TAG, "Executing blur");
    
        scriptIntrinsicBlur.setInput(allocOriginalScreenshot);
        scriptIntrinsicBlur.forEach(allocBlurred);
    
        allocBlurred.ioSend();
    }
    

  • 2)显示一个简单的对话框

    2) Display a simple dialog

    3)取消模糊原始视图

    3) Unblur the original view

    取消模糊处理将从布局中删除TextureView并还原原始View.

    The unblur process removes the TextureView from the layout and restores the original View.

    参考: RenderScript:在Android上并行计算的简便方法 查看全文

    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆