简体   繁体   English

动态更改ShapeDrawable的颜色

[英]Changing the color of ShapeDrawable dynamically

I have a Canvas . 我有一块Canvas When user tap the display, I draw a red circle on the canvas using ShapeDrawable . 当用户点击显示器时,我使用ShapeDrawable在画布上绘制一个红色圆圈。 I add every circle to List<ShapeDrawable> called hits. 我将每个圆圈添加到List<ShapeDrawable>称为hits。 But I want just the last circle to be red and all others to be recolored to blue. 但是我只希望最后一个圆圈是红色,而其他所有颜色都重新着色为蓝色。

Here is my code: 这是我的代码:

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();
        }

But the color is not changing. 但是颜色没有改变。 What am I doing wrong please? 请问我做错了什么?

In place of your lines 代替你的台词

s.Paint.Color = Color.Red;

shapeDrawable.Paint.Color = Color.Blue;

Try the below: 请尝试以下方法:

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

Hope this helps. 希望这可以帮助。

If someone is looking for an answer to the same question, here is how I've solved it. 如果有人在寻找相同问题的答案,这就是我的解决方法。 I think that it is not possible to change something that has been already drawn on Canvas . 我认为无法更改已经在Canvas绘制的内容。 So I've been forced to create a custom view inherited from ImageView which has a bitmap as background and I'm saving every point that has been touched to the List and I've overridden onDraw(Canvas canvas) where I redraw every point in List using specific Paint . 因此,我不得不创建一个继承自ImageView的自定义视图,该视图具有位图作为背景,并且我将触摸到的每个点都保存到List并且覆盖了onDraw(Canvas canvas) ,在其中我重onDraw(Canvas canvas)每个点使用特定的Paint List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM