简体   繁体   English

如何交换两个圈子

[英]How to swap two circles

I drew 36 circles on Canvas in my class which is extended from View class. 我在班级的Canvas上绘制了36个圆圈,这是View类的扩展。

What I want to learn about, how can I change their positions between two of them like Two Dots game in Play Store. 我想了解的是,如何像Play Store中的“两点”游戏那样在两个位置之间改变它们的位置。

How can I do that? 我怎样才能做到这一点?

Here is my onDraw method code: 这是我的onDraw方法代码:

@Override
protected void onDraw (Canvas canvas){
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    int xArt = 0, yArt = 0, x, y;
    x = getWidth() / 4 - 50;
    y = getHeight() / 2 - 50;

    canvas.drawARGB(96, 25, 139, 10);
    for (int i = 1; i < 37; i++) {
        canvas.drawCircle(x + xArt, y + yArt, 30, paint.get(i));

        yArt += 100;
        if (i % 6 == 0) {
            xArt += 100;
            yArt = 0;
        }
    }

}


Here is the picture: tinypic.com/r/20a9m4j/9 这是图片: tinypic.com/r/20a9m4j/9 图片

So you have 36 circles drawn in a custom View and you want to swap just two of them? 因此,您在自定义View绘制了36个圆,并且只想交换其中两个? It is possible. 有可能的。 Just store the x and y coordinates ( x + xArt, y + yArt ) of the circles in an ArrayList . 只需将圆的x和y坐标( x + xArt, y + yArt )存储在ArrayList You can later use this to swap the circles. 您以后可以使用它交换圈子。 But swapping here means that you need to redraw all the 36 circles. 但是在这里交换意味着您需要重新绘制所有36个圆圈。 That's the problem with custom view, even if you make a slight change you need to redraw the whole view. 这就是自定义视图的问题,即使您进行了微小的更改,也需要重新绘制整个视图。

In your case, I think this is expensive and unnecessary. 就您而言,我认为这是昂贵且不必要的。 Instead, what you need to do is write a custom ViewGroup or use any layout if that's sufficient. 相反,您需要做的是编写自定义ViewGroup或使用任何布局(如果足够的话)。 Add your Circles as children to the ViewGroup . 将您的Circles作为子代添加到ViewGroup Now you can simply use getChild and swap them. 现在,您可以简单地使用getChild并交换它们。 This will not require you to redraw all the 36 circles. 这将不需要您重画所有36个圆。

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

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