我如何使用可绘制形状在 android 中制作此形状

How can i make this shape in android using shape drawable

我想在我的 android 项目中制作这个形状。如何使用 shape 制作此形状?

试试这个方法

您使用 MaterialButton

实现
<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:text="Nilesh rathod"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        app:shapeAppearanceOverlay="@style/CustomShapeAppearanceOverlay.Button"/>

现在在您的 styles.xml

中创建 CustomShapeAppearanceOverlay.Button
    <style name="CustomShapeAppearanceOverlay.Button" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">18dp</item>
    <item name="cornerSizeBottomLeft">18dp</item>
    <item name="cornerFamilyTopLeft">cut</item>
    <item name="cornerFamilyBottomLeft">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
</style>

确保您已添加 dependenciesmaterial 个库

implementation 'com.google.android.material:material:1.1.0-alpha10'

输出

你可以试试这个:

<?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="#FAD55C" />
        </shape>
    </item>
    <item
        android:top="-40dp"
        android:bottom="60dp"
        android:start="-24dp">
        <rotate
            android:fromDegrees="315">
            <shape android:shape="rectangle">
                <solid android:color="#FFF" />
            </shape>
        </rotate>
    </item>

    <item
        android:top="60dp"
        android:bottom="-40dp"
        android:start="-24dp">
        <rotate
            android:fromDegrees="-315">
            <shape android:shape="rectangle">
                <solid android:color="#FFF" />
            </shape>
        </rotate>
    </item>
</layer-list>

像这样创建一个自定义绘图Class

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ArrowDrawable extends Drawable {
    private Rect mBounds;
    private Paint mDrawPaint;

    public ArrowDrawable() {
        mDrawPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mDrawPaint.setColor(Color.BLUE);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        mBounds=getBounds();
        Path path=new Path();
        path.moveTo(mBounds.left,mBounds.bottom/2);
        path.lineTo(mBounds.right/8,mBounds.top);
        path.lineTo(mBounds.right,mBounds.top);
        path.lineTo(mBounds.right,mBounds.bottom);
        path.lineTo(mBounds.right/8,mBounds.bottom);
        path.lineTo(mBounds.left,mBounds.bottom/2);
        canvas.drawPath(path,mDrawPaint);
    }

    @Override
    public void setAlpha(int alpha) {
      mDrawPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
      mDrawPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

Drawable 设置为 TextView

ArrowDrawable arrowDrawable=new ArrowDrawable();
textView.setBackground(arrowDrawable);