5

我需要在应用程序上为 ViewPager 编写自定义转换android:minSdkVersion="9"

我正在尝试运行ZoomOutPageTransformer给定的here

我没有编译错误,在运行 Android 2.3.3 (API 10) 的三星 Galaxy S 上,这条线

pager.setPageTransformer(true, new ZoomOutPageTransformer());

没有给出任何错误,但 ZoomOutPageTransformer 中的代码transformPage永远不会执行。

如果我设置android:minSdkVersion="11"和使用我的 Nexus 4 (API 17),代码可以完美运行。

这些 PageTransformer 可以与 API 9 和 10 一起使用吗?否则,我该如何创建自定义转换ViewPager

编辑

这是我使用的代码:

public class HomeFragment extends Fragment{ 

    ViewPager mNewsTitlePager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.home_content, container, false);
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState){

        super.onActivityCreated(savedInstanceState); 

        mNewsTitlePager = (ViewPager) getView().findViewById(R.id.news_title_pager);
        mNewsTitlePager.setPageTransformer(true, new ZoomOutPageTransformer());

        mNewsTitlePager.setAdapter(new NewsPagerAdapter(news_item_list));


    }

    private class NewsPagerAdapter extends PagerAdapter {

        private List<News> news_item_list;
        private LayoutInflater inflater;

        NewsPagerAdapter(List<News> news_item_list) {
            this.news_item_list = news_item_list;
            inflater = getActivity().getLayoutInflater();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void finishUpdate(View container) {
        }

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

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            TextView news_title = (TextView) inflater.inflate(R.layout.news_title_item, view, false);

            news_title.setText(news_item_list.get(position).title);

            ((ViewPager) view).addView(news_title, 0);
            return news_title;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }


    }

    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private float MIN_SCALE = 0.85f;
        private 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);

            } 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);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }

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

                // Fade the page relative to its size.
                view.setAlpha(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);
            }
        }

    }


}
4

3 回答 3

10

我从这里复制了 ViewPager 类并删除了该版本的 if 语句并将其命名为 TransformableViewPager

public void setPageTransformer(boolean reverseDrawingOrder, PageTransformer transformer) {
    if (Build.VERSION.SDK_INT >= 11) {
        final boolean hasTransformer = transformer != null;
        final boolean needsPopulate = hasTransformer != (mPageTransformer != null);
        mPageTransformer = transformer;
        setChildrenDrawingOrderEnabledCompat(hasTransformer);
        if (hasTransformer) {
            mDrawingOrder = reverseDrawingOrder ? DRAW_ORDER_REVERSE : DRAW_ORDER_FORWARD;
        } else {
            mDrawingOrder = DRAW_ORDER_DEFAULT;
        }
        if (needsPopulate) populate();
    }
}

我还必须将所有 PageTransformer 更改为 ViewPager.PageTransformer。然后像这样在 ZoomOutPageTransformer 中进行转换,

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
    // > 11 version
    view.setAlpha(0);
}
else
{
    // Nine Old Androids version
    ViewHelper.setAlpha(view, 0);
 }

这甚至适用于 2.2

于 2013-07-11T13:23:41.473 回答
2

None of the answers above are the correct answer even if they work, Here is mine, do not forget to add nineoldandroids.jar to your libs folder:

 public 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) {
            ViewHelper.setAlpha(view, 0);
        } else if (position <= 1) { // [-1,1]
            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) {
                ViewHelper.setTranslationX(view,horzMargin - vertMargin / 2);
            } else {
                ViewHelper.setTranslationX(view,-horzMargin + vertMargin / 2);
            }
            ViewHelper.setScaleX(view,scaleFactor);
            ViewHelper.setScaleY(view, scaleFactor);
            ViewHelper.setAlpha(view, MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
        } else {
            ViewHelper.setAlpha(view,0);
        }
    }
}
于 2014-04-01T09:29:37.667 回答
2

好的:我在文档的开头得到了答案:

As property animation is only supported as of Android 3.0 and forward, setting a PageTransformer on a ViewPager on earlier platform versions will be ignored.

我会ViewFlipper改用。

于 2013-04-15T16:24:27.303 回答