创建一个可拖动的弹性球?

发布于 2024-09-10 21:06:37 字数 1038 浏览 6 评论 0原文

我创建了一个加载小球图像的自定义视图。这样 onDraw 方法将如下所示:

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(...);
}

后来,我添加了一个 onTouch 侦听器,以便能够侦听触摸事件,从而使球能够被拖动。

public boolean onTouchEvent(MotionEvent event) { 
        int eventaction = event.getAction(); 

        int X = (int)event.getX(); 
        int Y = (int)event.getY(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_MOVE:   // touch drag with the ball 
            // move the balls the same as the finger 
            ball.setX(X-25); 
            ball.setY(Y-25); 

            break; 
        } 
        // redraw the canvas 
        invalidate(); 
        return true; 
    } 

现在,我试图让球仅沿着曲线移动,如果它没有移动到固定点之外,则让它摆回到原来的位置。所以我目前面临两个问题:

  1. 修复球的运动路径
  2. 通过动画将其抛回。 我观察到的一个问题是如果我 使用ball.startAnimation,并且如果 球稍微失焦, 球看起来被切片。

球摆动

请问有什么建议吗?

I created a custom view loading an image of a small ball. So that the onDraw method would look like this:

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(...);
}

Later, I added an onTouch listener to be able to listen to touch events to enable the ball to be dragged around.

public boolean onTouchEvent(MotionEvent event) { 
        int eventaction = event.getAction(); 

        int X = (int)event.getX(); 
        int Y = (int)event.getY(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_MOVE:   // touch drag with the ball 
            // move the balls the same as the finger 
            ball.setX(X-25); 
            ball.setY(Y-25); 

            break; 
        } 
        // redraw the canvas 
        invalidate(); 
        return true; 
    } 

Now, I am trying to make the ball move ONLY along a curve and if its not moved beyond a fixed point, make it swing back to its original position. So there are two problems I am currently facing:

  1. Fixing the movement path of the ball
  2. Flinging it back by animating it.
    One problem I am observing is if I
    use ball.startAnimation, and if
    the ball was slightly out of focus,
    the ball appears sliced.

Ball Swing

Any suggestions please?

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

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

发布评论

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

评论(1

深海不蓝 2024-09-17 21:06:37

该曲线似乎是圆的一部分,表明它有一个圆心和一个半径。

我建议确定这些是什么,然后编写一个函数,根据给定的手指位置,返回球沿着曲线应该在的位置。这仅需要确定手指相对于圆心的角度,然后使用基本三角学使用该角度来正确定位球。

例如,在您的图片中,球的角度为 0°,而标记的角度约为 20°。

然后,您可以确定如果手指经过某个点,球应该弹回到什么角度(同样,用与圆中心的角度来描述)

The curve appears to be part of a circle, suggesting it has a centre and a radius.

I'd suggest determining what these are, and then writing a function that, given the finger position, returns where along the curve the ball should be. This simply requires determining the angle of the finger compared to the centre of the circle, and then using this angle to position the ball correctly using basic trigonometry.

In your picture, for example, the ball is at 0° and the mark appears to be at about 20°.

You could then determine what angle the ball should fling back to should the finger past a certain point (again, described by an angle from the circle's centre)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文