Android动画翻盖和缓解(打开书本动画)

时间:2015-08-18 08:53:36

标签: android animation android-animation

那里有几个按钮/图像。点击我想要这样的动画:

(图像打开)书籍封面打开,相关活动/片段在动画中轻松打开,全屏显示。

有什么想法吗?

在烹饪应用iOS,烹饪应用链接中发生了类似的事情:https://itunes.apple.com/us/app/cook/id687560846?mt=8

P.S:我已添加动画,gif将在完全加载后不间断运行。

animation

2 个答案:

答案 0 :(得分:3)

您可以使用标准的Android动画制作工具来实现此动画,动画书作为一组多个ImageViews - 每个用于页面表示:Cover,BackCover,FirstPage。并且在动画完成后启动活动或显示片段。

动画:

  • x和y比例,ObjectAnimator.ofFloat(mCover, "scaleY", ...)
  • x和y变换,ObjectAnimator.ofFloat(mCover, "x", ...)
  • y-rotation ObjectAnimator.ofFloat(mCover, "rotationY", ...)

参见我的例子

(当然,这个示例需要一些优化/修复,但为了更好地理解就足够了):

public class MainActivity extends AppCompatActivity {

    private ImageView mPage1;
    private ImageView mCover;
    private ImageView mCoverFullScreen;

    private AnimatorSet mAnimIncrease;

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

        mCoverFullScreen = (ImageView) findViewById(R.id.cover_full_screen);
        mPage1 = (ImageView) findViewById(R.id.page1);
        mCover = (ImageView) findViewById(R.id.cover);
        mCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAnimIncrease.start();
            }
        });

        mCoverFullScreen.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mCover.getMeasuredHeight() <= 0 || mCoverFullScreen.getMeasuredHeight() <= 0) {
                    return;
                }
                if (Build.VERSION.SDK_INT >= 16) {
                    mCoverFullScreen.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mCoverFullScreen.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                initAnimator(1000);
            }
        });
    }

    private void initAnimator(long animationDuration) {

        mAnimIncrease = new AnimatorSet();
        TimeInterpolator interpolator = new LinearInterpolator();

        float deltaX = mCover.getMeasuredWidth() / 2f;
        float deltaY = mCoverFullScreen.getY() - mCover.getY();
        float scale = mCoverFullScreen.getMeasuredHeight() / (float) mCover.getMeasuredHeight();
        float scaleMiddle = (scale + 1) / 2f;

        float xStart = mCover.getX();
        float xEnd = xStart + deltaX;
        float xMiddle = xStart + deltaX * interpolator.getInterpolation(0.5f);
        float xScaledEnd = xStart + deltaX * scale;

        float yStart = mCover.getY();
        float yEnd = yStart + deltaY;
        float yMiddle = yStart + deltaY * interpolator.getInterpolation(0.5f);

        AnimatorSet coverFrontSet = new AnimatorSet();
        coverFrontSet.setDuration(animationDuration / 2);
        coverFrontSet.playTogether(
                ObjectAnimator.ofFloat(mCover, "rotationY", 0f, -90f),
                ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
                ObjectAnimator.ofFloat(mCover, "x", xStart, xMiddle),
                ObjectAnimator.ofFloat(mCover, "y", yStart, yMiddle),
                ObjectAnimator.ofFloat(mCover, "scaleY", 1, scaleMiddle),
                ObjectAnimator.ofFloat(mCover, "scaleX", 1, scaleMiddle)
        );
        coverFrontSet.addListener(new AnimatorListenerStub() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mCover.setImageResource(R.drawable.cover);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mCover.setImageResource(R.drawable.cover_back);
            }
        });

        AnimatorSet coverBackSet = new AnimatorSet();
        coverBackSet.setDuration(animationDuration / 2);
        coverBackSet.playTogether(
                ObjectAnimator.ofFloat(mCover, "rotationY", -90f, -180f),
                ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
                ObjectAnimator.ofFloat(mCover, "x", xMiddle, xEnd),
                ObjectAnimator.ofFloat(mCover, "y", yMiddle, yEnd),
                ObjectAnimator.ofFloat(mCover, "scaleY", scaleMiddle, scale),
                ObjectAnimator.ofFloat(mCover, "scaleX", scaleMiddle, scale)
        );

        AnimatorSet coverSet = new AnimatorSet();
        coverSet.play(coverBackSet).after(coverFrontSet);

        AnimatorSet page1Set = new AnimatorSet();
        page1Set.setDuration(animationDuration);
        page1Set.playTogether(
                ObjectAnimator.ofFloat(mPage1, "scaleX", 1, scale),
                ObjectAnimator.ofFloat(mPage1, "scaleY", 1, scale),
                ObjectAnimator.ofFloat(mPage1, "x", xStart, xScaledEnd)
        );

        mAnimIncrease.play(coverSet).with(page1Set);
        mAnimIncrease.setInterpolator(interpolator);
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity"
    >

    <ImageView
        android:id="@+id/page1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/page1"
        />

    <ImageView
        android:id="@+id/cover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/cover"
        />

    <ImageView
        android:id="@+id/cover_full_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

Cover book animation example

答案 1 :(得分:0)

你应该可以使用android.graphics.Movie类来显示(和运行)动画gif。

您可以在此处找到关于课程的一些帮助:http://developer.android.com/reference/android/graphics/Movie.html

但可能更多的帮助是一个有效的例子:http://androidosbeginning.blogspot.com.au/2010/09/gif-animation-in-android.html