애니메이션을 중지하는 방법 (cancel ()이 작동하지 않음)


245

실행중인 번역 애니메이션을 중지해야합니다. .cancel()의 방법 Animation에는 영향을주지 않는다; 어쨌든 애니메이션은 끝까지 간다.

실행중인 애니메이션을 어떻게 취소합니까?

답변:


509

전화 clearAnimation()한 사람 View에게 전화하십시오 startAnimation().


그러나 실제로는 좀 더 복잡한 것이 필요합니다. 애니메이션이 중지되면 현재 위치에 있어야합니다. 즉 슬라이딩 이미지가 있고 터치 이벤트가 발생하면이 곳에서 정지해야합니다. 그것은 ()에 따라 setFillAfter / 종료 위치를 시작하는 상태를 리셋하기 때문에 clearAnimation는 ()와, 그렇지 않은
— 믹스

1
@ user349871 : setFillAfter()아마도 당신이 생각하는 것을하지 않을 것입니다. 터치 이벤트가 발생하면 애니메이션을 지우고 원하는 영구적 변경에 영향을 미치도록 레이아웃을 조정하십시오.
— CommonsWare

좋아, clearAnimation ()을 호출하여 애니메이션을 중지하려고합니다. 다음 단계는 애니메이션을 중지하기 전에 애니메이션이 있었던 위치를 찾는 것입니다. 그게 무슨 API입니까?
— 믹스

@ 혼합 : 그것에 대한 API가 없습니다.
— CommonsWare

7
다시 안녕! 애니메이션이 취소되었을 때 (보간 시간 측면에서) 포인트를 얻는 방법을 찾았습니다. 애니메이션을 서브 클래스로 만들고 안드로이드 코드 애니메이션 (예 : TranslateAnimation)의 코드를 넣어야합니다. 클래스에서는 applyTransformation () 함수에서 위치를 저장하고 추적 할 수 있습니다.
— 믹스

40

안드로이드 4.4.4에서, 내가 전화 한보기에 알파 페이드 애니메이션을 막을 수있는 유일한 방법은 같다 View.animate().cancel()(전화, 즉 .cancel()보기의에 ViewPropertyAnimator).

ICS 전후 호환성을 위해 사용하는 코드는 다음과 같습니다.

public void stopAnimation(View v) {
    v.clearAnimation();
    if (canCancelAnimation()) {
        v.animate().cancel();
    }
}

... 방법 :

/**
 * Returns true if the API level supports canceling existing animations via the
 * ViewPropertyAnimator, and false if it does not
 * @return true if the API level supports canceling existing animations via the
 * ViewPropertyAnimator, and false if it does not
 */
public static boolean canCancelAnimation() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}

중지하고있는 애니메이션은 다음과 같습니다.

v.setAlpha(0f);
v.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation listener set on the view.
v.animate()
    .alpha(1f)
    .setDuration(animationDuration)
    .setListener(null);

.setListener(null);이것은 나를 도왔다.
— Muhammad Saqib

18

애니메이션 리스너를 사용하는 경우을 설정하십시오 v.setAnimationListener(null). 모든 옵션과 함께 다음 코드를 사용하십시오.

v.getAnimation().cancel();
v.clearAnimation();
animation.setAnimationListener(null);

2
이 v.setAnimation(null);작업은에서 수행되므로 호출 할 필요는 없습니다 v.clearAnimation();.
— Christian

@Christian는 v.setAnimation (NULL)이 필요하지 않습니다 말했듯이
— 하기 Akshay을 bhange

5

.clearAnimation ()을 사용해야합니다. UI 스레드의 메소드 :

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        v.clearAnimation();
    }
});

3

할 수있는 것은 애니메이션을 중지하기 전에 변환 매트릭스를 애니메이션에서 가져오고 매트릭스 내용을 검사하여 원하는 위치 값을 얻는 것입니다.

다음은 살펴 봐야 할 API입니다.

public boolean getTransformation (long currentTime, Transformation outTransformation)

public Matrix getMatrix ()

public void getValues (float[] values)

예를 들어 (일부 의사 코드. 나는 이것을 테스트하지 않았습니다) :

Transformation outTransformation = new Transformation();
myAnimation.getTransformation(currentTime, outTransformation);
Matrix transformationMatrix = outTransformation.getMatrix();
float[] matrixValues = new float[9];
transformationMatrix.getValues(matrixValues);
float transX = matrixValues[Matrix.MTRANS_X];
float transY = matrixValues[Matrix.MTRANS_Y];

currentTime은 어디서 얻습니까?
— mako

오, 문서는 "벽 시계"에 해당한다고 말하고, 다른 곳에서는 벽 시계를 System.currentTimeMillis ()로 정의합니다. 이는 달력 시간에 해당하며 사용자 나 네트워크가 시간을 변경할 때마다 점프합니다. 사용하는 홀수 측정 값입니다.
— mako

0

setAnimation (null) 메소드를 사용하여 애니메이션을 중지하고 View.java 에서 public 메소드로 노출되며 모든 위젯 의 기본 클래스이며 대화 형 UI 구성 요소 (버튼, 텍스트 필드 등)를 만드는 데 사용됩니다. /** * Sets the next animation to play for this view. * If you want the animation to play immediately, use * {@link #startAnimation(android.view.animation.Animation)} instead. * This method provides allows fine-grained * control over the start time and invalidation, but you * must make sure that 1) the animation has a start time set, and * 2) the view's parent (which controls animations on its children) * will be invalidated when the animation is supposed to * start. * * @param animation The next animation, or null. */ public void setAnimation(Animation animation)


0

애니메이션을 멈추기 위해 아무것도하지 않는 objectAnimator를 설정할 수 있습니다.

먼저 수동으로 뒤집을 때 왼쪽에서 오른쪽으로 애니메이션이 있습니다.

flipper.setInAnimation(leftIn);
flipper.setOutAnimation(rightOut);

자동 뒤집기로 전환하면 애니메이션이 없습니다.

flipper.setInAnimation(doNothing);
flipper.setOutAnimation(doNothing);

doNothing = ObjectAnimator.ofFloat(flipper, "x", 0f, 0f).setDuration(flipperSwipingDuration);
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.