개발 일기

Move views using a fling animation ? 본문

Client/안드로이드

Move views using a fling animation ?

이건욱

플링 애니메이션은 객체의 속도에 대해서 비례하는 애니메이션을 보여줍니다.

따라서 초기에 대한 움직임이 점차 느려지게 됩니다.

 

사용할려면 일단 지원 라이브러리를 다음을 추가해야합니다.

dependencies {
	implementation 'com.android.support:support-dynamic-animation:28.0.0'
}

 

그리고 다음과 같이 진행을 할수가 있습니다.

 FlingAnimation(view, DynamicAnimation.SCROLL_X).apply {
        setStartVelocity(-velocityX)
        setMinValue(0f)
        setMaxValue(maxScroll)
        friction = 1.1f
        start()
    }
    

setStartVelocity - 속도를 설정을 할수가 있습니다.

setMinValue , setMaxValue - 최소 및 최대 애니메이션 값을 설정이 가능합니다.

setFriction - 애니메이션의 마찰 값을 변경할수가 있습니다. 

 

마지막으로 간단한 예제를 적도록 하겠습니다.

 

GestureDetector에 대한 내용은 링크를 걸어두도록 하겠습니다.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        temp.setBackgroundColor(ContextCompat.getColor(this , R.color.colorPrimary))

        val flingAnimationX: FlingAnimation by lazy(LazyThreadSafetyMode.NONE) {
            FlingAnimation(temp, DynamicAnimation.X).setFriction(1.0f)
        }
        val flingAnimationY: FlingAnimation by lazy(LazyThreadSafetyMode.NONE) {
            FlingAnimation(temp, DynamicAnimation.Y).setFriction(1.0f)
        }
        val gestureListener = object : GestureDetector.SimpleOnGestureListener() {

            override fun onDown(e: MotionEvent?): Boolean {
                return true
            }

            override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {

                flingAnimationX.setStartVelocity(velocityX)
                flingAnimationY.setStartVelocity(velocityY)

                flingAnimationX.start()
                flingAnimationY.start()

                return true
            }
        }

        val gestureDetector = GestureDetector(this, gestureListener)

        temp.setOnTouchListener { _, motionEvent ->
            gestureDetector.onTouchEvent(motionEvent)
        }

        temp.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                val display = windowManager.defaultDisplay
                val screenSize = Point()
                display.getSize(screenSize)
                flingAnimationX.setMinValue(0f).setMaxValue((screenSize.x - temp.width).toFloat())
                flingAnimationY.setMinValue(0f).setMaxValue((screenSize.y - temp.height).toFloat())
                temp.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
>
    <Button android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/temp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

 

감사합니다.

'Client > 안드로이드' 카테고리의 다른 글

Auto animate layout updates ?  (0) 2020.05.15
zoom animation ?  (0) 2020.05.14
RecyclerView Scroll Position 복원 ?  (0) 2020.05.11
애니메이션 뷰 이동?  (0) 2020.05.11
Animation View Hide ?  (0) 2020.05.04
Comments