在ViewPager Android中跨片段平滑转换视图

时间:2016-05-25 09:30:09

标签: android android-layout android-fragments android-animation

我必须做简单的翻译,我必须从左侧移动图像(活动和片段1在屏幕上) 现在,当我滚动页面(片段)时,片段1向左移动,片段2从右侧移动,图像也应从左向右翻译

activity_intro.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout"

tools:context="com.example.rahulkumarlohra.retrofitsample.Retro.Activity.IntroActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

<ImageView
    android:id="@+id/imageView1"
    android:src="@mipmap/plus_icon_blue_xxhdpi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="143dp" />


<ImageView
    android:id="@+id/imageView2"
    android:src="@mipmap/plus_icon_blue_xxhdpi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/viewPager"
    android:layout_alignEnd="@+id/viewPager"
    android:layout_marginTop="81dp" />

上面的代码是activity_intro.xml

我在ViewPager外面有一个imageView,我想在ViewPager.Right中的片段之间翻译这个imageView现在我能够使用viewPager.addOnPageChangeListener翻译这个imageView,但翻译并不顺畅

以下是viewPager.addOnPageChangeListener

的代码
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.d(TAG,"position:"+position);
            Log.d(TAG,"positionOffset:"+positionOffset);
            Log.d(TAG,"positionOffsetPixels:"+positionOffsetPixels);

            DecimalFormat df = new DecimalFormat("#.##");

            float movement = (Float.parseFloat(df.format(positionOffset)));

            int rlWidth = relativeLayout.getWidth();
            IntroActivity.c = movement;

            imageView1.animate().translationX(positionOffsetPixels).start();
            if(movement!=IntroActivity.c)
            {

                if(movement>IntroActivity.c)
                {
                    if(movement-IntroActivity.c>0.03)
                    {

                        imageView1.animate().translationX(rlWidth*movement).withLayer().start();
                        IntroActivity.c = movement;

                    }
                }else {

                    if(IntroActivity.c-movement>0.03)
                    {

                        imageView1.animate().translationX(rlWidth*movement).withLayer().start();
                        IntroActivity.c = movement;

                    }
                }
            }

        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG,"onPage Selected position:"+position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

1 个答案:

答案 0 :(得分:0)

Animation to show ImageView from left to right

OR

使用图像样式

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:fromXDelta="100%"
    android:toXDelta="0%"/> 

 <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%" android:toXDelta="100%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="600"/>