OStack程序员社区-中国程序员成长平台

标题: android - 如何创建带有圆形边缘的对角渐变的自定义可绘制对象? [打印本页]

作者: 菜鸟教程小白    时间: 2022-11-6 16:17
标题: android - 如何创建带有圆形边缘的对角渐变的自定义可绘制对象?

我正在尝试使用 xml 制作自定义可绘制对象,如附图 enter image description here

以下是我做过的两种方法,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="30dp" />
<solid android:color="@color/colorAccent" />
<stroke
    android:width="2dp"
    android:color="@color/colorAccent" />
<gradient
    android:angle="135"
    android:endColor="#000"
    android:startColor="#ffff"
    android:type="linear" />
</shape>

这样我可以得到正确的效果,但是颜色似乎被合并了,我想要两种颜色没有任何合并效果,然后我尝试这样,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white">
    <shape android:shape="oval">
        <corners android:radius="@dimen/size_30dp" />
    </shape>
</item>
<item>
    <rotate
        android:fromDegrees="35"
        android:pivotX="0%"
        android:pivotY="100%">
        <shape android:shape="rectangle">
            <solid android:color="@color/textColorGreyExtraLight" />
        </shape>
    </rotate>
</item>
</layer-list>

这种方法实际上搞砸了 UI,而且我在圆角上也有妥协,

那么我有什么方法可以使用 XML 来绘制这种效果吗?

我们将不胜感激任何形式的帮助。

仅供引用drawable的宽度和高度会有所不同,因此对角线应始终为左下边缘到右上边缘。

谢谢



Best Answer-推荐答案


试试这个:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke android:color="#0a0909"
    android:width="2dp"/>

<corners android:radius="20dp"/>

<gradient android:angle="45"
    android:type="linear"
    android:startColor="#d61a1a"
    android:endColor="#19f269"
    android:useLevel="false"/>

<size android:height="250dp"
    android:width="250dp"/>
</shape>

you will get output like this

关于android - 如何创建带有圆形边缘的对角渐变的自定义可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46704040/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4