有什么方法可以将垂直/水平渐变边缘应用于 ImageView
组件?
我已经尝试过 android:fadingEdge
但不幸的是,这个属性已被弃用,从 API 级别 14 开始将被忽略,有什么想法吗?
最佳答案
我找到了一个非常适合我的巧妙解决方法。我需要淡出 imageView 的顶部和底部(尽管这种方法适用于任何一侧,只需创建不同的渐变)。我将 ImageView
包裹在 FrameLayout
中,并将 2 个 View
放在顶部和底部,之后,我创建了带有渐变的 xml这些的背景,简单的把它们当做背景。这是它的样子:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/fade_effect_heigth"
android:layout_gravity="bottom"
android:background="@drawable/custom_gradient_bottom"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/fade_effect_heigth"
android:layout_gravity="top"
android:background="@drawable/custom_gradient_top"/>
</FrameLayout>
现在 2 个 View 位于 ImageView 之上,它们的渐变 xml 背景文件如下所示(这是顶部渐变,底部相同,只是翻转开始/结束颜色):
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:startColor="#00000000" <- black with 0 alpha, so transparent
android:endColor="#000000" <- black color
android:type="linear" />
您可以选择适合您的淡入淡出高度,我选择了 120dp,它很好地满足了我的需求。这是我的 ImageView
模拟器的屏幕截图:
关于java - 将渐变边缘应用到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21888674/