如何使用带有圆边的对角线渐变创建自定义绘图?

San*_*oop 9 xml android gradient android-layout

我试图使用xml制作自定义drawable,如附加图像 在此输入图像描述

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

<?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>
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

这种方法实际上搞砸了UI,我也在圆边上妥协,

所以,我有什么方法可以使用XML绘制这种效果?

任何形式的帮助将不胜感激.

FYI可绘制的宽度和高度会有所不同,因此对角线应始终从左下边缘到右上边缘.

谢谢

小智 10

试试这个:

<?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>
Run Code Online (Sandbox Code Playgroud)

你会得到这样的输出

  • 看到Raghav,我完全感谢你的努力,如果你没有正确阅读我的问题,我想有一条对角线,而不是合并效果,谢谢:) (2认同)