简体   繁体   中英

Changing the color of ShapeDrawable dynamically

I have a Canvas . When user tap the display, I draw a red circle on the canvas using ShapeDrawable . I add every circle to List<ShapeDrawable> called 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 . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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