安卓 - 绘制一条缎带形状

7
我希望不使用图像视图。这是因为,稍后,我将生成许多具有相同形状但不同颜色的对象,因此我不需要不断添加图像视图,而只需在我的代码中添加颜色。

enter image description here

我该如何在Android Studio上创建这些图片?我查看了Paint、onDraw、Canvas等内容,但对我来说似乎很难。

2
取一个白色三角形图像,将其放置在左侧,并根据需要更改其余视图的背景。 - Bills
3
有许多方法可以做到这一点,但最好的方法可能是使用矢量图。 - Sarthak Mittal
检查我的答案 - Aditya Vyas-Lakhan
使用9 patch或VectorDrawable。 - Phantômaxx
如何从上到下进行操作,如果是从右到左,我们该如何从上到下进行操作? - Jeyaseelan
4个回答

4
如果您想使用XML,请尝试以下方法,它能够正常工作。
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>
        <shape android:shape="rectangle">
            <size
                android:width="100dp"
                android:height="40dp" />
            <solid android:color="#5EB888" />
            <corners android:radius="0dp"/>
        </shape>
    </item>


    <item
        android:top="-26dp"
        android:bottom="31dp"
        android:left="-90dp"
        android:right="75dp">
        <rotate
            android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#ffffff" />
            </shape>
        </rotate>
    </item>



</layer-list>

输出

在此输入图片描述


能否将那个白色移除? - Sunisha Guptan
你给的第二个矩形是白色的,我试图用透明来替换它,但那时它会看起来像一个矩形。 - Sunisha Guptan
keep it white please - Aditya Vyas-Lakhan
我明白你做了什么,甚至是很棒的工作。但有时候背景会像图片一样,这时我们不能设置为白色,所以我才问的。 - Sunisha Guptan
你试过用相同的颜色吗? - Aditya Vyas-Lakhan
是的,那时它可以工作...我在问当背景是图像时,我们不能设置这个颜色代码吗?所以我们必须给透明的。 - Sunisha Guptan

1
你可以使用任何矢量程序(如Adobe Illustrator)创建矢量图像,或使用转换工具(如vectormagic)将其转换,或者找到已经存在的矢量图像,然后将其导入到Android Studio中。

enter image description here

它将被导入为XML文件,您可以根据需要更改颜色。

1
创建一个名为left_arrow.xml的可绘制文件。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <rotate
        android:fromDegrees="45"
        android:toDegrees="90"
        android:pivotX="0%"
        android:pivotY="1%" >
        <shape android:shape="rectangle" >
            <stroke
                android:width="10dp"
                android:color="#00000000" />

            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

现在,创建一个视图布局来创建这种类型的条纹或像这样的图像,下面是代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/llMain"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:background="@color/black"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.1"
        android:background="@drawable/left_arrow"
        android:contentDescription="@string/app_name" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:contentDescription="@string/app_name" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9" />

这个布局创建了所需的箭头条纹作为一个布局。
现在将此布局添加到您需要显示这些条纹的所需布局中。例如,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<include 
android:id="@+id/llMain"
layout="@layout/xtra"/>

</LinearLayout>

现在,您也可以使用“llMain”来使用OnClickListener。希望这有所帮助。

0
你可以使用已经有的白色 PNG 图片,并在 ImageView 类上使用 setColorFilter 方法,将其着色为任何你想要的颜色。

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