উন্নত অ্যানিমেশনের উদাহরণ: জেসচার

শুধুমাত্র অ্যানিমেশন নিয়ে কাজ করার তুলনায় টাচ ইভেন্ট ও অ্যানিমেশন নিয়ে কাজ করার সময় আমাদের বেশ কিছু বিষয় বিবেচনা করতে হয়। প্রথমত, টাচ ইভেন্ট শুরু হলে আমাদের হয়ত চলমান অ্যানিমেশনকে বাধা দিতে হবে কারণ ব্যবহারকারীর ইন্টারঅ্যাকশনকে সর্বোচ্চ অগ্রাধিকার দেওয়া উচিত।

নিচের উদাহরণে, আমরা একটি বৃত্ত কম্পোনেন্টের অফসেট পজিশন দেখাতে Animatable ব্যবহার করি। টাচ ইভেন্ট pointerInput মডিফায়ারের মাধ্যমে প্রসেস করা হয়। আমরা নতুন ট্যাপ ইভেন্ট শনাক্ত করলে, ট্যাপ পজিশনে অফসেট ভ্যালু অ্যানিমেট করতে animateTo কল করি। অ্যানিমেশন চলাকালীনও ট্যাপ ইভেন্ট ঘটতে পারে এবং সেক্ষেত্রে, animateTo চলমান অ্যানিমেশনকে বাধা দেয় এবং বাধাগ্রস্ত অ্যানিমেশনের গতি বজায় রেখে নতুন টার্গেট পজিশনে অ্যানিমেশন শুরু করে।

@Composable
fun Gesture() {
    val offset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                coroutineScope {
                    while (true) {
                        // Detect a tap event and obtain its position.
                        awaitPointerEventScope {
                            val position = awaitFirstDown().position

                            launch {
                                // Animate to the tap position.
                                offset.animateTo(position)
                            }
                        }
                    }
                }
            }
    ) {
        Circle(modifier = Modifier.offset { offset.value.toIntOffset() })
    }
}

private fun Offset.toIntOffset() = IntOffset(x.roundToInt(), y.roundToInt())

আরেকটি সাধারণ প্যাটার্ন হল, আমাদের অ্যানিমেশন ভ্যালুগুলিকে টাচ ইভেন্ট থেকে আসা ভ্যালুগুলির সাথে সিঙ্ক করতে হবে, যেমন টেনে আনা। নিচের উদাহরণে, আমরা দেখতে পাচ্ছি যে "বাতিল করতে সোয়াইপ করুন" ফিচারটি Modifier হিসেবে প্রয়োগ করা হয়েছে (SwipeToDismiss কম্পোজেবল ব্যবহার করার পরিবর্তে)। Animatable দিয়ে এলিমেন্টের অনুভূমিক অফসেট বোঝানো হয়। এই API-এর একটি বৈশিষ্ট্য আছে যা জেসচার অ্যানিমেশনের ক্ষেত্রে উপযোগী। টাচ ইভেন্ট ও অ্যানিমেশনের মাধ্যমে এর ভ্যালু পরিবর্তন করা যেতে পারে। আমরা টাচ ডাউন ইভেন্ট পেলে, stop পদ্ধতির মাধ্যমে Animatable বন্ধ করে দিই যাতে যেকোনও চলমান অ্যানিমেশন ইন্টারসেপ্ট করা যায়।

ড্র্যাগ ইভেন্ট চলাকালীন, টাচ ইভেন্ট থেকে গণনা করা ভ্যালুর সাথে Animatable ভ্যালু আপডেট করতে আমরা snapTo ব্যবহার করি। ফ্লিংয়ের জন্য, Compose VelocityTracker প্রদান করে যাতে ড্র্যাগ ইভেন্ট রেকর্ড করা এবং গতিবেগ গণনা করা যায়। ফ্লিং অ্যানিমেশনের জন্য বেগ সরাসরি animateDecay-এ ফিড করা যেতে পারে। আমরা যখন অফসেট ভ্যালু স্লাইড করে আসল পজিশনে ফিরিয়ে আনতে চাই, তখন animateTo মেথডের মাধ্যমে 0f-এর টার্গেট অফসেট ভ্যালু নির্দিষ্ট করি।

fun Modifier.swipeToDismiss(
    onDismissed: () -> Unit
): Modifier = composed {
    val offsetX = remember { Animatable(0f) }
    pointerInput(Unit) {
        // Used to calculate fling decay.
        val decay = splineBasedDecay<Float>(this)
        // Use suspend functions for touch events and the Animatable.
        coroutineScope {
            while (true) {
                val velocityTracker = VelocityTracker()
                // Stop any ongoing animation.
                offsetX.stop()
                awaitPointerEventScope {
                    // Detect a touch down event.
                    val pointerId = awaitFirstDown().id

                    horizontalDrag(pointerId) { change ->
                        // Update the animation value with touch events.
                        launch {
                            offsetX.snapTo(
                                offsetX.value + change.positionChange().x
                            )
                        }
                        velocityTracker.addPosition(
                            change.uptimeMillis,
                            change.position
                        )
                    }
                }
                // No longer receiving touch events. Prepare the animation.
                val velocity = velocityTracker.calculateVelocity().x
                val targetOffsetX = decay.calculateTargetValue(
                    offsetX.value,
                    velocity
                )
                // The animation stops when it reaches the bounds.
                offsetX.updateBounds(
                    lowerBound = -size.width.toFloat(),
                    upperBound = size.width.toFloat()
                )
                launch {
                    if (targetOffsetX.absoluteValue <= size.width) {
                        // Not enough velocity; Slide back.
                        offsetX.animateTo(
                            targetValue = 0f,
                            initialVelocity = velocity
                        )
                    } else {
                        // The element was swiped away.
                        offsetX.animateDecay(velocity, decay)
                        onDismissed()
                    }
                }
            }
        }
    }
        .offset { IntOffset(offsetX.value.roundToInt(), 0) }
}