java - 如何在顶部、底部和透明中心对 View 进行渐变?

标签 java android xml gradient

Image like this

我想给这个 View 提供渐变,比如顶部大约 10% 的 ImageView ,透明中心,底部有 70% 和 30% 的渐变。 我怎样才能做到这一点

最佳答案

不要只使用带有 gradientshape,而是使用带有指定的 2 个 gradientslayer-list每个高度

API 21

android:bottom , android:top 属性表示层列表的填充,更改这些以更改要显示的渐变百分比

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:bottom="690dp"
    android:gravity="top">
    <shape android:shape="rectangle" >
        <gradient
            android:angle="270"
            android:type="linear"
            android:endColor="@android:color/transparent"
            android:startColor="#000000" />
    </shape>
</item>

<item
    android:top="610dp"
    android:gravity="bottom">
    <shape android:shape="rectangle" >
        <gradient
            android:angle="270"
            android:type="linear"
            android:endColor="#000000"
            android:startColor="@android:color/transparent" />
    </shape>
</item>

如果您的 minSDK 是 23+,那么为每个图层类型指定 height 会更容易

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:height="40dp"
    android:gravity="top">
    <shape android:shape="rectangle">
        <gradient
            android:angle="270"
            android:endColor="@android:color/transparent"
            android:startColor="#000000"
            android:type="linear" />
    </shape>
</item>

<item
    android:height="70dp"
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <gradient
            android:angle="270"
            android:endColor="#000000"
            android:startColor="@android:color/transparent"
            android:type="linear" />
    </shape>
</item>

然后将其应用为 ViewPagerbackground

试试这个,希望这能解决你的问题

关于java - 如何在顶部、底部和透明中心对 View 进行渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62891214/

相关文章:

java - 如何使用 FFmpeg 调整 yuv420sp 的大小

android - 在 Android 上使用访问 token 创建 Facebook session 对象

html - 使用正则表达式查找嵌套的 XML 标记

java - RxJava - 带有 concatWith 和 map 的可观察链

android - 为什么我们需要拉动刷新移动应用程序?

Java - 最大化屏幕=缓冲区闪烁

android - 为什么我的 ImageView 元素与我的 Button 混在一起?

c# - 是否存在任何不在解析时加载整个文件的 DOM 实现?

java - 在Java中打印文本文件的确切内容

java - ThreadPoolExecutor.shutdownNow() 没有在 Thread 中抛出 InterruptedException