Android如何创建带渐变效果的线条Drawable?
实现Android渐变线条的正确方案
你的代码无法显示渐变的原因是Android的shape="line"的<stroke>标签不支持<gradient>属性,渐变无法作用在线条描边上,因此只会显示纯色线条;同时多余的rotate标签也没必要保留。
改用shape="rectangle"模拟渐变线条,通过控制矩形的宽高来实现线条效果,这样渐变就能正常生效:
垂直渐变线条(对应你预期的效果)
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 宽度设为线条粗细,高度设为需要的长度 --> <size android:width="1dp" android:height="match_parent" /> <gradient android:startColor="#FFEE8D85" android:endColor="#000000" android:type="linear" /> </shape>
水平渐变线条
如果需要水平方向的渐变线条,只需交换宽高设置:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="match_parent" android:height="1dp" /> <gradient android:startColor="#FFEE8D85" android:endColor="#000000" android:type="linear" /> </shape>
这种方式利用矩形的渐变效果模拟线条,既可以正常显示渐变,也能灵活控制线条的长度和粗细,完全满足你的需求。
内容的提问来源于stack exchange,提问作者Raunak Seth
相关产品推荐
相关产品推荐

