gpt4 book ai didi

android - 如何在xml中剪辑圆圈

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:39 26 4
gpt4 key购买 nike

我需要在 xml 中创建半圆。我遇到过剪辑,但我不确定如何使用它。

这是我的 circle_view 可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
android:color="#666666"/>

<size
android:width="120dp"
android:height="120dp"/>
</shape>

这是我用 clip 尝试的:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/circle_view"
android:gravity="top"/>

但结果是什么都没有。如何在纯xml中正确使用clip?

最佳答案

您可以实现自己的 Drawable。但这不能从 XML 中膨胀。您需要使用

从代码中设置可绘制对象
 View.setBackgroundDrawable();

试试这个示例实现,使用 drawable 绘制一个半圆。

public class SemiCircleDrawable extends Drawable {

private Paint paint;
private RectF rectF;
private int color;
private Direction angle;

public enum Direction
{
LEFT,
RIGHT,
TOP,
BOTTOM
}

public SemiCircleDrawable() {
this(Color.BLUE, Direction.LEFT);
}

public SemiCircleDrawable(int color, Direction angle) {
this.color = color;
this.angle = angle;
paint = new Paint();
paint.setColor(color);
paint.setStyle(Style.FILL);
rectF = new RectF();
}

public int getColor() {
return color;
}

/**
* A 32bit color not a color resources.
* @param color
*/
public void setColor(int color) {
this.color = color;
paint.setColor(color);
}

@Override
public void draw(Canvas canvas) {
canvas.save();

Rect bounds = getBounds();

if(angle == Direction.LEFT || angle == Direction.RIGHT)
{
canvas.scale(2, 1);
if(angle == Direction.RIGHT)
{
canvas.translate(-(bounds.right / 2), 0);
}
}
else
{
canvas.scale(1, 2);
if(angle == Direction.BOTTOM)
{
canvas.translate(0, -(bounds.bottom / 2));
}
}


rectF.set(bounds);

if(angle == Direction.LEFT)
canvas.drawArc(rectF, 90, 180, true, paint);
else if(angle == Direction.TOP)
canvas.drawArc(rectF, -180, 180, true, paint);
else if(angle == Direction.RIGHT)
canvas.drawArc(rectF, 270, 180, true, paint);
else if(angle == Direction.BOTTOM)
canvas.drawArc(rectF, 0, 180, true, paint);
}

@Override
public void setAlpha(int alpha) {
// Has no effect
}

@Override
public void setColorFilter(ColorFilter cf) {
// Has no effect
}

@Override
public int getOpacity() {
// Not Implemented
return 0;
}

}

致谢:#Vivek Khandelwal

关于android - 如何在xml中剪辑圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204428/

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