Android OnTouch 事件众多对象

发布于 2024-12-06 16:41:15 字数 1086 浏览 1 评论 0原文

好的,我正在玩 ontouch 事件扩展视图。

我所做的是在触摸时画一个圆圈。当你移动时,圆圈会跟随。当你移动时,会形成另一个圆圈,并且会处于减小半径的位置,直到它消失..(现在最多大约 10 个圆圈)。我还可以处理多个手指同时触摸的情况。问题是这样的……代码很糟糕!

创建多个圆圈这是我的绘画方法:

public void onDraw(Canvas canvas)
    {
        paint.setColor(Color.RED);                
        paint.setStyle(Style.STROKE); 
        paint.setStrokeWidth(stroke);
        canvas.drawCircle(x,y,radius,paint);
        canvas.drawCircle(x1,y1,radius1,paint);
        canvas.drawCircle(x2,y2,radius2,paint);
        canvas.drawCircle(x3,y3,radius3,paint);
        canvas.drawCircle(x4,y4,radius4,paint);
        canvas.drawCircle(x5,y5,radius5,paint);
        canvas.drawCircle(x6,y6,radius6,paint);

        paint.setColor(Color.BLUE);
        canvas.drawCircle(x7,y7,radius7,paint);
        canvas.drawCircle(x8,y8,radius8,paint);

        paint.setColor(Color.YELLOW);
        canvas.drawCircle(x9,y9,radius9,paint);
        canvas.drawCircle(x10,y10,radius10,paint);
    }

所以你可以看到这到目前为止效率很低并且会产生一些很长的令人讨厌的代码..问题的一部分是我只能在Ontouch中更改坐标..并作废。任何人都知道我可以更有效地做到这一点的方法(以更面向对象的类型方法)。

ok I'm playing w/ ontouch events extending a view.

what I've done is made a circle on touch.. the cirlce will follow as you move. As you move another circle is made and will sit in the postion decrementing the radius until it disappears.. (right now up to like 10 circles). I can also handle multiple fingers touching at one point in time. Here's the problem.. THE CODE IS NASTY!

To create multiple circle This is my paint method:

public void onDraw(Canvas canvas)
    {
        paint.setColor(Color.RED);                
        paint.setStyle(Style.STROKE); 
        paint.setStrokeWidth(stroke);
        canvas.drawCircle(x,y,radius,paint);
        canvas.drawCircle(x1,y1,radius1,paint);
        canvas.drawCircle(x2,y2,radius2,paint);
        canvas.drawCircle(x3,y3,radius3,paint);
        canvas.drawCircle(x4,y4,radius4,paint);
        canvas.drawCircle(x5,y5,radius5,paint);
        canvas.drawCircle(x6,y6,radius6,paint);

        paint.setColor(Color.BLUE);
        canvas.drawCircle(x7,y7,radius7,paint);
        canvas.drawCircle(x8,y8,radius8,paint);

        paint.setColor(Color.YELLOW);
        canvas.drawCircle(x9,y9,radius9,paint);
        canvas.drawCircle(x10,y10,radius10,paint);
    }

so as you can see this by far inefficient and makes for some long nasty code.. Part of the issue is the fact I'm bound to only being able to change coordinates in Ontouch.. and invalidate. Anyoone know a way I can do this more efficently (in a more object orriented type approach).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2024-12-13 16:41:15

首先,从这个开始:

public class Circle {
    public int x;
    public int y;
    public double radius;
    public Paint paint;

    /* constructors, getters & setters if you feel like ...*/

}

把你所有的圈子放在一个

ArrayList<Circle> circles = new ArrayList();


public void onDraw(Canvas canvas)
{
    /*...*/
    Iterator iterator = circles.iterator();
    while(iterator.hasNext()) {
        drawCircle(iterator.next());
    }
}

public void drawCircle(Canvas canvas, Circle circle) {
    canvas.drawCircle(circle.x, circle.y, circle.raidus, circle.paint);
}

First things first, start with this:

public class Circle {
    public int x;
    public int y;
    public double radius;
    public Paint paint;

    /* constructors, getters & setters if you feel like ...*/

}

And put all your circles in a

ArrayList<Circle> circles = new ArrayList();


public void onDraw(Canvas canvas)
{
    /*...*/
    Iterator iterator = circles.iterator();
    while(iterator.hasNext()) {
        drawCircle(iterator.next());
    }
}

public void drawCircle(Canvas canvas, Circle circle) {
    canvas.drawCircle(circle.x, circle.y, circle.raidus, circle.paint);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文