可以在 Android 上没有 ToolBar 的情况下制作视差效果吗? [英] Could be possible to make Parallax effect without ToolBar on Android?

查看:57
本文介绍了可以在 Android 上没有 ToolBar 的情况下制作视差效果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找了很长时间(也在寻找第三方库)来制作某种视差",但没有 Toolbar,我所看到的只是使用 Toolbar,但这不符合我的最大利益,因为我在整个应用程序中删除了 Toolbar.

我遵循了这个例子:

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

解决方案

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:

这篇关于可以在 Android 上没有 ToolBar 的情况下制作视差效果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆