unity 物理台球
学习Android开发 (Learning Android Development)
While playing around with Android Physics-based Animation API, I just created a simple App (just one Activity class— not good programming practice, but to state how simple it is) with about 200 lines of codes, and have the snooker like game.
W¯¯往往微不足道玩弄基于物理学的Android动画API ,我就创建了一个简单的应用程序(只需一个活动讲座不是良好的编程习惯,但是国家是多么简单),与约200行代码,并且有一个类似的游戏斯诺克。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/802e14db93e5cbc0fcc163511d953c7a.gif)
基于Android物理的动画 (Android Physics-Based Animation)
Android Physics-Based Animation consists of these two animations, i.e. FlingAnimation
and SpringAnimation
, which is a subclass of DynamicAnimation
.
基于Android物理的动画由这两个动画组成,即FlingAnimation
和SpringAnimation
,它们是DynamicAnimation
的子类。
It is not provided by default on Android. You’ll need to get the dependencies through build.gradle
by adding
Android上默认不提供此功能。 您需要通过添加以下内容来通过build.gradle
获取依赖build.gradle
implementation "androidx.dynamicanimation:dynamicanimation:1.0.0"
Unlike normal animation, Fling and Spring animation behave according to the velocity provided to them. Hence the behavior is dynamic in correspond to its start velocity.
与普通动画不同,Fling和Spring动画根据提供给它们的速度进行动作。 因此,行为与其初始速度相对应是动态的。
I use both of them and tied them together to make this simple snooker-like game. Check out below for a little more details of how it is implemented.
我将它们都使用,并将它们绑在一起,以制作这款简单的斯诺克式游戏。 请在下面查看有关其实施方式的更多详细信息。
手势检测 (Gesture Detection)
The only touch control I add on is the gestureDetector
我添加的唯一触摸控件是gestureDetector
img_ball.setOnTouchListener { view, motionEvent ->
gestureDetector.onTouchEvent(motionEvent)
true}
Upon detecting the Fling gesture, we could then get both the X and Y velocity. Using those velocities, we’ll start the FlingAnimations
一旦检测到Fling手势,我们就可以同时获得X和Y速度。 使用这些速度,我们将启动FlingAnimations
private val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent?): Boolean {
stopAnimation = false
return true
}
override fun onFling(
e1: MotionEvent?,
e2: MotionEvent?,
velocityX: Float,
velocityY: Float
): Boolean {
if (isAnimationRunning()) return false
flingAnimationX.setStartVelocity(velocityX).start()
flingAnimationY.setStartVelocity(velocityY).start()
return true
}
}
动态动画 (Fling Dynamic Animation)
This is the most important behavior of the game. One could just click and swipe the ball towards the direction it should head and the velocity of the fling based on the gesture of the user.
这是游戏中最重要的行为。 只需单击并朝着用户应前进的方向滑动球,然后根据用户的手势挥动速度即可。
Unlike any other animation, the developer doesn’t define the target final destination, but instead, it is determined by the velocity provide