【问题标题】:Android ImageView flipping animationAndroid ImageView 翻转动画
【发布时间】:2017-07-06 20:54:52
【问题描述】:

我必须在 ImageView 上显示一个动画。它会喜欢翻牌。显示 X 图像的 ImageView翻转 并显示 Y 图像。我该怎么做?有什么想法吗?

【问题讨论】:

  • 我已经去了。它是通过使用两个片段来完成的。但是我在RecycleView的一个项目上做了,很难申请。
  • 好的..所以相应地发布您的问题..提供所有详细信息

标签: android animation imageview android-animation flip


【解决方案1】:

你可以使用这个类来做翻转动画

FlipAnimation.class

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View; 
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class FlipAnimation extends Animation {
private Camera camera;

private View fromView;
private View toView;

private float centerX;
private float centerY;

private boolean forward = true;

/**
 * Creates a 3D flip animation between two views.
 *
 * @param fromView First view in the transition.
 * @param toView   Second view in the transition.
 */
public FlipAnimation(View fromView, View toView) {
    this.fromView = fromView;
    this.toView = toView;

    setDuration(500);
    setFillAfter(false);
    setInterpolator(new AccelerateDecelerateInterpolator());
}

public void reverse() {
    forward = false;
    View switchView = toView;
    toView = fromView;
    fromView = switchView;
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    centerX = width / 2;
    centerY = height / 2;
    camera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    // Angle around the y-axis of the rotation at the given time
    // calculated both in radians and degrees.
    final double radians = Math.PI * interpolatedTime;
    float degrees = (float) (180.0 * radians / Math.PI);

    // Once we reach the midpoint in the animation, we need to hide the
    // source view and show the destination view. We also need to change
    // the angle by 180 degrees so that the destination does not come in
    // flipped around
    if (interpolatedTime >= 0.5f) {
        degrees -= 180.f;
        fromView.setVisibility(View.GONE);
        toView.setVisibility(View.VISIBLE);
    }

    if (forward)
        degrees = -degrees; //determines direction of rotation when flip begins

    final Matrix matrix = t.getMatrix();
    camera.save();
    camera.rotateY(degrees);
    camera.getMatrix(matrix);
    camera.restore();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postTranslate(centerX, centerY);
}
}

并使用下面添加动画

FlipAnimation flipAnimation = new FlipAnimation(view1,view2);
if (view1.getVisibility() == View.GONE) {
   flipAnimation.reverse();
   view2.startAnimation(flipAnimation);
} else{
   view1.startAnimation(flipAnimation);
}

您必须为此使用 2 个 ImageView。

【讨论】:

  • 我无法让反向翻转正常工作。在上面的代码中,它只在翻转时调用 reverse 函数,这并不完全执行动画。
猜你喜欢
  • 2014-10-03
  • 2011-03-22
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 2013-12-21
相关资源
最近更新 更多