2017-02-17 167 views
-2

我必須在ImageView上顯示一個動畫。它會喜歡翻牌。顯示X圖像的ImageView翻轉並將顯示Y圖像。我怎樣才能做到這一點?任何想法?Android ImageView翻轉動畫

+3

請參閱https://developer.android.com/training/animation/cardflip.html你有沒有嘗試過? – Akshay

+0

我已經走了。它通過使用兩個片段完成。但是我已經在RecycleView的一個項目上做了,它很難應用。 –

+0

好的。所以發佈你的問題相應..提供所有的細節 – Akshay

回答

3

你可以使用這個類翻轉動畫

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(); 
    }else{ 
     view1.startAnimation(flipAnimation); 
    } 

你必須使用2個imageviews此添加動畫。