<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Max Yablochkin on Medium]]></title>
        <description><![CDATA[Stories by Max Yablochkin on Medium]]></description>
        <link>https://medium.com/@mr.yablochkin?source=rss-e4c9efe0b278------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*M48sOkVvY0wBgyhp</url>
            <title>Stories by Max Yablochkin on Medium</title>
            <link>https://medium.com/@mr.yablochkin?source=rss-e4c9efe0b278------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 20:43:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mr.yablochkin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Composing smooth animation transition with Pager in Jetpack Compose]]></title>
            <link>https://proandroiddev.com/composing-smooth-animation-transition-with-pager-in-jetpack-compose-f71455699a00?source=rss-e4c9efe0b278------2</link>
            <guid isPermaLink="false">https://medium.com/p/f71455699a00</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[ux]]></category>
            <dc:creator><![CDATA[Max Yablochkin]]></dc:creator>
            <pubDate>Sun, 16 Feb 2025 23:06:21 GMT</pubDate>
            <atom:updated>2025-03-30T21:55:01.408Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6YS9GcbsxbW3O3fux2FuFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*mRKtxKjcYoejlPn6I5fbxQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*8FNSOFPn36FZc2JgS0QWYA.gif" /></figure><p>A dynamic, responsive UI with an animated gradient that changes as you scroll and smooth transitions between cards for both <a href="https://drive.google.com/file/d/1MT6GbyC3QWmVugDMjF_ZaWpLwIxysmny/view?usp=sharing">light</a> and <a href="https://drive.google.com/file/d/1SqYz0dDgPiBFFw-DgafZNmHKSH__dolt/view?usp=sharing">dark</a> themes.</p><p>In this article, you’ll learn how to create this screen with minimal code using HorizontalPager and other Jetpack Compose features.</p><h4><strong>Configuring PagerState and Determining Page Offset</strong></h4><p><a href="https://developer.android.com/reference/kotlin/androidx/compose/foundation/pager/PagerState">PagerState</a> provides an API for managing page scroll state, including:</p><blockquote>pageCount — total number of pages</blockquote><blockquote>currentPage — the currently displayed page</blockquote><blockquote>targetPage — the target page during a scroll</blockquote><blockquote>settledPage — the final settled page after scrolling</blockquote><blockquote>scrollToPage — jumps to a page without animation</blockquote><blockquote>animateScrollToPage — smoothly animates the scroll to a page</blockquote><p>The key function we need is<em> </em><a href="https://developer.android.com/reference/kotlin/androidx/compose/foundation/pager/PagerState#getOffsetDistanceInPages(kotlin.Int)"><em>getOffsetDistanceInPages()</em></a>, which calculates the offset of a given page. By passing any page to this function, we can determine its scroll offset relative to the specified starting position.</p><h4>Let’s get started composing!</h4><p>We start by creating a PagerState with the desired number of pages:</p><pre>val pagerState = rememberPagerState { 3 }</pre><p>To determine the offset of the current page, we use:</p><pre>val pageOffset = pagerState.getOffsetDistanceInPages(currentPage)</pre><blockquote>0 : The page is centered and fully visible</blockquote><blockquote>0.5 : The page is halfway shifted to the right and may be partially covered on the right</blockquote><blockquote>-0.5 : The page is halfway shifted to the left and may be partially covered on the left</blockquote><blockquote>1 : The page is fully shifted past the right edge and is almost invisible</blockquote><blockquote>-1 : The page is fully shifted past the left edge and is almost invisible</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*CWE_6GCkmiy8nEL-O2mX-w.gif" /></figure><p>This offset can be used to create various effects and transition animations between pages, depending on the scroll state.</p><h4>Creating the Basic Layout</h4><p>First, we’ll build the layout using Scaffold, a linear gradient, a list with HorizontalPager, and card representations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1be19a28d35762e45794db28c7e6aa95/href">https://medium.com/media/1be19a28d35762e45794db28c7e6aa95/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad66f7d3a550352187ad60af54b34f04/href">https://medium.com/media/ad66f7d3a550352187ad60af54b34f04/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qx6xRmIG21FnDd5UEhqeOA.png" /></figure><p>To render the gradient in both light and dark themes, we could simply use the background modifier, since theme changes are infrequent and the gradient is static.</p><p>However, in our case, the color needs to change dynamically based on the scroll offset. Therefore, we should use the drawWithCache modifier to cache the Brush object, as recomputing it repeatedly is expensive.</p><p>Alternatively, we can use the remember function for caching instead of drawWithCache.</p><p>Since the gradient will be actively changing its color in the background, we need to use one of the following drawing modifiers:</p><blockquote>drawBehind</blockquote><blockquote>drawWithContent</blockquote><blockquote>drawWithCache</blockquote><p>Using these drawing modifiers helps avoid unnecessary recompositions, as the drawing will occur in the <strong>Render</strong> phase rather than the <strong>Composition</strong> phase, as it would with the background modifier.</p><h4>Creating Effects and Animations</h4><p>To achieve specific motion physics for elements, we need to link the scroll offset with the drawing layer using the <a href="https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/package-summary#(androidx.compose.ui.Modifier).graphicsLayer(kotlin.Function1)"><em>graphicsLayer { }</em></a> modifier.</p><p>It is highly recommended to use graphicsLayer with a lambda, as this allows us to work directly in the Render phase, avoiding unnecessary recompositions.</p><p>Let’s create an extension function for the modifier to make it reusable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5db94285cce1f33257c3f4cfc5dc504e/href">https://medium.com/media/5db94285cce1f33257c3f4cfc5dc504e/href</a></iframe><p>For smooth animations of <strong>scale</strong> and <strong>alpha</strong> during page transitions, the <strong>lerp</strong> function is used to perform linear interpolation. For example, with scaleX and scaleY, it decreases the size as the page moves away from the center. At the center, the scale is 1f, and at the edges, it reduces to 0.7f, creating a depth effect and a smooth transition between elements on adjacent screens.</p><p>To achieve a natural page-flipping effect, the <strong>EaseOutQuad</strong> easing function is applied. This makes the movement fast at the beginning and gradually slows down towards the end, creating a fluid transition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*olK4-CycibFOPNrbCgwqhw.gif" /><figcaption><a href="https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#EaseOutQuad()">source</a></figcaption></figure><p><strong>pageOffset * size.width / 2f</strong> — calculates how much the object needs to be shifted. Dividing by 2f reduces the amplitude of the offset to ensure the effect isn’t too harsh.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7db72d77342ba01e42b8126cd4229934/href">https://medium.com/media/7db72d77342ba01e42b8126cd4229934/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*swyJU8gHONoTspMgF_tlHA.gif" /></figure><h4>Now, let’s begin creating the transition for the cards</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b163ff90fa01a7a3bc3d5c2635377221/href">https://medium.com/media/b163ff90fa01a7a3bc3d5c2635377221/href</a></iframe><blockquote>Left Y rotation → rotation value * pageOffset.absoluteValue</blockquote><blockquote>Right Y rotation → -rotation value * pageOffset.absoluteValue</blockquote><blockquote>Left Z rotation → -rotation value * pageOffset.absoluteValue</blockquote><blockquote>Right Z rotation → rotation value * pageOffset.absoluteValue</blockquote><p>In the case of visible offset for the current page to the right, artifacts of the card offset on the adjacent page to the right may appear. To avoid this, we should handle the case when pageOffset &gt;= 1f.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e41a4520306fad6a0b04dd0ef314661c/href">https://medium.com/media/e41a4520306fad6a0b04dd0ef314661c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*t0Ayhf4Ztdw8D1v5S5GXGA.gif" /></figure><h4>Changing the gradient colors</h4><p>We will again use the getOffsetDistanceInPages function from PagerState to determine the distance from the current page to the pages with indices 1 and 2. These values, stored in the distanceTo1Page and distanceTo2Page variables, indicate how far each of these pages is from the current position. In these lists, we modify the red and green components of the first two elements using the values of distanceTo1Page and distanceTo2Page. This results in a change in color hue based on the distance to the corresponding pages, creating a smooth color transition effect as the pages are scrolled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c1d3486cfbf9099aa519c7d03a6c104/href">https://medium.com/media/0c1d3486cfbf9099aa519c7d03a6c104/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*dmY3Q-0hAguXkrN4CjWmBA.gif" /></figure><p>To create the blur effect on the top bar, we will use the <a href="https://chrisbanes.github.io/haze">Haze</a> library, which provides the ability to create various glassmorphism effects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*wxzKT1ZsIGXzkAKwWI2Wtg.gif" /></figure><h3>Happy Composing 😉</h3><p>Follow me on <a href="https://www.linkedin.com/in/max-yablochkin-434459281/">LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f71455699a00" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/composing-smooth-animation-transition-with-pager-in-jetpack-compose-f71455699a00">Composing smooth animation transition with Pager in Jetpack Compose</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>