4

I was looking for a long time (looking on third-party libraries also) to make some kind of "Parallax" but without Toolbar, All I've seen is working with Toolbar, but it's not in my best interest, because I removed Toolbar on whole application.

I followed this example: https://www.youtube.com/watch?v=HO82ES_RiSQ but it didn't convince me...

There's anybody who can resolve this issue? I would like to know how can I do Parallax without using Toolbar

Thanks in advance

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46

1 Answers1

5

You can do it in just three steps :)

  1. Add compile 'com.github.ksoichiro:android-observablescrollview:1.6.0' to your dependencies in build.gradle (project on github)
  2. Create layout with ImageView, scrollable content and ObservableScrollView as container.

    activity_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <com.github.ksoichiro.android.observablescrollview.ObservableScrollView      
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/observable_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:orientation="vertical">
    
           <ImageView
               android:id="@+id/image_view"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:adjustViewBounds="true"
               android:src="@drawable/example" />
           <View
               android:id="@+id/your_content"
               android:layout_width="wrap_content"
               android:layout_height="600dp"
               android:background="@android:color/black" />
       </LinearLayout>
    </com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
    
  3. Set ObservableScrollViewCallbacks in your ObservableScrollView and translate over y axis your ImageView in onScrollChange method

    public class MainActivity extends AppCompatActivity implements  ObservableScrollViewCallbacks {
        private ImageView imageView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_layout);
             ObservableScrollView observableScrollView = (ObservableScrollView) findViewById(R.id.observable_scrollview);
             observableScrollView.setScrollViewCallbacks(this);
             imageView = (ImageView) findViewById(R.id.image_view);
        }
    
        @Override
        public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
             imageView.setTranslationY(scrollY / 2);
        } 
    }
    

Very important is imageView.setTranslationY(scrollY / 2); which means that your ImageView is scrolling two times slower than your scrolling content.

And here is how it looks like:

Parallax image

lukjar
  • 7,220
  • 2
  • 31
  • 40
  • I had seen recently this code and It seems to be in my best interest (but I didn't see this example, all examples are using toolbar at main page) thanks you very much! – M. Mariscal Mar 23 '16 at 19:36
  • Hey friend! I'm getting some problems adding a new layout (I don't want to have Parallax effect this new one) instead of this black-view, can you help me please? – M. Mariscal Mar 29 '16 at 15:35
  • Hey please can you reply to me with an answer? thanks you very much! – M. Mariscal Apr 07 '16 at 15:55
  • What problem do you have? You can replace view with id android:id="@+id/your_content" with your layout. – lukjar Apr 08 '16 at 08:38
  • 1
    Why is the library needed for this? You could do scrollView.getViewTreeObserver().addOnScrollChangedListener on a normal scrollview – DennisVA Mar 21 '18 at 16:33
  • Nevermind, it seems that there are some flickering issues when not using the library – DennisVA Mar 21 '18 at 16:42
  • You can try to use NestedScrollView from support library instead ScrollView and it should also works – lukjar Mar 21 '18 at 22:39
  • @lukjar I found this solution but every ImageView and TextView that I had, when I got them inside the ObservableScrollView they got in random positions and I can't change their positions. Can you help me? – André Silva May 11 '18 at 10:27