简体   繁体   中英

ImageView in Fragment of ViewPager(animation / rotate)

I have ViewPager with some Fragments. In one of them I have ImageView. I want to rotate that ImageView gradually when user scroll the pages from one to another! Here below you can see example. I want to have same effect as in the second page here.

I tried to use these code, but it rotate instantly not gradually.

 @Override
 public void transformPage(View page, float position) {
        if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
        page.setAlpha(0);
        } else if (position <= 1) {
              ImageView imageView = (ImageView) page.findViewById(R.id.news_content);
              if (imageView != null) {
                  for(int angle=0; angle<180; angle++){
                            ViewCompat.setRotation(imageView, angle);
                  }
              }
          }
 }

在此处输入图片说明

Thanks for any help!

I'm trying to solve same problem )

Try this :

public class MyTransformer implements ViewPager.PageTransformer {

private static final double RADIUS = 200;
private static double originX;
private static double originY;

public void transformPage(View view, float position) {

    originX = 0; //  current position X // zero coz android:layout_centerInParent="true"

    originY = 0; // current position Y //

    TextView textView = (TextView) view.findViewById(R.id.tvPage);
    ImageView imageView = (ImageView) view.findViewById(R.id.iv_rotation);

    textView.setText(position + "");

    imageView.setRotation(360 * position);
    imageView.setTranslationY((float) (originY + Math.cos(360 * position * 0.02) * RADIUS));
    imageView.setTranslationX((float) (originX + Math.sin(360 * position * 0.02) * RADIUS));

}

}

X := originX + sin(angle)*Size,
Y := originY + cos(angle)*Size,
0.02 - rotation speed

MainActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(pagerAdapter);
    pager.setPageTransformer(true, new MyTransformer());

}

XML fragment:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/background">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_rotation"
        android:layout_alignParentLeft="false"
        android:layout_alignParentStart="false"
        android:src="@mipmap/ic_launcher"
        android:layout_alignWithParentIfMissing="false"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/tvPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="PAGE TEXT"
        android:layout_below="@+id/iv_rotation"
        android:layout_centerHorizontal="true">
    </TextView>
</RelativeLayout>

xD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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