如何模糊视图

4

我有一个带不同颜色的视图。我需要模糊该视图的背景。例如,有一个LinearLayout,在其中有一个显示一些应用程序的Grid,这个LinearLayout(Gridview容器)有颜色(红色/绿色/黑色等,没有图像)。现在我需要模糊LinearLayout的背景。

这张图片是我要实现的效果:

enter image description here

我使用Android Render脚本来完成所有操作,因为我有很多片段,每个片段都有颜色的背景,所以我认为Render是最好的选择,否则在滑动View Pager时可能会出现卡顿。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout mainLayout=(RelativeLayout)findViewById(R.id.activity_main);
    view=(View)findViewById(R.id.view);
    Bitmap blurredBitmap = blur( this,  getBitmapFromView(view) );

    view.setBackgroundDrawable( new BitmapDrawable( getResources(), blurredBitmap ) );
    mainLayout.setBackgroundResource(R.drawable.wallp);
}
public static Bitmap getBitmapFromView(View view) {
    view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.draw(canvas);
    return bitmap;
}
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);
    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

现在的问题是,如果我在LinearLayout的背景中设置颜色,则会出现以下错误:

Caused by: java.lang.IllegalArgumentException: 宽度和高度必须大于0。我们能否模糊具有颜色但没有图像的视图的背景?

这是我的视图... enter image description here


请注意,本文中的HTML标签已保留,以便在需要时使用。

如果我在LinearLayout的背景中设置颜色,那么不清楚:您想要模糊...一个颜色吗????很明显,您只能模糊图像。而且它们的大小必须大于0(我甚至会说大于1*1 px)。在我看来,“模糊”单个颜色似乎是您想要对颜色透明度进行操作。这与“模糊”效果远不相同。简而言之:“我们可以模糊具有颜色但没有图像的视图的背景吗?” 不行。 - Phantômaxx
@Rotwang 谢谢,你说得对,但情况是这样的,如果我将那种颜色的透明度设置低,那么我可以清晰地看到背景墙纸。在这里,如果用户设置不透明的颜色,那么你肯定是对的,但如果用户像我上传的图片中一样设置了透明颜色,那么我需要在GridView后面显示模糊效果(目前图片中没有)。 - M.ArslanKhan
再说一遍,不是很清楚。但如果我理解得正确的话,您想让 GridView 容器是“半透明”的,而 GridView 完全透明,就像现在一样。 - Phantômaxx
@Rotwang 在这里有一个很好的解释:https://dev59.com/K1wZ5IYBdhLWcg3wIdV8。我需要模糊视图(线性布局)后面的内容,无论线性布局是纯色还是透明颜色。 - M.ArslanKhan
1
这是关于模糊图像的问题。这就是模糊存在的意义。对于一个单一颜色进行“模糊”毫无意义。 - Phantômaxx
答案与透明度有关,而不是模糊... - c-an
2个回答

2
这可以通过以下步骤实现:
  1. 通过裁剪背景图像来提取LinearLayout的背景图像。
  2. 现在扩展LinearLayout类。

  3. 覆盖OnDraw(Canvas mCanvas)方法。

  4. 在您的自定义LinearLayout类中创建两个方法:1. DrawBitmap 2. DrawColor。

  5. 首先调用DrawBitmap函数,通过给出从ViewPager到背景图像得到的偏移量,使图像在用户滑动屏幕时移动。

  6. 最后使用您的透明度级别绘制颜色
我希望这将解决您的问题。
此处是此示例代码:查看背景模糊

-1

在调用getBitmapFromView方法时,您的视图可能尚未准备好。您可以尝试将您的方法包装起来:

view.post(new Runnable() {
    public void run(){
        Bitmap blurred = blur(YourActivity.this, getBitmapFromView(view));
        view.setBackgroundDrawable(new BitmapDrawable(getResources(), blurred));
    }
});

你可以使用blur()方法

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);
    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

希望这可以帮到你!


模糊(blur())函数从哪里来的? - c-an
@c-你可以使用问题中提供的相同的blur()方法。 - Hemant Kaushik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接