Could be possible to make Parallax effect without ToolBar on Android? [closed]

这一生的挚爱 提交于 2020-01-04 02:15:06

问题


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


回答1:


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:



来源:https://stackoverflow.com/questions/36184778/could-be-possible-to-make-parallax-effect-without-toolbar-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!