【问题标题】:how to get position(x and y coordinates) of circle drwan on canvas in android?如何获取在android中画布上绘制的圆的位置(x和y坐标)?
【发布时间】:2019-06-14 18:21:38
【问题描述】:

我对canvas android有两个疑惑。

解释如下:

首先,我在画布上绘制了一排圆圈,我想捕获绘制的圆圈(仅在一个圆圈上)的 x 和 y 坐标,以便我可以在圆圈上(在中心)绘制位图。

其次,我想在圆圈上暗示触摸事件,即我希望它们在有人触摸它们时改变颜色有人可以帮助我吗?

【问题讨论】:

  • "我想捕捉 x 和 y 坐标..." 你在画圆圈时已经有了坐标 - 为什么不直接保留坐标以便以后使用它们呢?
  • “在圆上绘制位图”是什么意思?

标签: android canvas position coordinates geometry


【解决方案1】:

对于#2: 计算点的中心和触摸事件之间的距离 - 如果距离小于圆的半径 - 你按下了圆

【讨论】:

    【解决方案2】:

    首先,您应该为您的“圆形区域”创建 GridSector 类。

    public class GridSector {
    
        private int x = 0;
        private int y = 0;
        private Rect rect = new Rect(0, 0, 0, 0);
    
        public GridSector(int x, int y, Rect rect) {
            this.x = x;
            this.y = y;
            this.rect = rect;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public Rect getRect() {
            return rect;
        }
    
        public void setRect(Rect rect) {
            this.rect.set(rect.left, rect.top, rect.right, rect.bottom);
        }
    }
    

    然后创建您想要触摸的视图。

        public class GridSectorsView extends View {
    
    
            GridSector currentSelectedSector;
            @Override
                protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);
                    drawCircle(canvas); // draw your circles;
                }
    
              @Override
                public boolean onTouchEvent(MotionEvent ev) {
                    switch (ev.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                        invalidate(currentSelectedSector.getRect()); // update your circles (call  onDraw Function ) ;
                   }
                }
    }
    

    【讨论】:

    • 你能解释一下吗?因为我是 android 新手
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2022-09-30
    相关资源
    最近更新 更多