如何在安卓设备上模糊背景图片

79

什么是模糊背景图像的最佳方法,如下图所示?我看到了一些代码和库,但它们都已经过时了,或者像BlurBehind库那样,效果不太一样。

enter image description here


大多数人只是自己开发或使用现有的库。这就是我建议的做法。您可以很容易地在网上找到快速的盒状模糊实现。或者,如果您不需要动态模糊效果,可以预先渲染模糊图像。 - William Morrison
你看过这个吗:https://dev59.com/iWw15IYBdhLWcg3wJ4cR?对我来说非常有效。 - StephenG
有两种方法可以实现。1)您可以使用FrameLayout,然后设置模糊背景。2)您可以使用我拥有的最新的Blur库! - Piyush
谢谢@StephenG,你的回答帮了我很多。 - heloisasim
Kotlin 版本供需要的人使用:请查看此 Github 存储库。 - Hamed Jaliliani
14个回答

-1

您可以创建一个背景颜色为黑色的视图,并将视图的透明度设置为0.7或根据您的需求进行调整。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/onboardingimg1">
    <View
        android:id="@+id/opacityFilter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:layout_alignParentBottom="true"
        android:alpha="0.7">
    </View>


</RelativeLayout>

17
它不能像模糊效果一样产生同样的效果。 - heloisasim
1
此外,这会模糊作为子项添加的文本视图。 - Munib
模糊效果与不透明度效果是不同的。 - mdikici

-1

尝试以下代码.. 将此代码放入On Create中..

 if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy =
                    new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
       Url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTIur0ueOsmVmFVmAA-SxcCT7bTodZb3eCNbiShIiP9qWCWk3mDfw";
//        Picasso.with(getContext()).load(Url).into(img_profile);
//        Picasso.with(getContext()).load(Url).into(img_c_profile);

        bitmap=getBitmapFromURL(Url);
        Bitmap blurred = blurRenderScript(bitmap, 12);//second parametre is radius
        img_profile.setImageBitmap(blurred);

创建以下方法... 只需复制粘贴...
 public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }
    @SuppressLint("NewApi")
    private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

        try {
            smallBitmap = RGB565toARGB888(smallBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Bitmap bitmap = Bitmap.createBitmap(
                smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.ARGB_8888);

        RenderScript renderScript = RenderScript.create(getActivity());

        Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
        Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
                Element.U8_4(renderScript));
        blur.setInput(blurInput);
        blur.setRadius(radius); // radius must be 0 < r <= 25
        blur.forEach(blurOutput);

        blurOutput.copyTo(bitmap);
        renderScript.destroy();

        return bitmap;

    }

    private Bitmap RGB565toARGB888(Bitmap img) throws Exception {
        int numPixels = img.getWidth() * img.getHeight();
        int[] pixels = new int[numPixels];

        //Get JPEG pixels.  Each int is the color values for one pixel.
        img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

        //Create a Bitmap of the appropriate format.
        Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

        //Set RGB pixels.
        result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
        return result;
    }

-4

这可能是一个很晚的回复,但我希望能对有所帮助。

  1. 您可以使用第三方库,如RenderScript/Blurry等。
  2. 如果您不想使用任何第三方库,请使用alpha执行以下操作(将alpha设置为0表示完全模糊,1表示与现有内容相同)。

注意(如果您正在使用2号点):在将背景alpha设置为0时,它将使整个布局变得模糊。为了避免这种情况,创建一个新的xml,其中包含drawable,并在此处将alpha设置为0.5(或您希望的值),并将此drawable名称(文件名)用作背景。

例如,将其用作以下方式(假设文件名为bgndblur.xml):

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shape="rectangle"
android:src="@drawable/registerscreenbackground" 
android:alpha="0.5">

在你的布局中使用以下代码:

<....
 android:background="@drawable/bgndblur">

希望这有所帮助。

2
这不是模糊。 - Tom

-5

您可以通过以下方式快速实现模糊效果。

// 将此代码添加到 build.gradle app 文件中 //

Compile ' com.github.jgabrielfreitas:BlurImageView:1.0.1 '

// 添加到XML

<com.jgbrielfreitas.core.BlurImageView
    android:id="@+id/iv_blur_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

//添加至java

Import com.jgabrielfreitas.core.BlueImageView;

// 在公共类 *活动名称* 下 //

BlurImageView myBlurImage;

// 在OnCreate下方//

myBlurImage = (ImageView) findViewById(R.id.iv_blur_image)
MyBlurImage.setBlue(5)

希望这能帮到某个人


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