Android上的Alpha梯度

Android上的Alpha梯度,android,imageview,gradient,android-imageview,alpha-transparency,Android,Imageview,Gradient,Android Imageview,Alpha Transparency,我需要在ImageView的边缘创建一个alpha渐变。最好只使用XML 这里是解决方案 图片如下图所示 这方面的代码: 制备形状可拉深 res/drawable/gradient\u shape.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient

我需要在ImageView的边缘创建一个alpha渐变。最好只使用XML

这里是解决方案

图片如下图所示

这方面的代码

  • 制备形状可拉深
res/drawable/gradient\u shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
            android:startColor="#00FFFFFF"
            android:endColor="#FFFFFFFF"
            android:type="linear" />
</shape>
<ImageView
    android:id="@+id/photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/photo"
    android:src="@drawable/gradient_shape" />

定义布局:活动\u main.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
            android:startColor="#00FFFFFF"
            android:endColor="#FFFFFFFF"
            android:type="linear" />
</shape>
<ImageView
    android:id="@+id/photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/photo"
    android:src="@drawable/gradient_shape" />

这里的drawable/photo只是drawables文件夹中的jpeg

编辑 有关更多信息,请参阅此


完全透明。确保“渐变”下的视图实际上位于“渐变”下,而不仅仅是“渐变形状”视图

如果您有主相对视图、列表视图和视图(背景:渐变)

确保渐变视图位于列表视图的顶部,而不是侧面。如果您的渐变放在一旁,它将通过相对背景色而不是通过列表视图而透明

我希望你能理解我想说的话

透明度:

android:startColor="@null"
或者您可以使用8位十六进制颜色,如下所示:

android:startColor="#FF000000"

前两个FF用于颜色的alpha,00是完全透明的,FF不透明

如果需要无色透明,则需要对自定义ImageView进行编码

如果您的背景是静态颜色,以下是我首选的解决方法:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_image"/>

    <item>
        <shape android:shape="rectangle">
            <gradient android:startColor="#00efefef"
                android:endColor="#ffefefef"
                android:angle="270"
                android:type="linear"/>
        </shape>
    </item>
</layer-list>


@ffp27这是适合你的解决方案&与你的图像一样&请参考我的答案教程,希望这对你有所帮助。不,这是一个错误的答案。这只是将alpha/白色渐变形状覆盖在图像上。它不会以任何方式使原始图像的一部分透明。这是正确的答案,您只需要更改开始和结束颜色,但如何在imageview的每个边框中使用此效果?xml中不提供“真实”alpha渐变。添加另一层带有颜色的渐变(因为不幸的是android没有无色的纯alpha颜色代码)会污染带有颜色的渐变,在这种情况下是FFFFFF。要实现无色渐变,您可能需要为这里提到的复合阴影:也请看这里:也适用于ImageView。@Lucker10是的。这是正确的答案。非常感谢。