java - 如何在android中将渐变颜色从完全透明设置为深色?

标签 java android gradient background-color

我有一个 imageview 和一个 cardview 提升在它上面。卡片 View 中包含多个 View 。

我想为 cardview 设置背景颜色,这样它的 startcolor 应该使它后面的 imageview 的初始部分以 30% 的透明度可见,并且 cardview 应该变得越来越暗和黑色结束。

这是我目前尝试过的:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#80000000"
    android:endColor="#FF000000"
    android:angle="270"
    android:dither="true"
    />
</shape>

这不会使我的 imageview 可见。我探索了许多 SOF 帖子,但无法重现我想要的内容!

欢迎任何意见,这将非常有帮助!!

最佳答案

使用这个 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#21293B"
    android:centerColor="#21293B00"
    android:endColor="#00000000"
    android:angle="90"/>
</shape>

把它作为一个 View 的背景,把那个 View 放在你的 ImageView 之上

 <RelativeLayout
            android:id="@+id/player_layout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
<ImageView
            android:id="@+id/shasha_poster"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="500dp"
            android:scaleType="centerCrop"
            android:src="@drawable/placeholder"
            android:visibility="visible" />

<View
            android:id="@+id/gradient"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/background_with_shadow2" />
 </RelativeLayout>

关于java - 如何在android中将渐变颜色从完全透明设置为深色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56178588/

相关文章:

Java 相当于 C# Big Rational

java - 为什么当我想重新绘制我的画时 repaint() 不起作用?

android - 如何获取自定义 ListView 中每个项目的引用或 ID?

colors - 有没有一种简单的方法来设置带有渐变的文本样式?

image - 带有图像和默认渐变背景的 UINavigationBar 与 iOS5

swift - 如何修复 : gradient background at landscaping?

java - 创建单例实例的工厂方法

java - IE 11 未在 navigator.mimeTypes 中显示 Java 小程序

android - 房间数据库在 macbook pro m1 中不起作用

android - 我是否需要回收 BitmapDrawable