透明黑色渐变形状可绘制颜色代码

16

ViewPager 指示器的渐变效果

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

这是我尝试过的代码,但最终结果是完全黑色。


9
你拼错了Butler,显然有人没有管家! 嗤之以鼻 - weston
4个回答

54

这个渐变怎么样?从完全透明的顶部到底部50%透明度的黑色

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

是的,那就是我在寻找的。 - Denny Mathew
1
@MD 很高兴能帮到你!;) - Phantômaxx
你很棒。我一直在寻找它。 - Naveed Ahmad
@NaveedAhmad 谢谢,非常感谢! - Phantômaxx

3
请尝试使用这个方法:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#00000000"
        android:endColor="#FF000000"
        android:angle="270"
        android:dither="true"
     />
</shape>

只需将endColor中的FF更改为任何alpha值即可。 - FD_

1
In my case it works great:


   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:angle="270"
            android:endColor="@android:color/transparent"
            android:startColor="#000000" />
    </shape>

1

将这段代码添加到你的类中

GradientDrawable gd = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, new int[] {endColor,startColor}); gd.setCornerRadius(0f); layout.setBackgroundDrawable(gd); 希望这能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接