android - 如何在 View 底部绘制圆段[android]?

标签 android android-drawable

我的 Activity 应该是这样的:

我不确定在顶部 View 和底部 View 之间显示圆圈部分的最佳方式是什么。

我想避免将 fragment 保存为图像。

有没有办法使用 drawable xml 来显示它(我已经尝试使用 oval 来实现这个,但我不知道如何做到这一点“平坦的”)?

也许在 canvas 上绘图?

最佳答案

我认为选项 3 是最好的,你可以精确地控制你正在绘制的内容,但你不会失去 OOP 业力点数

选项 1

您可以为您的顶级 View 创建自定义 View 并像这样自己实现绘图,我在这里子类化 RelativeLayout 但您可以子类化任何你想要的View

public class CurveBgRelativeLayout extends RelativeLayout {

    public CurveBgRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private Path path;
    private Paint paint;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(0xffff0000);
        path = new Path();

        float horizontalOffset = w * .8f;
        float top = -h * .8f;
        float bottom = h;

        RectF ovalRect = new RectF(-horizontalOffset, top, w + horizontalOffset, bottom);
        path.lineTo(ovalRect.left, top);
        path.arcTo(ovalRect, 0, 180, false);
        path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (path != null)
            canvas.drawPath(path, paint);

        super.onDraw(canvas);
    }
}

然后在你的xml布局中

<?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">

    <com.example.CurveBgRelativeLayout
        android:layout_width="match_parent"
        android:background="#ffffffff"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffff0000"
        />
</LinearLayout>



选项 2

您可以创建一个 xml 可绘制对象作为顶部 View 的背景,这里唯一的问题是圆的水平拉伸(stretch)以 dp 为单位给出,因此它与 View 的大小

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:height="40dp"
        android:gravity="bottom">
        <shape android:shape="rectangle">
            <solid android:color="#ffff0000" />
        </shape>
    </item>
    <item
        android:width="500dp"
        android:height="60dp"
        android:gravity="bottom|center_horizontal"
        android:top="-40dp">
        <shape android:shape="oval">
            <solid android:color="#ffffffff" />    
        </shape>
    </item>
    <item
        android:height="30dp"
        android:bottom="30dp"
        android:gravity="bottom">
        <shape android:shape="rectangle">
            <solid android:color="#ffffffff" />
        </shape>
    </item>
</layer-list>


选项 3

public class CurveBottomDrawable extends Drawable {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Path path = new Path();

    public CurveBottomDrawable(int color) {
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(color);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(0xffff0000);

        path.reset();
        Rect bounds = getBounds();

        float horizontalOffset = bounds.width() * .8f;
        float top = -bounds.height() * .8f;
        float bottom = bounds.height();

        RectF ovalRect = new RectF(-horizontalOffset, top, bounds.width() + horizontalOffset, bottom);
        path.lineTo(ovalRect.left, top);
        path.arcTo(ovalRect, 0, 180, false);
        path.setFillType(Path.FillType.INVERSE_EVEN_ODD);

        canvas.drawPath(path, paint);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        paint.setAlpha(alpha);
    }

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

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

}

然后以编程方式设置它

myView.setBackground(new CurveBottomDrawable(0xffff0000));

关于android - 如何在 View 底部绘制圆段[android]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42734711/

相关文章:

android - 在Android上以编程方式绘制带有边框(角半径)的椭圆形

android - 从可绘制资源中更改 png 图标颜色

android - 我的图层列表可绘制有什么问题?

Android:如何添加按钮和自定义 View

android - RecyclerView 可见计数给出了 Recyclerview 中的项目总数

java - 针对 Spring Security 的应用程序身份验证

java - Android应用程序计算错误和崩溃

android - 添加 Prepared Maps 导致 Android SDK 崩溃

android - 如何在 Compose 中使用动画矢量可绘制对象

java - 以编程方式更改现有形状中的纯色