如何绘制多个颜色的圆圈?

问题描述 投票:0回答:1

我想在照片周围绘制多个颜色圈,这是通过以下代码从我这里获得的:

colors circle

public class DrawPartCircleView extends View {

    private String mColor = "#000000";
    private float mRadius;
    private float mStrokeWidth = 14;
    private Map<String, Integer> mColorsMap;

    public DrawPartCircleView(Context context) {
        super(context);
    }

    public DrawPartCircleView(Context context,  float radius, float strokeWidth, Map<String, Integer> colorsMap) {
        super(context);
        mRadius = radius;
        mStrokeWidth = strokeWidth;
        mColorsMap = colorsMap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float width = (float) getWidth();
        float height = (float) getHeight();

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

        Paint paint = new Paint();
        paint.setStrokeWidth(mStrokeWidth);
        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 - mRadius,
                center_y - mRadius,
                center_x + mRadius,
                center_y + mRadius);

        int countColors = countColors(mColorsMap);
        int startDegree = 0;

        for (String key : mColorsMap.keySet()) {
            int stepDegree = (mColorsMap.get(key) * 360 / countColors);
            paint.setColor(Color.parseColor(key));
            canvas.drawArc(oval, startDegree, stepDegree, false, paint);
            startDegree += stepDegree;
        }
    }

    private int countColors(Map<String, Integer> myColorsMap) {
        int count = 0;
        for (String key : myColorsMap.keySet()) {
            count += myColorsMap.get(key);
        }
        return count;
    }
}

当我有这样的列表视图(超过20件)时出现问题。使用此项目的回收站视图中存在延迟,混乱。

qazxsw poi但是在这个解决方案中,当列表中的元素超过10件时出现了问题。

我如何绘制一个圆圈但是性能却没有变差?也许我可以用I looked here做到这一点?

android android-recyclerview android-custom-view android-glide
1个回答
1
投票

如果我们谈论表演,有一些事情正在发挥作用。

1)你在glide内进行分配。 onDraw()被称为A LOT。你应该在onDraw()之外构建你的PathPaintRectF,并在随后的onDraw()调用中重复使用它们。此外,提前将颜色解析为颜色映射,因此您无需在onDraw()中解析它们。

2)为什么onDraw()不只是使用countColors()

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