0

我正在尝试在我的 ViewPager 上实现转换。我想添加一个动画,就像我翻书一样,但很简单。我看到一些带有 curl-animation 的帖子等等,但我不需要这样的东西。

有人可以帮助我实现它吗?

4

2 回答 2

0

您可以使用此库https://github.com/moritz-wundke/android-page-curl来实现所需的动画。

于 2017-03-09T06:00:42.570 回答
-1

我创建了这个 PageTransformer:

public class PageCurlTransformer extends BaseTransformer {

    private final Matrix OFFSET_MATRIX = new Matrix();
    private final Camera OFFSET_CAMERA = new Camera();
    private final float[] OFFSET_TEMP_FLOAT = new float[2];

    public static final String TYPE_CURL_CENTERED = "CENTERD";
    public static final String TYPE_CURL_TOP = "TOP";
    public static final String TYPE_CURL_BOTTOM = "BOTTOM";

    private boolean mRotation;
    private float mRotationCurlType = 0.5f;

    public PageCurlTransformer(boolean rotation) {
        mRotation = rotation;
        mRotationCurlType = 0.5f;
    }

    public PageCurlTransformer(String curlType) {
        mRotation = true;

        if (curlType.equals(TYPE_CURL_CENTERED))
            mRotationCurlType = 0.5f;
        else if (curlType.equals(TYPE_CURL_TOP))
            mRotationCurlType = 1.5f;
        else if (curlType.equals(TYPE_CURL_BOTTOM))
            mRotationCurlType = -0.5f;
    }

    @Override
    protected void onTransform(View view, float position) {
        //view.setTranslationX(position < 0 ? 0f : -view.getWidth() * position);

        if (position <= 0f) {

            view.setPivotX(position < 0 ? 0 : view.getWidth());
            view.setScaleX(position < 0 ? 1f + position : 1f - position);

            if (mRotation) {
                final float rotation = (position < 0 ? 30f : -30f) * Math.abs(position);

                view.setTranslationX(getOffsetXForRotation(rotation, view.getWidth(), view.getHeight()));

                view.setPivotY(view.getHeight() * mRotationCurlType);
                view.setRotationY(-rotation);
            }
        } else if (position <= 1f) {
            view.setTranslationX(position < 0 ? 0f : -view.getWidth() * position);
        }
    }

    protected final float getOffsetXForRotation(float degrees, int width, int height) {
        OFFSET_MATRIX.reset();
        OFFSET_CAMERA.save();
        OFFSET_CAMERA.rotateY(Math.abs(degrees));
        OFFSET_CAMERA.getMatrix(OFFSET_MATRIX);
        OFFSET_CAMERA.restore();

        OFFSET_MATRIX.preTranslate(-width * 0.5f, -height * 0.5f);
        OFFSET_MATRIX.postTranslate(width * 0.5f, height * 0.5f);
        OFFSET_TEMP_FLOAT[0] = width;
        OFFSET_TEMP_FLOAT[1] = height;
        OFFSET_MATRIX.mapPoints(OFFSET_TEMP_FLOAT);
        return (width - OFFSET_TEMP_FLOAT[0]) * (degrees > 0.0f ? 1.0f : -1.0f);
    }
}

BaseTransformer 在哪里:

public abstract class BaseTransformer implements PageTransformer {

    protected abstract void onTransform(View view, float position);

    @Override
    public void transformPage(View view, float position) {
        onPreTransform(view, position);
        onTransform(view, position);
    }

    protected boolean hideOffscreenPages() {
        return true;
    }

    protected boolean isPagingEnabled() {
        return false;
    }

    protected void onPreTransform(View view, float position) {
        final float width = view.getWidth();

        view.setRotationX(0);
        view.setRotationY(0);
        view.setRotation(0);
        view.setScaleX(1);
        view.setScaleY(1);
        view.setPivotX(0);
        view.setPivotY(0);
        view.setTranslationY(0);
        view.setTranslationX(isPagingEnabled() ? 0f : -width * position);

        if (hideOffscreenPages()) {
            view.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
        } else {
            view.setAlpha(1f);
        }
    }
}

我希望它对你有帮助。

于 2017-10-24T08:40:32.087 回答