【问题标题】:ImageView in Fragment of ViewPager(animation / rotate)ViewPager片段中的ImageView(动画/旋转)
【发布时间】:2015-02-28 17:21:53
【问题描述】:

我有带有一些片段的 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);
                  }
              }
          }
 }

感谢您的帮助!

【问题讨论】:

  • 更新了,看下面)

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


【解决方案1】:

我正在尝试解决同样的问题)

试试这个:

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(角度)*尺寸,
0.02 - 旋转速度

主活动:

 @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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多