ViewPager片段中的ImageView(动画/旋转)

时间:2015-02-28 17:21:54

标签: android android-activity android-fragments android-viewpager android-animation

我有ViewPager和一些片段。在其中一个我有ImageView。当用户将页面从一个页面滚动到另一个页面时,我想逐渐旋转ImageView!在下面你可以看到例子。我想在这里与第二页有相同的效果。

我尝试使用这些代码,但它不会逐渐旋转。

 @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);
                  }
              }
          }
 }

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我试图解决同样的问题)

试试这个:

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(角度)*尺寸,
Y:= originY + cos(角度)*尺寸,
0.02 - 转速

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片段:

<?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