我有一个图像,上面有一个文本视图。我想知道如何模糊文本视图的背景(仅图像的一部分(以使其更具可读性。我试过看模糊库,但似乎它只能模糊整个画面。
添加此自定义 BlurView 解决方案: https://github.com/mmin18/RealtimeBlurView
然后将 BlurView 放置在布局中的 TextView 后面:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<com.github.mmin18.widget.RealtimeBlurView
android:id="@+id/textview_blurview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/my_textview"
android:layout_alignBottom="@+id/my_textview"
android:layout_alignStart="@+id/my_textview"
android:layout_alignLeft="@+id/my_textview"
android:layout_alignEnd="@+id/my_textview"
android:layout_alignRight="@+id/my_textview"/>
<TextView
android:id="@+id/my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text in front of blur view"/>
</RelativeLayout>
最后,在代码中根据需要设置模糊和颜色
blurView.setBlurRadius(80.0f);
blurView.setOverlayColor(R.color.background_blur_color);
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_height="200dp"/>
<TextView
android:layout_width="match_parent"
android:background="#10000000"
android:layout_gravity="bottom"
android:layout_height="100dp" />
<TextView
android:layout_width="wrap_content"
android:text="Text Here"
android:layout_marginBottom="50dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_height="wrap_content" />
</FrameLayout>
您还可以将文本视图背景设置为具有较低 aplha(AA <"FF"->max alpha(的某种颜色 (#AARRGGBB(,以轻松重现类似的效果。
因为您希望使文本视图位于图像视图上方时更具可读性。要做到这一点而不是模糊图像,为什么不使用渐变可绘制对象或颜色。
我在应用程序中也有同样的要求,我只是将分级视图放在 ImageView 的部分上。
让我们这样做
步骤1:创建一个渐变形状并将其放在可绘制文件夹下,假设形状名称为gradient_overlay.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#00000000"
android:startColor="#BB000000"/>
</shape>
第 2 步:创建一个虚拟视图并将其放置在 ImageView 或 ImageView 的一部分上。
<View
android:id="+@id/gradientView"
android:layout_width="match_parent"
android:layout_height="60dp" // adjust the height
android:layout_alignBottom="@id/picture" // give the id of your image
android:background="@drawable/gradient_overlay">
</View>
第三步: 然后将文本视图与渐变视图对齐。 使文本变得正确可见。