【问题标题】:Changing the color of ShapeDrawable dynamically动态改变 ShapeDrawable 的颜色
【发布时间】:2016-02-02 17:04:42
【问题描述】:

我有一个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();
        }

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

【问题讨论】:

    标签: c# android canvas xamarin shape


    【解决方案1】:

    代替你的台词

    s.Paint.Color = Color.Red;
    
    shapeDrawable.Paint.Color = Color.Blue;
    

    试试下面的:

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

    希望这会有所帮助。

    【讨论】:

    • 谢谢,但我使用的是 Android 版 Xamarin,并且没有名为 FillStyle 或 FillColor 的属性。
    【解决方案2】:

    如果有人正在寻找同一问题的答案,这就是我的解决方法。我认为不可能改变已经在Canvas 上绘制的东西。所以我不得不创建一个继承自ImageView 的自定义视图,它有一个位图作为背景,我将每个被触及的点保存到List 并且我已经覆盖了onDraw(Canvas canvas),在那里我重绘了每个使用特定的Paint 指向List。

    【讨论】:

      猜你喜欢
      • 2018-12-31
      • 1970-01-01
      • 2019-09-10
      • 2011-10-19
      • 2012-10-17
      • 2023-03-21
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多