[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["# Move views using a fling animation\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use Animations in Compose. \n[Spring AnimationSpec →](/jetpack/compose/animation#spring) \n\n\nFling-based animation uses a friction force that is proportional to an\nobject's velocity. Use it to animate a property of an object and\nto end the animation gradually. It has an initial momentum, which is\nmostly received from the gesture velocity, and gradually slows down. The\nanimation comes to an end when the velocity of the animation is low enough\nthat it makes no visible change on the device screen.\n**Figure 1.** Fling animation\n\n\nTo learn about related topics, read the following guides:\n\n- [Physics-based\n motion](/training/animation/overview#physics-based)\n- [Animate movement\n using spring physics](/guide/topics/graphics/spring-animation)\n\nAdd the AndroidX library\n------------------------\n\nTo use the physics-based animations, you must add the AndroidX library to your project\nas follows:\n\n1. Open the `build.gradle` file for your app module.\n2. Add the AndroidX library to the `dependencies` section. \n\n ### Groovy\n\n ```groovy\n dependencies {\n implementation 'androidx.dynamicanimation:dynamicanimation:1.0.0'\n }\n \n ```\n\n ### Kotlin\n\n ```kotlin\n dependencies {\n implementation(\"androidx.dynamicanimation:dynamicanimation:1.0.0\")\n }\n \n ```\n\nCreate a fling animation\n------------------------\n\n\nThe [FlingAnimation](/reference/androidx/dynamicanimation/animation/FlingAnimation) class lets you create\na fling animation for an object. To build a fling animation, create an\ninstance of the [FlingAnimation](/reference/androidx/dynamicanimation/animation/FlingAnimation) class and\nprovide an object and the object's property that you want to animate. \n\n### Kotlin\n\n```kotlin\nval fling = FlingAnimation(view, DynamicAnimation.SCROLL_X)\n```\n\n### Java\n\n```java\nFlingAnimation fling = new FlingAnimation(view, DynamicAnimation.SCROLL_X);\n```\n\nSet velocity\n------------\n\n\nThe starting velocity defines the speed at which an animation property\nchanges at the beginning of the animation. The default starting velocity is\nset to zero pixels per second. Therefore, you must define a start velocity\nto ensure the animation does not end right away.\n\n\nYou can use a fixed value as the starting velocity, or you can base it off\nof the velocity of a touch gesture. If you choose to provide a fixed value,\nyou should define the value in dp per second, then convert it to pixels\nper second. Defining the value in dp per second allows the velocity to be\nindependent of a device's density and form factors. For more information about\nconverting the starting velocity to pixels per second, refer to the\n[Converting\ndp per second to pixels per second](/topic/libraries/support-library/preview/spring-animation#converting-value) section in\n[Spring Animation](/topic/libraries/support-library/preview/spring-animation).\n\n\nTo set the velocity, call the `setStartVelocity()` method and pass\nthe velocity in pixels per second. The method returns the fling object on\nwhich the velocity is set.\n\n**Note:** Use the\n[GestureDetector.OnGestureListener](/reference/android/view/GestureDetector.OnGestureListener) and the\n[VelocityTracker](/reference/android/view/VelocityTracker) classes to retrieve and compute\nthe velocity of touch gestures, respectively.\n\n### Set an animation value range\n\n\nYou can set the minimum and the maximum animation values when you want to\nrestrain the property value to a certain range. This range control is\nparticularly useful when you animate properties that have an intrinsic\nrange, such as alpha (from 0 to 1).\n\n**Note**: When the value of a fling animation reaches the\nminimum or maximum value, the animation ends.\n\n\nTo set the minimum and maximum values, call the `setMinValue()`\nand `setMaxValue()` methods, respectively.\nBoth methods return the animation object for which you have set the value.\n\n### Set friction\n\n\nThe `setFriction()` method lets you change the animation's\nfriction. It defines how quickly the velocity decreases in an animation.\n\n**Note**: If you don't set the friction at the beginning of\nthe animation, the animation uses a default friction value of 1.\n\n\nThe method returns the object whose animation uses the friction value you\nprovide.\n\n#### Sample code\n\n\nThe example below illustrates a horizontal fling. The velocity captured from\nthe velocity tracker is `velocityX` and, the scroll bounds are\nset to 0 and\nmaxScroll. The friction is set to 1.1. \n\n### Kotlin\n\n```kotlin\nFlingAnimation(view, DynamicAnimation.SCROLL_X).apply {\n setStartVelocity(-velocityX)\n setMinValue(0f)\n setMaxValue(maxScroll)\n friction = 1.1f\n start()\n}\n```\n\n### Java\n\n```java\nFlingAnimation fling = new FlingAnimation(view, DynamicAnimation.SCROLL_X);\nfling.setStartVelocity(-velocityX)\n .setMinValue(0)\n .setMaxValue(maxScroll)\n .setFriction(1.1f)\n .start();\n```\n\n### Set the minimum visible change\n\n\nWhen you animate a custom property that isn't defined in pixels, you should set the\nminimal change of animation value that is visible to users. It\ndetermines a reasonable threshold for ending the animation.\n\nIt isn't necessary to call this method when animating\n[DynamicAnimation.ViewProperty](/reference/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty) because the\nminimum visible change is derived from the property. For example:\n\n- The default minimum visible change value is 1 pixel for view properties such as `TRANSLATION_X`, `TRANSLATION_Y`, `TRANSLATION_Z`, `SCROLL_X`, and `SCROLL_Y`.\n- For animations that use rotation, such as `ROTATION`, `ROTATION_X`, and `ROTATION_Y`, the minimum visible change is `MIN_VISIBLE_CHANGE_ROTATION_DEGREES`, or 1/10 pixel.\n- For animations that use opacity, the minimum visible change is `MIN_VISIBLE_CHANGE_ALPHA`, or 1/256.\n\n\nTo set the minimum visible change for an animation, call the\n`setMinimumVisibleChange()` method and pass either\none of the minimum visible constants or a value that you need to calculate\nfor a custom property. For more information on calculating this value,\nrefer to the\n[Calculating a minimum visible change value](#calculating-mvc-value)\nsection. \n\n### Kotlin\n\n```kotlin\nanim.minimumVisibleChange = DynamicAnimation.MIN_VISIBLE_CHANGE_SCALE\n```\n\n### Java\n\n```java\nanim.setMinimumVisibleChange(DynamicAnimation.MIN_VISIBLE_CHANGE_SCALE);\n```\n\n**Note**: You need to pass a value only when you animate a\ncustom property that is not defined in pixels.\n\n#### Calculating a minimum visible change value\n\n\nTo calculate the minimum visible change value for a custom property, use the\nfollowing formula:\n\nMinimum visible change = Range of custom property value / Range of\nanimiation in pixels\n\n\nFor example, the property that you want to animate progresses from 0 to\n100. This corresponds to a 200-pixel change. Per the formula, the minimum\nvisible change value is 100 / 200 is equal to 0.5 pixels."]]