2017-02-17 10 views
-2

에 하나의 애니메이션을 표시해야합니다. ImageView. 그것은 카드 뒤집기를 좋아할 것입니다. 이미지 뷰은 X 이미지를 표시하며 이며 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); 
} 
} 

이 클래스를 사용 그리고이 2 imageviews를 사용하는

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

다음은 한 사용하여 애니메이션을 추가 할 수 있습니다.