48

Does anyone know how can I achieve the new parallax scrolling effect - you can see the effect when you open an app on the PlayStore and try to scroll down, the content goes over the top image. How can I achieve that?

enter image description here

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • 1
    just 100 or so lines of code, see: http://gist.github.com/pskink/d7089e5a82c970917be1 and sample implementation: http://gist.github.com/pskink/d7f0673b77fe8537b270 – pskink May 18 '15 at 13:09

6 Answers6

61

Google has recently announced Design support library and with this it has support for implementing Collapsing Toolbar.

In addition to pinning a view, you can use app:layout_collapseMode="parallax" (and optionally app:layout_collapseParallaxMultiplier="0.7" to set the parallax multiplier) to implement parallax scrolling (say of a sibling ImageView within the CollapsingToolbarLayout)

Example:

<android.support.design.widget.AppBarLayout
        android:layout_height="192dp"
        android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
AnV
  • 2,794
  • 3
  • 32
  • 43
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 1
    This is the preferred way. No need for 3rd party! – Chad Bingham Sep 05 '15 at 17:53
  • @Paresh Mayani i have kind of similar problem, can you please take a look at it, http://stackoverflow.com/questions/34760578/implement-globe-weis-file-folder-in-android thanks – Sasa Jan 13 '16 at 07:53
29

You could try this (FadingActionBar library): https://github.com/ManuelPeinado/FadingActionBar

Try an example of this library on android: https://play.google.com/store/apps/details?id=com.manuelpeinado.fadingactionbar.demo

EDIT: Rather than third party library use this AppBarLayout and CollapsingToolbarLayout http://android-developers.blogspot.in/2015/05/android-design-support-library.html

Yuraj
  • 3,185
  • 1
  • 23
  • 42
19

try the ObservableScrollView library

https://github.com/ksoichiro/Android-ObservableScrollView

demo application from play store

https://play.google.com/store/apps/details?id=com.github.ksoichiro.android.observablescrollview.samples2

sample demo,

enter image description here

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • 7
    Why to suggest 3rd party library when Google has already announced [Design support library](http://android-developers.blogspot.in/2015/05/android-design-support-library.html)! – Paresh Mayani Jun 15 '15 at 06:31
  • 6
    @PareshMayani Thanks for info..But why downvote only for me?. all the answers are give 3rd party library including accepted answer.. – Ranjithkumar Jun 15 '15 at 06:52
  • Because you have posted answer after Google has already announced an official support library! – Paresh Mayani Jun 15 '15 at 06:53
  • 6
    Material design library doesn't support fling gesture. Bad experience with scrollview! I'll try this library. – Muzaffer Nov 30 '15 at 18:11
  • 2
    It`s any necessary we only use the official methods.. If better than official we can go for 3rd party..for ex Networking library **Retrofit** faster than official library.Then what is our choice? – Ranjithkumar Apr 16 '16 at 06:42
  • 1
    Certainly an upvote because the cost of adding *official design support library* to the gradle dependency certainly equals the cost of adding 3rd party gradle dependency, sometimes with the added benefits of far greater support as mentioned by @wisemann – ShayHaned Feb 03 '18 at 22:17
4

There's a library called FadingActionBar that does exactly what you're asking for. You can find the library on GitHub (click) and a Demo-Application in the Play Store (click).

Usage would be something like this:

FadingActionBarHelper helper = new FadingActionBarHelper()
    // Set the ActionBar drawable - basically the color
    .actionBarBackground(R.drawable.ab_background)
    // Set the Header - usually an image
    .headerLayout(R.layout.header)
    // Set the main layout
    .contentLayout(R.layout.activity_scrollview);
setContentView(helper.createView(this));
helper.initActionBar(this);
reVerse
  • 35,075
  • 22
  • 89
  • 84
4

Actually few minutes after posting this question I bumped on two of libraries that do the effect I'm looking for and even more.

Here are links to them:

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
0

You may custom the parallax animation by tracking the Recycler View Scrolling

Firstly in the image view layout. Set the parent layout is smaller than image view so that prevent the image outside the bound when set translationY

<android.support.percent.PercentRelativeLayout
        android:id="@+id/index_level6_image_section"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:clipChildren="false">

        <ImageView
            android:id="@+id/index_level6_parallaxImage"
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:layout_centerInParent="true"
            android:background="@color/timberwolf"
            android:layout_marginTop="-20"
            android:layout_marginBottom="-20"
            android:scaleType="centerCrop"
            app:imageUrl="@{level6CellViewModel.level6ImageUrl}" />

    </android.support.percent.PercentRelativeLayout>

After that, track the recycler view scrolling effect and transitionY the image view.

*** I am using rxbinding and kotlin for implementation. You may use traditional listening method and java approach with the same idea.

RxRecyclerView.scrollEvents(recyclerView)
                    .subscribe { event ->
                        // get the visible cell items of the recycler view
                        val firstVisible = layoutManager.findFirstVisibleItemPosition()
                        val visibleCount = Math.abs(firstVisible - layoutManager.findLastVisibleItemPosition())

                        /** loop the visible cell items from the recycler view */
                        for (i in firstVisible..firstVisible + visibleCount) {
                            event.view().layoutManager?.findViewByPosition(i)?.let { cellItem ->
                                /** only for index cell level 6 parallax image */
                                cellItem.findViewById(R.id.index_level6_parallaxImage)?.let { imageView ->
                                    /** setting the parallax effect */
                                    val translationY = (cellItem.top - cellItem.height) / level6ParallaxRate
                                    imageView.translationY = -translationY
                                }
                            }
                        }
                    }
Kit
  • 2,370
  • 13
  • 11