6

我正在使用 viewPager 和 Fragments,现在我正在尝试获取自定义动画,精确的缩小页面转换。

但是我遇到了一些错误。我会将错误与代码片段一起放在注释中。

类 MainActivity

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);/** Getting a reference to the ViewPager defined the layout file */
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

        // **ERROR:The method setPageTransformer(boolean, ViewPager.PageTransformer) in the type ViewPager is not applicable for the arguments (boolean, ZoomOutPageTransformer)**
        pager.setPageTransformer(true, new ZoomOutPageTransformer());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

类 ZoomOutPageTransformer

import android.view.View;
    // **ERROR on 'ViewPager.PageTransformer --> ViewPager cannot be resolved to a type **
    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static float MIN_SCALE = 0.85f;
        private static 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

5 回答 5

6

我已经解决了这个问题:

  1. 下载最新的Android SDK
  2. 浏览到android-sdk-windows\extras\android\support\v4
  3. android-support-v4.jar复制到项目中的 libs 文件夹中
  4. 清理你的项目
  5. 运行你的代码

这对我有用,通过 ecplise 下载支持库给了我这个错误。这为我解决了问题

于 2013-06-01T18:01:36.977 回答
2

@mXX 几乎是对的。但是对于那些仍然有问题的人(比如我),我需要在他的程序中添加一点。

您需要更新您的 Android 支持库包。但是,如果您现在拥有它,则需要先将其卸载,然后重新安装才能获得新的。

如果您有 IntellijIdea:

  1. 从“工具/Android/SDK 管理器”打开 SDK 管理器窗口
  2. 找到 Exteras 文件夹,然后检查 Android 支持库。如果其状态为已安装,请单击“删除 1 个包...”以将其删除。
  3. 检查此软件包并单击安装按钮。
  4. 导航到 \extras\android\support\v4 然后复制 android-support-v4.jar 并将其粘贴到您的 Libs 文件夹中。
  5. Idea 应该自动重新编译项目,但是如果它没有这样做,那么您应该通过单击“Build/Rebuild Project”手动重新编译它

而已 ;)

于 2013-07-19T09:06:11.633 回答
1

对于仍然坚持这一点的人来说,当前支持 v4 问题不导入 v4 ViewPager 接口:它不需要静态 ViewPager 类实例。只需实现 PageTransformer。PageTransformer 接口已经用 ViewPager 封装在里面。

public class ZoomOutPageTransformer implements PageTransformer {

    private static float MIN_SCALE = 0.85f;
    private static 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);
        }
    }
}
于 2015-03-08T03:19:09.893 回答
0

更新 android support v4 库后,您可以从 eclipse 中删除该项目。然后再次导入项目。做干净的构建,一切都会好起来的!

于 2013-03-21T02:45:35.483 回答
0

只需将compile 'com.android.support:support-v13:23.1.1'添加到build.gradle中的依赖项(截图

于 2016-04-20T19:28:58.510 回答