简体   繁体   English

如何在ViewPager位置偏移更改时为视图设置动画

[英]How to animate views as the ViewPager position offset changes

We would like to create an app intro with animation where the user scrolls through pages and, as the user scrolls, a view is animated and travels through all the slides. 我们想创建一个带有动画的应用介绍,其中用户滚动页面,并且随着用户滚动,视图被动画化并遍历所有幻灯片。 The animated view should move as the user scrolls, so if the user scrolls faster the animated view should move faster and if the user scrolls back to a previous page, the animated view should move backwards. 动画视图应随着用户滚动而移动,因此,如果用户滚动得更快,动画视图应移动得更快,并且如果用户向后滚动到上一页,则动画视图应向后移动。

This is super easy in iOS with https://github.com/IFTTT/JazzHands but I can't find a way to do this in Android. 在iOS中使用https://github.com/IFTTT/JazzHands超级简单,但是我找不到在Android中执行此操作的方法。

Problems I've found: 我发现的问题:

  • Animations run in real time; 动画实时运行; you can't let the time be the ViewPager offset. 您不能让时间成为ViewPager的偏移量。
  • In FragmentPagerAdapter , views from one fragment can't be moved to another fragment. FragmentPagerAdapter ,不能将一个片段的视图移动到另一个片段。

Any suggestion? 有什么建议吗? Thank you. 谢谢。

This is a partial answer. 这是部分答案。 It's for the first problem. 这是第一个问题。

  • Animations run in real time; 动画实时运行; you can't let the time be the ViewPager offset. 您不能让时间成为ViewPager的偏移量。

I can change the properties of the animated view in the onPageScrolled method of OnPageChangeListener of the ViewPager . 我可以改变动画视图的属性在onPageScrolled的方法OnPageChangeListener所述的ViewPager

This is a simple example that changes the left margin of the animated view so it moves to the right. 这是一个简单的示例,它更改了动画视图的左边距,因此它向右移动。

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);

            LayoutParams params = (LayoutParams) animatedView.getLayoutParams();
            params.setMargins((int) ((position + positionOffset) * 500), 0, 0, 0);
            animatedView.setLayoutParams(params);
        }

The second problem is not solved, though. 但是,第二个问题没有解决。 When the view reaches the right side of the page it is in, it disappears. 当视图到达页面右侧时,它消失。 In other words, the view can't move to the next page (fragment). 换句话说,视图无法移至下一页(片段)。

For the second problem: 对于第二个问题:

The viewPagerAdapter will destroy the views when they are no more needed so to keep a view visible through all the pages you need add it to the layout that contains the ViewPager. 当不再需要视图时,viewPagerAdapter将销毁这些视图,以便使视图在所有需要的页面中可见,并将其添加到包含ViewPager的布局中。

Something like this can works: 这样的事情可以工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_transparent"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text out of view pager"
        android:textSize="25sp"
        />
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM