你可以通过继承 View 类来创建自定义的椭圆 View,并在 onDraw 方法中实现绘制指定颜色、指定圆角大小和添加白边的椭圆。

以下是一个示例代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CustomOvalView extends View {
    private int leftColor;
    private int rightColor;
    private int cornerRadius;
    private int strokeWidth;

    public CustomOvalView(Context context) {
        super(context);
        init();
    }

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

    public CustomOvalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        leftColor = Color.BLUE; // 默认左半边颜色为蓝色
        rightColor = Color.RED; // 默认右半边颜色为红色
        cornerRadius = 20; // 默认圆角半径为20
        strokeWidth = 2; // 默认边框宽度为2
    }

    public void setLeftColor(int color) {
        leftColor = color;
        invalidate();
    }

    public void setRightColor(int color) {
        rightColor = color;
        invalidate();
    }

    public void setCornerRadius(int radius) {
        cornerRadius = radius;
        invalidate();
    }

    public void setStrokeWidth(int width) {
        strokeWidth = width;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        // 创建画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        // 绘制左半边椭圆
        paint.setColor(leftColor);
        RectF leftRect = new RectF(strokeWidth, strokeWidth, width / 2, height - strokeWidth);
        Path leftPath = new Path();
        leftPath.addRoundRect(leftRect, cornerRadius, cornerRadius, Path.Direction.CW);
        canvas.drawPath(leftPath, paint);

        // 绘制右半边椭圆
        paint.setColor(rightColor);
        RectF rightRect = new RectF(width / 2, strokeWidth, width - strokeWidth, height - strokeWidth);
        Path rightPath = new Path();
        rightPath.addRoundRect(rightRect, cornerRadius, cornerRadius, Path.Direction.CW);
        canvas.drawPath(rightPath, paint);

        // 绘制白边
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);
        canvas.drawPath(leftPath, paint);
        canvas.drawPath(rightPath, paint);
    }
}

在布局文件中使用自定义的椭圆 View:

<com.example.CustomOvalView
    android:layout_width="200dp"
    android:layout_height="100dp"
    app:leftColor="#FF0000"
    app:rightColor="#00FF00"
    app:cornerRadius="30"
    app:strokeWidth="4" />

你可以通过设置 leftColorrightColor 属性来指定左半边和右半边的颜色,通过设置 cornerRadius 属性来指定圆角半径,通过设置 strokeWidth 属性来指定白边的宽度

android开发: 给个自定义view画指定颜色指定圆角大小的椭圆要求可设置这个椭圆左半边的颜色和右半边的颜色可设置四个圆角的半径可给整个椭圆加白边

原文地址: https://www.cveoy.top/t/topic/iM1J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录