动态更改ShapeDrawable的颜色

时间:2016-02-02 17:04:43

标签: c# android canvas xamarin shape

我有Canvas。当用户点击显示时,我使用ShapeDrawable在画布上绘制一个红色圆圈。我将每个圆圈添加到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();
        }

但颜色并没有改变。我做错了什么?

2 个答案:

答案 0 :(得分:0)

取代你的行

s.Paint.Color = Color.Red;

shapeDrawable.Paint.Color = Color.Blue;

尝试以下方法:

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

希望这有帮助。

答案 1 :(得分:0)

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