gpt4 book ai didi

android - 如何在android中画一个半圆

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:58 25 4
gpt4 key购买 nike

我正在使用此代码在我的应用中绘制一半:

  <?xml version="1.0" encoding="utf-8" ?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:left="35dp"
android:top="40dp"
android:bottom="40dp"
android:right="0dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:innerRadius="30dp" android:thickness="0dp">
<solid android:color="@color/transparent"/>
<stroke android:width="3dp" android:color="@color/White"/>

</shape>
</item>
</layer-list>

输出:

enter image description here

但我需要像下面这样的东西:

enter image description here

这个怎么画?

最佳答案

我建议通过代码绘制。

1- 创建类 MyView 并放置下面的代码。

public class MyView extends View {

public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub
super.onDraw(canvas);
float width = (float) getWidth();
float height = (float) getHeight();
float radius;

if (width > height) {
radius = height / 4;
} else {
radius = width / 4;
}

Path path = new Path();
path.addCircle(width / 2,
height / 2, radius,
Path.Direction.CW);

Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);

float center_x, center_y;
final RectF oval = new RectF();
paint.setStyle(Paint.Style.STROKE);

center_x = width / 2;
center_y = height / 2;

oval.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
canvas.drawArc(oval, 90, 180, false, paint);
}
}

2-在您的 Activity 或 fragment 中初始化此类:-

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}

关于android - 如何在android中画一个半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com