动态改变 ShapeDrawable 的颜色

Changing the color of ShapeDrawable dynamically

我有一个Canvas。当用户点击显示时,我使用 ShapeDrawable 在 canvas 上画了一个红色圆圈。我将每个圆圈添加到 List<ShapeDrawable> 称为命中。但我只希望最后一个圆圈为红色,所有其他圆圈都重新着色为蓝色。

这是我的代码:

if (e.Event.Action == MotionEventActions.Up)
        {
            int x = (int) e.Event.GetX();
            int y = (int) e.Event.GetY();

            ShapeDrawable s = new ShapeDrawable(new OvalShape());
            s.Paint.Color = Color.Red;
            s.SetIntrinsicWidth(30);
            s.SetIntrinsicHeight(30);

            double widthRatio = canvas.Width/(double) imageView.Width;
            double heightRatio = canvas.Height/(double) imageView.Height;

            s.SetBounds((int) (x*widthRatio - 15), (int) (y*heightRatio - 15), (int) (x*widthRatio + 15),
                (int) (y*heightRatio + 15));

            s.Draw(canvas);
            foreach (var shapeDrawable in hits)
            {
                shapeDrawable.Paint.Color = Color.Blue;
                shapeDrawable.InvalidateSelf();
            }
            hits.Add(s);
            imageView.Invalidate();
        }

但是颜色没有变化。请问我做错了什么?

代替你的台词

s.Paint.Color = Color.Red;

shapeDrawable.Paint.Color = Color.Blue;

尝试以下方法:

s.FillStyle = FillStyle.Solid;
s.FillColor = Color.Red;

希望对您有所帮助。

如果有人正在寻找同一问题的答案,我是这样解决的。我认为不可能更改已经在 Canvas 上绘制的内容。所以我被迫创建一个继承自 ImageView 的自定义视图,它以位图作为背景,我正在保存每个触及到 List 的点,并且我已经覆盖了 onDraw(Canvas canvas) 我使用特定的 Paint.

重新绘制 List 中的每个点