将父布局背景变暗/模糊化

3

这里有与以上主题相同的问题,但没有得到答案。

我这里有一个布局。

我需要在下面的布局中使用与顶部布局相同的图像,但具有模糊样式。

设置透明度没有帮助-文本视图也受影响。

如何模糊下面的布局背景。

XML代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#ffffff">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="105"
        android:orientation="vertical">
/// top layout with  image 
        <LinearLayout
            android:background="@drawable/s"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="45">



        </LinearLayout>


        ////  other set of layouts that i want to add the same iamge but in blur style
        <LinearLayout
            android:alpha="220"

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:weightSum="75"
            android:orientation="vertical">



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="25">

<TextView
    android:id="@+id/brand_name"
    android:layout_marginTop="10dp"
    android:textSize="25dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SHAKY ISLES"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

                <ImageView
                    android:id="@+id/image_icon"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:background="#000000"
                    android:layout_below="@+id/brand_name"
                    android:layout_centerHorizontal="true" />

                <TextView

                    android:text="  6 - 502 m"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/image_icon"
                    android:layout_alignRight="@+id/brand_name"
                    android:layout_alignEnd="@+id/brand_name"
                    android:layout_below="@+id/brand_name" />

            </RelativeLayout>

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="20">

                <TextView
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:id="@+id/discription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here. "
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />

                <View
                    android:id="@+id/boder"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#000000"></View>




            </RelativeLayout>

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="30"
               >

                <TextView
                    android:id="@+id/address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="22, Customs St E ,Auckland"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />

                <View
                    android:id="@+id/down_border"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#000000"></View>

            </RelativeLayout>



        </LinearLayout>

    </LinearLayout>



</LinearLayout>

需要帮忙吗?

即使没有XML代码,也可以提供帮助。


你想要模糊一个背景吗? - Aditay Kaushal
它就像是两个布局,顶部一个包含图片,第二个我已经在XML上添加了注释,也包括子布局。因此,我想要模糊背景图像,而不影响子布局中的文本视图,但子布局也会受到那个模糊的背景影响。 - Sisara Ranasinghe
4个回答

5

尝试在xml中使用以下代码

android:alpha="0.5"

或在代码中

yourView.getBackground().setAlpha(100);

尝试使用这段代码。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#ffffff">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="105"
    android:orientation="vertical">
    /// top layout with  image
    <LinearLayout
        android:background="@drawable/s"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="45">



    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:weightSum="75"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
           android:alpha="0.5"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/s"/>

        <LinearLayout


            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:weightSum="75"
            android:orientation="vertical">



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="25">

                <TextView
                    android:id="@+id/brand_name"
                    android:layout_marginTop="10dp"
                    android:textColor="@color/BlackColor"
                    android:textSize="25dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SHAKY ISLES"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

                <ImageView
                    android:id="@+id/image_icon"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:background="#000000"
                    android:layout_below="@+id/brand_name"
                    android:layout_centerHorizontal="true" />

                <TextView

                    android:text="  6 - 502 m"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/image_icon"
                    android:layout_alignRight="@+id/brand_name"
                    android:layout_alignEnd="@+id/brand_name"
                    android:layout_below="@+id/brand_name" />

            </RelativeLayout>

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="20">

                <TextView
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:id="@+id/discription"
                    android:layout_width="wrap_content"
                    android:textColor="@color/BlackColor"
                    android:layout_height="wrap_content"
                    android:text="22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here.22, Customs St E ,Auckland and discription here. "
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />

                <View
                    android:id="@+id/boder"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#000000"></View>




            </RelativeLayout>

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="30"
                >

                <TextView
                    android:id="@+id/address"
                    android:layout_width="wrap_content"
                    android:textColor="@color/BlackColor"
                    android:layout_height="wrap_content"
                    android:text="22, Customs St E ,Auckland"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />

                <View
                    android:id="@+id/down_border"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#000000"></View>

            </RelativeLayout>



        </LinearLayout>

    </FrameLayout>

    ////  other set of layouts that i want to add the same iamge but in blur style

</LinearLayout>


设置 alpha 属性也会影响到文本视图,不是吗? - Sisara Ranasinghe
@SisaraRanasinghe 我修改了一些代码,试试看。它可以工作。好的。 - Vishal Thakkar
@SisaraRanasinghe 你好亲爱的,你有没有尝试过编程?请告诉我。 - Vishal Thakkar
1
@SisaraRanasinghe,我认为使用XML应用模糊效果而不需要代码是更好的方式。如果可以通过XML实现,为什么我们还要写这么多行代码呢? - Vishal Thakkar
Vishal Halani - 你如何只使用 XML 实现模糊效果? - Sisara Ranasinghe
显示剩余3条评论

5

如果您想模糊您的视图,请尝试以下代码。我已经为您编写了一些代码。

public class MainActivity extends Activity {
    private ImageView imViewAndroid;

    private TextView discriptionn;
    private TextView statusText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        imViewAndroid = (ImageView) findViewById(R.id.image);

        discriptionn = (TextView) findViewById(R.id.discription);
        statusText = addStatusText((ViewGroup) findViewById(R.id.relative_layout_down));
        applyBlur();


    }

    private void applyBlur() {
        imViewAndroid.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                imViewAndroid.getViewTreeObserver().removeOnPreDrawListener(this);
                imViewAndroid.buildDrawingCache();

                Bitmap bmp = imViewAndroid.getDrawingCache();
                blur(bmp, discriptionn);
                return true;
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void blur(Bitmap bkg, View view) {
        long startMs = System.currentTimeMillis();

        float radius = 20;

        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
                (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(overlay);

        canvas.translate(-view.getLeft(), -view.getTop());
        canvas.drawBitmap(bkg, 0, 0, null);

        RenderScript rs = RenderScript.create(MainActivity.this);

        Allocation overlayAlloc = Allocation.createFromBitmap(
                rs, overlay);

        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
                rs, overlayAlloc.getElement());

        blur.setInput(overlayAlloc);

        blur.setRadius(radius);

        blur.forEach(overlayAlloc);

        overlayAlloc.copyTo(overlay);

        view.setBackground(new BitmapDrawable(
                getResources(), overlay));

        rs.destroy();
        statusText.setText(System.currentTimeMillis() - startMs + "ms");
    }

    @Override
    public String toString() {
        return "RenderScript";
    }

    private TextView addStatusText(ViewGroup container) {
        TextView result = new TextView(MainActivity.this);
        result.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        result.setTextColor(0xFFFFFFFF);
        container.addView(result);
        return result;
    }
}

1
我找到的最简单的解决方法是:
在您的XML布局文件中使用FrameLayout。将高度和宽度设置为match_parent,然后在其中使用alpha值。在父级布局标签下方写入Framelayout。
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Add_Detail"
    tools:showIn="@layout/activity_add__detail">

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/niki"
        android:alpha="0.1"
        />
....

</RelativeLayout>

0

我使用过它,对我来说真的很有帮助。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/select_country_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gray_background"
    tools:context=".startupscreen.controller.SelectCountryActivity">



</RelativeLayout>

创建一个背景XML。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#636262"/>
        </shape>
    </item>

</selector>

现在在Java文件中终于完成了

 relativeLayout = findViewById(R.id.select_country_main);
        relativeLayout.getBackground().setAlpha(200);

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