Translating Objects along a Path (Part 2) | by Nikhil Mandlik
In the previous article, I explained how we can create a different path inside a canvas and animate shapes along that path.
In this session, I’ll explain how we can use the same concept to animate any compose view from one position to another on the screen.
Overview ?
- Understanding UI hierarchy
- Getting source and end destination offset
- Creating TranslatingObject Compose function
- Animating Multiple Objects along the same path
Understanding UI hierarchy ?
- TranslatingObject -> compose function containing Translating Emojis
- MyHorizontalList -> compose function containing Lazy Row, the second item in this list is our start destination
- MyHorizontalRow -> Row containing three boxes, the second box in this row is our end destination
All these compose functions are present inside a Box. with Box we can stack different views, so if we want to translate anything inside a box we just have to manage its offset value and z-index. Ill explain TranslatingObject compose function in a further section of this blog.
Getting source and end destination offset ?
In compose we have OnGloballyPositionedModifier, This modifier gives the final layout coordinates of any view after measuring is done. i have used the same modifier inside MyHorizontalList and MyHorizontalRow to collect start and end destination offset.
These offsets are stored as mutable state in MainScreen. we are also taking the size of view to find out the centre position of the view.
Modifier.onGloballyPositioned {
updateOffset(it.positionInRoot(), it.size)
}
Creating Translating Object Compose Function ?
The TranslatingObject compose function follows a slotting pattern. it takes content as a compose lambda. This content lambda is wrapped within one Box composable. To translate this content we just have to change the offset value of this parent box
Now the Next step will be to create a smooth curve between the start and end destination.
val pos = FloatArray(2)
val tan = FloatArray(2)
val pathToAnimate = Path().apply {
val verticalDistance = endDestination.y - startDestination.y
val horizontalDistance = endDestination.x - startDestination.x
//cubic-bezier(.71,0,.29,1)
val c1x = startDestination.x + (0.71 * horizontalDistance)
val c1y = startDestination.y - (0.71 * verticalDistance)
val c2x = startDestination.x + (0.29 * horizontalDistance)
val c2y = endDestination.y
moveTo(startDestination.x, startDestination.y)
cubicTo(
x1 = c1x.toFloat(),
y1 = c1y.toFloat(),
x2 = c2x.toFloat(),
y2 = c2y.toFloat(),
x3 = endDestination.x,
y3 = endDestination.y
)
}
once we have the path object with us, we can use the PathMeasure function to calculate the position of any point along that path using getPosTan method. here we are using Animatable to update animateOffset value.
PathMeasure().apply {
setPath(pathToAnimate.asAndroidPath(), false)
pathAnimatable.animateTo(1f, tween(1500)) {
getPosTan(length * this.value, pos, tan)
animationOffset.value = Offset(pos[0], pos[1])
onProgressUpdateListener(this.value)
}
}
Overall following are the major components in TranslatingObject compose function
- Box composable: wrapper around content lambda
- pathToAnimate: smooth bezier curve between start and end destination
- pathAnimatable: Animatable use to update animateOffset value
- animateOffset: mutable offset which will be applied to BOX
Animating Multiple Objects along a Path ?
I have used “for loop” over TranslatingObject compose function. this will create n no of translating objects. now we just have to update n value based on our requirements
Related Posts
Leave a Reply Cancel reply
Categories
- ! Без рубрики (1)
- ++PU (1)
- 1 (1)
- 1w (1)
- 1win Brazil (1)
- 1win India (1)
- 1WIN Official In Russia (1)
- 1win Turkiye (1)
- 1xbet egypt (1)
- 2ankarafayansustasi.net_may (1)
- ankarafayansustasi.netsiteai apr (1)
- Artificial intelligence (1)
- Arts & Entertainment, Photography (1)
- belugasitesi_mAY (1)
- BH_TOPsitesi apr (1)
- BHsitesy_may (2)
- Blog (3)
- Bookkeeping (14)
- Bootcamp de programação (2)
- Bootcamp de programación (2)
- BT_TOPsitesi apr (1)
- casino (5)
- casinom-hub (1)
- casinom-hub.comsitesi apr (3)
- colombian mail order brides (1)
- Cryptocurrency exchange (2)
- Dinamobet_next (1)
- Disease & Illness, Colon Cancer (1)
- Dumanbet (1)
- Dumanbet_next (1)
- Finance, Insurance (1)
- FinTech (5)
- Forex Trading (11)
- Galabet (1)
- Health & Fitness, Fitness Equipment (1)
- Hitbet (1)
- Home & Family, Crafts (1)
- Home & Family, Gardening (1)
- Internet Business, Audio-Video Streaming (1)
- Internet Business, Ecommerce (1)
- Internet Business, Email Marketing (1)
- Internet Business, Internet Marketing (1)
- IT Вакансії (1)
- IT Образование (5)
- IT Освіта (1)
- latin women dating (1)
- mail order bride (1)
- Mars bahis (2)
- Matadorbet (1)
- minimiri.comsitesi apr (3)
- Mobile App Development (771)
- Mostbet Russia (1)
- New Post (1)
- News (1)
- PB_TOPsitesi apr (1)
- PBsitesi_may (1)
- Pusulabet (1)
- redmirepool.bizsitesi apr (2)
- redmirepoolsitesi_may (1)
- Reference & Education, College (1)
- Reference & Education, Sociology (1)
- Rokusitesi apr (1)
- Sober living (6)
- Society, Divorce (1)
- Software development (7)
- Superbetin (1)
- Tempobet_next (1)
- thelongeststride.comsitesi apr (1)
- tipobet-turkiyesitesi apr (1)
- Ultrabet (1)
- Uncategorized (1)
- Игра (2)
- казино (1)
- Криптовалюты (1)
- Новости Криптовалют (1)
- Финтех (7)
- Форекс Брокеры (9)
- Форекс обучение (2)