在android中的卡翻转动画

POU*_*IMI 10 java xml android

我已经厌倦了在android中制作一张翻转卡片.请做一个图像视图,当我点击它时,它翻转就像  这个

小智 21

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final ObjectAnimator oa1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0f);
        final ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "scaleX", 0f, 1f);
        oa1.setInterpolator(new DecelerateInterpolator());
        oa2.setInterpolator(new AccelerateDecelerateInterpolator());
        oa1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                imageView.setImageResource(R.drawable.frontSide);
                oa2.start();
            }
        });
        oa1.start();
    }
});
Run Code Online (Sandbox Code Playgroud)

这个动画没有任何深度,但也许你会喜欢它.

您还可以使用设置动画持续时间

oa1.setDuration(1000);
oa2.setDuration(1000);
Run Code Online (Sandbox Code Playgroud)


bwt*_*bwt 15

属性动画(不要与旧的View动画混淆)非常强大:

final View v = <the_image_view>;

// first quarter turn
v.animate().withLayer()
        .rotationY(90)
        .setDuration(300)
        .withEndAction(
                new Runnable() {
                    @Override public void run() {

                        <change the image...>

                        // second quarter turn
                        v.setRotationY(-90);
                        v.animate().withLayer()
                                .rotationY(0)
                                .setDuration(300)
                                .start();
                    }
                }
        ).start();
Run Code Online (Sandbox Code Playgroud)

您可以使用View.setCameraDistance()调整透视效果

  • 有效。我的“卡片”图像太大,所以我使用 setCameraDistance 建议来避免卡片因为离屏幕太近而被“剪切”:`float scale = getApplicationContext().getResources().getDisplayMetrics().density; 浮动距离 = ivPreview.getCameraDistance() * (scale + (scale / 3)); ivPreview.setCameraDistance(距离);` (3认同)