创建一个可拖动的弹性球?
我创建了一个加载小球图像的自定义视图。这样 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;
}
现在,我试图让球仅沿着曲线移动,如果它没有移动到固定点之外,则让它摆回到原来的位置。所以我目前面临两个问题:
- 修复球的运动路径
- 通过动画将其抛回。 我观察到的一个问题是如果我 使用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:
- Fixing the movement path of the ball
- Flinging it back by animating it.
One problem I am observing is if I
useball.startAnimation
, and if
the ball was slightly out of focus,
the ball appears sliced.
Any suggestions please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该曲线似乎是圆的一部分,表明它有一个圆心和一个半径。
我建议确定这些是什么,然后编写一个函数,根据给定的手指位置,返回球沿着曲线应该在的位置。这仅需要确定手指相对于圆心的角度,然后使用基本三角学使用该角度来正确定位球。
例如,在您的图片中,球的角度为 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)