Android用边框画半圈

问题描述 投票:5回答:2

我想知道是否有可能绘制一个填充了一半的圆圈,如下所示:

如果可能的话,稍后我想填充这个cirlce或者只剩下边框使它变空,这很好但是无论如何都要添加动画吗?比如从一个空圆圈到一半填充或完全填充的圆圈发生过渡?

提前致谢

android android-animation android-drawable
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://stackoverflow.com/a/22568222/1052261

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

© www.soinside.com 2019 - 2024. All rights reserved.