有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中带有底线曲线的java自定义形状布局

我想做一个像图中一样的布局,底部弯曲

我尝试在stackoverflow中使用一些示例,但无法确切了解如何做到这一点

现在我正在尝试使用可绘制的xml并将其包含在布局中:

`<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:shape="rectangle">
    <corners
        安卓:bottomLeftRadius="0dp"
        安卓:bottomRightRadius="5dp"
        安卓:topLeftRadius="0dp"
        安卓:topRightRadius="0dp" />
    <padding
        安卓:bottom="5dp"
        安卓:left="20dp"
        安卓:right="0dp"
        安卓:top="0dp" />
    <stroke
        安卓:width="0.5dp"
        安卓:color="@color/colorPrimary" />
    <solid 安卓:color="@color/colorPrimary" />
</shape>`

screen image


共 (1) 个答案

  1. # 1 楼答案

    你可以这样做-
    创建为自定义视图-

    public class CustomDrawableView extends View {
      Paint paint;
      void init(){
        paint =  new Paint();
      }
    }
    

    现在,在onDraw()方法中,您可以绘制为-
    首先画一个高度为getHeight()的矩形/2
    宽度=getWidth()

    onDraw(Canvas canvas){
      Rect r = new Rect(0, 0, width , height );
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.RED);
      canvas.drawRect(r, paint);
      int centerX=widht;
      int centerY = -50;
      int radius = Math.sqrt((0-centerX)*(0-centerX) + (height-centerY )*(height-centerY)); 
      canvas.drawCircle(width, height -50 , radius, paint); // for big circle
    
    }
    

    设置CustomDrawableView的高度和宽度以匹配父视图,还可以根据需要调整中心