java - 将渐变边缘应用到 ImageView

标签 java android xml imageview fade

有什么方法可以将垂直/水平渐变边缘应用于 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 模拟器的屏幕截图:

enter image description here

关于java - 将渐变边缘应用到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21888674/

相关文章:

java - 为什么boolean一开始就设置为false,代码对吗?

android - 在 SQLite 查询中使用变量

java - 运行应用程序时的 IntelliJ MyClass.class(没有这样的文件或目录)

java - 在克隆的 dom4j 对象上使用 Xpath 会产生不同的结果

java - 有多少数据可以存储到 Google App Engine 应用程序中?

java - 为什么在 TomEE 上远程调用 EJB 时会收到 AuthenticationException?

java - 如何使计数器每天只添加一次(android)?

android - 在其他线程中收到信息后开始新 Activity

xml - 如何让 XML IntelliSense 在 VS 2015 中工作?

php - 我正在尝试通过php函数转换xml数据,但出现解析器错误