The AppBar is an evolution of the ActionBar which provides us with a rich toolbox for defining and customising the behaviour. All apps will have different requirements for their AppBars and each app may have different AppBar implementations in different Activities. In this series, rather than look at the AppBar implementations themselves, we’re going to explore how can integrate and animate different elements of the AppBar to provide smooth UI and UX which implement material design style animations and transitions.
The next thing that would be nice would be to add some motion to our feature image transitions. However, because of the relatively large images being used, simply tracking the movement of the ViewPager will be a little too much movement, In my opinion. What would be much nicer is a rather more subtle movement. The effect that we’re going to go for is a parallax transition which matches the direction of the ViewPager movement, but at a slower speed to soften the effect somewhat.
Before we get stuck in to the details it is worth pointing out that the inspiration for this came from a Google+ post from Kirill Grouchnikov which detailed some upcoming changes in the Google Play Store app. There is a nice movement and cross-fade animation, but I wasn’t keen on the hard edges because the images are only matching the screen width, so I was keen to widen the images so that these hard edges are not vsible.
The first thing we need to do is think about the size of the image. We want each image to be able to move a small amount to the left and right for the transition so we need a little bit of the image on each side to be positioned outside the screen bounds. So we really need to make the image a little larger than the screen itself. Typically we would need to create a custom View to calculate the width based upon the display size. However, the somewhat new (at the time of writing) Percent support library allows us to specify a child whose width is 120% that of its parent ViewGroup. If we then centre this within the parent we’ll get 10% of the image either side falling outside the screen dimensions, and we can use this for our transition.
So the two images that we are using for the fade transition have a common parent which is a FrameLayout. If we replace this with a PercentFrameLayout we can get what we need:
<?xml version="1.0" encoding="utf-8"?>
So the parent PercentFrameLayout is still what gets collapsed by the CollapsingToolbarLayout (meaning that both images will be collapsed together). We change the
android:layout_width of both images to
0dp and then set
app:layout_widthPercent="120%" to size each image to be 20% larger than the parent.
android:layout_gravity="center_horizontal" then centres the images within the parent giving a 10% overlap either side.
So with remarkably little effort we have gone from an image whose width matches the parent:
To one which has 10% of it’s width falling off screen on either side:
So now that we have this additional space either side we can actually perform the movement during the transition and we’ll cover than in the next article.
The source code for this article is available here.
© 2015, Mark Allison. All rights reserved.
AppBar – Part 3 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.