【问题标题】:How to set gradient color from complete transparency to dark in android?如何在android中将渐变颜色从完全透明设置为黑暗?
【发布时间】:2019-10-04 07:46:47
【问题描述】:

我有一个 imageview 和一个 cardview 提升在它上面。卡片视图内部包含多个视图。

我想为 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>

这不会使我的图像视图可见。我浏览了许多 SOF 帖子,但无法重现我想要的内容!

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

【问题讨论】:

  • 抱歉,找不到你想要的,你能提供一个示例图片吗?
  • 80 用于 0.5 alpha 。少用Opacity\ .
  • 你能分享任何你想构建的演示图像吗?
  • 你做的渐变,放那个卡片视图的背景
  • 抱歉,回复晚了,感谢您的输入,不幸的是我没有图像,但我可以通过修改 Ezaldeen sahb 的答案来复制它。

标签: java android gradient background-color


【解决方案1】:

使用此 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>

【讨论】:

  • 如何使用白色、浅灰色、浅灰色、浅黑色、全黑的背景来修改你的渐变色。在这种情况下,我们有几个中心颜色!有什么想法吗?
  • 我希望这个工具可以做出你想要的,你可以编辑参数直到找到适合你的“使用alpha编辑颜色透明度”,第二个URl将帮助你了解Alpha十六进制颜色代码angrytools.com/gradient, gist.github.com/lopspower/03fb1cc0ac9f32ef38f4
  • 感谢您的意见!
【解决方案2】:

你可以这样做

// this is your back image
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/xxxxx"
    ...
    />

// your cardview over the image
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:cardBackgroundColor="#00000000"    // make cardview transparent
    ...
    >

    // an image to make the new drawable background
    <ImageView
        android:background="@drawable/gradient"  // the xml with your gradient
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        />

</android.support.v7.widget.CardView>

【讨论】:

    【解决方案3】:

    我认为这就是您要寻找的东西,我对此进行了测试并按您的预期工作。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/your_image" />
    
        <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            card_view:cardBackgroundColor="@color/transparent"
            card_view:cardCornerRadius="5dp"
            card_view:cardElevation="0dp"
            card_view:cardMaxElevation="0dp"
            card_view:cardPreventCornerOverlap="false"
            card_view:cardUseCompatPadding="true"
            card_view:contentPaddingTop="2dp">
    
            <RelativeLayout
                android:id="@+id/list_container_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/your_gradient"
                android:gravity="center"
                android:orientation="vertical">
    
                <!--Add your cardview contents-->
    
    
            </RelativeLayout>
    
    
        </android.support.v7.widget.CardView>
    
    </FrameLayout>
    

    这是渐变背景文件your_gradient.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#80000000"
            android:endColor="#FF000000"
            android:angle="270"
            android:dither="true"
            />
    </shape>
    

    【讨论】:

    • 感谢@Dulanga 的回答,但这仅向我显示渐变颜色,最初不显示图像视图。可能是我没有正确传达。 Ezaldeen sahb 是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多