Android中的垂直视差滚动

时间:2015-05-04 19:59:34

标签: android parallax

我想在没有使用外部库的情况下在Android中实现视差滚动。无论如何,在谷歌游戏商店中实现这一点。 感谢

2 个答案:

答案 0 :(得分:3)

RecyclerView

中实现这一点
public class MyScrollListener extends RecyclerView.OnScrollListener {

    private int totalScrollDistance = 0;
    private View parallax;

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        totalScrollDistance += dy;
        if (parallax == null) {
            parallax = recyclerView.getChildAt(0);
        }
        parallax.setTranslationY(totalScrollDistance/2);
    }
}

设定:

mRecyclerView.setOnScrollListener(new MyScrollListener());

要在ScrollView中实现该功能,您必须实施OnScrollListener

public class MyScrollView extends ScrollView {

    private int lastScrollY;

    private OnScrollListener listener;

    public void setOnScrollListener(OnScrollListener listener) {
        this.listener = listener;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int scrollY = getScrollY();
            if (lastScrollY != scrollY) {
                lastScrollY = scrollY;
                // update when lastScrollY != scrollY and keep sending message till lastScrollY == scrollY
                handler.sendMessageDelayed(handler.obtainMessage(), 5);
            }
            if (listener != null) {
                listener.onScroll(scrollY);
            }
        }
    };

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        lastScrollY = getScrollY();
        if (listener != null) {
            listener.onScroll(lastScrollY);
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            //might still scrolling after touching, use a handler to handle it
            handler.sendMessageDelayed(handler.obtainMessage(), 5);
        }
        return super.onTouchEvent(ev);
    }    

    public interface OnScrollListener {
        void onScroll(int dy);
    }
}

然后执行我们在OnScrollListener的{​​{1}}中所做的事情:

RecyclerView

答案 1 :(得分:0)

对我来说,以下方法可以解决。

在您的android布局中,提及以下代码:`

     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:fitsSystemWindows="true">

      <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
      >

      <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        >


        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >
    //place the widgets that you want to collapse .....(here i have used an 
       ImageView)

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <ImageView

                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/imgViewFirst"
                android:src="@drawable/firstImage"
                android:scaleType="fitXY"/>


            </LinearLayout>

        </android.support.design.widget.CollapsingToolbarLayout>

      //place the widgets that you want to pin after scrolling up.....

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/lightGrey"/>


    </android.support.design.widget.AppBarLayout>

   //place the widgets you want to show after you scrolled upwards...(here i have 
       used an ImageView)

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/imgViewSecond"
            android:src="@drawable/secondImage"
            android:scaleType="fitXY"/>

    </android.support.v4.widget.NestedScrollView>

     </android.support.design.widget.CoordinatorLayout>


    </LinearLayout>`