如何在Android中模糊图像并将其设置为相对布局

eulz3vhy  于 2023-03-11  发布在  Android
关注(0)|答案(5)|浏览(91)

我想模糊像这样的图像,我需要设置为RelativeLayout,它应该创建像下面给出的图像模糊图像:

我使用了github的blurry库,它使用此代码完成了我的所有工作,但我只能将其设置为Imageview,而不能设置为RelativeLayout

Blurry.with(context).capture(view).into(imageView);

所以,请给予我解决办法。

pqwbnv8z

pqwbnv8z1#

希望这能帮到你
自行保持高度和宽度

示例:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:adjustViewBounds="true"
        android:alpha="0.3"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/abcd" />
</RelativeLayout>
nbysray5

nbysray52#

您可以使用Picasso的BlurTransformation并将图像加载到自定义目标中(在您的情况下是RelativeLayout),例如

Picasso  
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .transform(new BlurTransformation(context))
    .into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
    });
iswrvxsc

iswrvxsc3#

它可以在不使用任何库文件的情况下完成,只需在drawable文件夹中创建一个图层列表XML文件并使用它。
对于模糊效果,请在drawable文件夹中创建blur.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/nature" /> <!-- change @drawable/nature with your desired image -->
    <item>
        <shape android:shape="rectangle">

<!-- You can use from 1% transparency to 100% transparency, according to your need,-->
            <solid
                android:color="#80FFFFFF"/>  <!-- Here transparency level is 80%-->
        </shape>
    </item>
    </layer-list>

现在将其用作布局文件中的背景图像。

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blur">

好了......

n3ipq98p

n3ipq98p4#

尝试www.example.com上介绍的RenderScriptFastBlur技术trickyandroid.com
http://trickyandroid.com/advanced-blurring-techniques/

shyt4zoc

shyt4zoc5#

想要更模糊的效果,请检查我用过的以下类。注意for循环-〉循环时间越长,就会变得越模糊。(对于以上问题,ImageView可以在RelativeLayout中使用)

public class BlurTransformation implements Transformation {

RenderScript rs;

public BlurTransformation(Context context) {
    super();
    rs = RenderScript.create(context);
}

@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    for (int i = 0; i < 10; i++) {

        // Allocate memory for Renderscript to work with
        Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
        Allocation output = Allocation.createTyped(rs, input.getType());

        // Load up an instance of the specific script that we want to use.
        ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setInput(input);

        // Set the blur radius
        script.setRadius(25);

        // Start the ScriptIntrinisicBlur
        script.forEach(output);

        // Copy the output to the blurred bitmap
        output.copyTo(blurredBitmap);
    }
    bitmap.recycle();

    return blurredBitmap;
}

@Override
public String key() {
    return "blur";
}
}

与毕加索一起使用:

List<Transformation> transformations = new ArrayList<>();
transformations.add(new BlurTransformation(mContext));

Picasso.get().load(Uri.parse(url))
              .tag(mContext)
              .placeholder(defaultresource)
              .transform(transformations)
              .placeholder(defaultresource)
              .into(imageview, new Callback() {
                        @Override
                        public void onSuccess() { }

                        @Override
                        public void onError(Exception e) {
                        }
                    });

希望这对你有帮助。

相关问题