Android ImageView flipping animation

房东的猫 提交于 2019-12-11 06:23:34

问题


I have to show one animation on ImageView. It will like card flipping. The ImageView showing X image will flip and will show Y image. How can I do the same? Any idea?


回答1:


You can use this class for flip animation

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);
}
}

And add animation using below

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

You have to use 2 ImageView for this.



来源:https://stackoverflow.com/questions/42296095/android-imageview-flipping-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!