-2

出了点小问题。我的动画不工作。我没有收到任何错误。只是我的动画不起作用。

这是我的代码。

public class MainActivity extends FragmentActivity {

    private PagerAdapter mPagerAdapter;

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

        this.initialisePaging();
    }

    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager)super.findViewById(R.id.pager);
        pager.setAdapter(this.mPagerAdapter);
        pager.setPageTransformer(true, new ZoomOutPageTransformer());
    }

    public static class Tab1Fragment extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
            return (ImageView)inflater.inflate(R.layout.tab_frag1_layout, container, false);
        }
    }

    public static class Tab2Fragment extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
            return (ImageView)inflater.inflate(R.layout.tab_frag2_layout, container, false);
        }
    }

    public static class Tab3Fragment extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
            return (ImageView)inflater.inflate(R.layout.tab_frag3_layout, container, false);
        }
    }

    public static class PagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

    public static class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;

        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
//              view.setAlpha(0);
                ViewHelper.setAlpha(view, 0);

            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
//                  view.setTranslationX(horzMargin - vertMargin / 2);
                    ViewHelper.setTranslationX(view, horzMargin - vertMargin / 2);
                } else {
//                  view.setTranslationX(-horzMargin + vertMargin / 2);
                    ViewHelper.setTranslationX(view, -horzMargin + vertMargin / 2);
                }

                // Scale the page down (between MIN_SCALE and 1)
//              view.setScaleX(scaleFactor);
//              view.setScaleY(scaleFactor);
                ViewHelper.setScaleX(view, scaleFactor);
                ViewHelper.setScaleY(view, scaleFactor);

                // Fade the page relative to its size.
//              view.setAlpha(MIN_ALPHA +
//                      (scaleFactor - MIN_SCALE) /
//                      (1 - MIN_SCALE) * (1 - MIN_ALPHA));

                ViewHelper.setAlpha(view, MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE) * (1 - MIN_ALPHA));

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
//              view.setAlpha(0);
                ViewHelper.setAlpha(view, 0);
            }
        }
    }
}

其他一切工作正常。我想实现这一点。请帮我。

在我不使用 NineOldroids 实施之前。但即使在实施后,它也没有奏效。

4

2 回答 2

1

我也有这个问题,看看这个答案。使用 Nineoldandroid 应该可以工作,但 PageTransform 在 pre 11 (Heneycomb) 中被忽略了,尽管它在支持库中。我正在考虑移动到查看脚蹼只是为了做动画。但我仍在寻找另一种方式。

编辑:查看我在那个问题中的答案,看看我是如何解决这个问题的。我基本上复制了 viewpager 类并删除了在 setPageTransformer 中检查 SDK 版本的 if 语句

于 2013-07-11T11:41:06.937 回答
0

它在 android 2.2 平台的模拟器中不起作用,但是当我签入以 ICS 作为平台的 Phone Sony Xperia Tipo Dual 时,它起作用了。

于 2013-06-23T11:49:11.027 回答