暗淡/模糊父布局背景

时间:2016-01-06 12:04:25

标签: android layout blur

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

我有一个布局

我需要在顶部布局中使用相同的图像作为底部,但具有模糊样式

设置alpha无效 - 文本视图也会影响

如何模糊向下布局的背景

xml here

<?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

4 个答案:

答案 0 :(得分:2)

在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>

答案 1 :(得分:2)

先生。 sisara模糊您的视图尝试此代码 为你做了一些编码

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;
    }
}

答案 2 :(得分: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);

答案 3 :(得分:0)

我发现自己最简单的解决方法:

在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>