如何使尖锐的渐变可绘制

how to make sharp gradient drawable

我想做一个像这样的锐利的黑白渐变

我所做的就是这个

这是我的可绘制文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#000"
    android:endColor="#fff"
    android:centerColor="#fff"
    />
</shape>

想知道怎么做吗!

有一个很棒的网站,只需转到那里并切换到 'android' 选项卡。 这是 link:Angrytools.com

首先,根据您的图片,这只包含黑色。所以你可以使用简单的固体来做到这一点 作为

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

    <solid android:color="#000000" />

</shape>

正如其他人已经注意到的,您想要实现的效果不是渐变。 如果我理解你正在尝试实现 flexible 图像,divided 1:1.

不可能通过 .xml 文件或 9patch[=43] 获得此信息 =](然后你失去了黑白之间的急剧过渡)。

第一个解决方案是使用 LinearLayoutlayout_weight。您可以在布局中添加 'layer' - 如果它是背景 - 在其他组件下。这个 'layer' 看起来像这样:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/black"/>

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/white"/>

</LinearLayout>

如果您在布局中使用它一次或两次,此 additional/nested 布局不会有问题,但请避免在集合项中使用此类解决方案。


您也可以尝试使用普通双色png文件ImageView。 (提示:此文件的适当尺寸可以使您有明显的过渡。)


第三种方法,最轻一种,但有点耗时,就是覆盖View.onDraw()方法和在绘制其他内容之前绘制两半。