可以在 Android 上没有 ToolBar 的情况下制作视差效果吗?

M. *_*cal 4 java android parallax

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

我按照这个例子:https : //www.youtube.com/watch?v=HO82ES_RiSQ但它并没有说服我......

有谁能解决这个问题吗?我想知道如何在不使用的情况下进行视差Toolbar

提前致谢

luk*_*jar 5

您只需三步即可完成:)

  1. 添加compile 'com.github.ksoichiro:android-observablescrollview:1.6.0'build.gradle 中的依赖github 上的项目
  2. 使用 ImageView、可滚动内容和ObservableScrollView作为容器创建布局。

    活动布局.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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. ObservableScrollView中设置ObservableScrollViewCallbacks并在 onScrollChange 方法中在 y 轴上平移 ImageView

    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);
        } 
    }
    
    Run Code Online (Sandbox Code Playgroud)

非常重要的是imageView.setTranslationY(scrollY / 2); 这意味着您的 ImageView 滚动速度比滚动内容慢两倍。

这是它的样子:

视差图像