Android绘制带边框的半圆形

5
我想知道是否有可能画一个半填充的圆形,类似于下面的图片: enter image description here 如果可能的话,我希望稍后可以填充这个圆形或将其变为空心,只剩下边框,这很好,但是否有任何方法可以添加动画?例如从空心圆到半填充或完全填充的圆形,发生过渡?
提前致谢。

是的,这是可能的。我现在有点忙,无法为您提供完整的答案。但是,您需要构建一个自定义的Drawable,重写draw(Canvas)方法,在canvas对象上调用方法以制作圆形,并在需要重新绘制(由于动画)时调用invalidateSelf()。祝你好运。 - Budius
你可以查看这个答案。你需要调整数学计算来将圆旋转90度。此外,如果只针对API 19及以上版本,使用Path#op()方法甚至更简单。 - Mike M.
1
@Budius 谢谢伙计,指导我朝正确的方向就足够了,我会研究一下的。 - arash moeen
@Indiandroid,基本上我想用它来评分,所以它会根据用户的交互而改变,因此我认为gif不会起作用,但无论如何还是谢谢。 - arash moeen
1
@MikeM。那似乎是我想要的,我会研究一下并更新给你,谢谢伙计。 - arash moeen
2个回答

0
尝试为其分配自定义背景,例如:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >

    <gradient
        android:angle="0"
        android:endColor="#FF000000"
        android:startColor="#00000000" />

</shape>

0

示例代码答案:

还可以查看此答案:https://dev59.com/sGEh5IYBdhLWcg3wXCZL#22568222

public class CircleView extends View {

    public CircleView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        _paint = new Paint();
        _paint.setColor(context.getResources().getColor(R.color.colorPrimary));
        _paint.setAntiAlias(true);

        _paintCenter = new Paint();

        _paintCenter.setColor(Color.BLUE);
        _paintCenter.setAntiAlias(true);

        _timeRingSize = Utils.convertDpToPixel(context, 10);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        final float height = canvas.getHeight();
        final float width = canvas.getWidth();

        float radious = width > height ? height * 0.5F : width * 0.5F;

        final float centerX = canvas.getWidth() * 0.5F;
        final float centerY = canvas.getHeight() * 0.5F;

        canvas.drawCircle(centerX, centerY, radious, _paint);

        RectF rectF = new RectF();
        rectF.set(centerX - radious, centerY - radious, centerX + radious, centerY + radious);

        canvas.drawArc(rectF, 0, 90, true, _paintCenter);
    }

    private final float _timeRingSize;
    private Paint _paintCenter;
    private Paint _paint;
}

输出:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接