2013-06-25 3 views
6

로딩 애니메이션으로 사용할 이미지 뷰가 있습니다. 일단 데이터가로드되면 애니메이션을 멈추려고합니다. 끝까지 순환 한 다음 멈추는 대신 애니메이션이 중간 지점으로 이동 한 다음 멈추고 이미지가 원래 상태로 스냅되어 다시보기 흉하게 보입니다. . 여기 주기가 끝날 때 애니메이션을 중단하는 방법은 무엇입니까?

내가 무엇을 시도했다입니다 :

옵션 1 :

ImageView iv = (ImageView) findViewById(R.id.refreshImage); 
if (iv != null) { 
    iv.clearAnimation(); 
} 

옵션 2 :

ImageView iv = (ImageView) findViewById(R.id.refreshImage); 
if (iv != null && iv.getAnimation() != null) { 
    iv.getAnimation().cancel(); 
} 

옵션 3 :

ImageView iv = (ImageView) findViewById(R.id.refreshImage); 
if (iv != null && iv.getAnimation() != null) { 
    iv.getAnimation().setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      animation.cancel(); 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

     } 
    }); 
} 

최종 결과는 세 가지 경우 모두 동일합니다. 어떻게 이미지를 회전시키고 이미지가 시작된 곳으로 돌아 가게합니까?

편집 :

일부 추가 정보 : 내 회전 애니메이션 : 나는 애니메이션을 시작하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="1000" 
android:fromDegrees="0" 
android:interpolator="@android:anim/linear_interpolator" 
android:pivotX="50%" 
android:pivotY="50%" 
android:toDegrees="360" /> 

:

ImageView iv = (ImageView) findViewById(R.id.refreshImage); 
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate); 
rotation.setRepeatCount(Animation.INFINITE); 
iv.startAnimation(rotation); 

답변

14

당신이 그것을 시작하기 전에 애니메이션의 반복 횟수 후 무한로 설정 액션이 끝나면 애니메이션의 반복 횟수를 0으로 설정합니다. 애니메이션은 현재 루프를 종료하고 피하려는 점프없이 중지합니다. 당신은 예를 들어 1000로 설정하면, 0에 다음과 (그들은 동일한 작업을 수행하기 때문에 -1)

//How you start 
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate); 
      rotation.setRepeatCount(Animation.INFINITE); 
iv.startAnimation(rotation); 

//You do your stuff while it spins 
... 

//You tell it not to repeat again 
rotation.setRepeatCount(0); 

그것은 당신이 첫번째 Animation.INFINITE로 설정하는 것이 중요합니다, 그것은 습관에 따라 몇 가지 이유로 중단 내 테스트.

+0

내 애니메이션 중 일부는 여전히 불안해 보이지만, 대부분의 경우 감사합니다! – Catherine

+2

@ 캐서린 동시에 여러 항목이 발생하는 애니메이션 세트의 경우 불안해 보일 수 있습니다. 테스트하지는 않았지만 간단한 애니메이션의 경우이 방법을 사용합니다. –

+0

멋진 트릭을 가져 주셔서 감사합니다. 안드로이드가 api stopAfterCycleCompletion() 또는 이와 유사한 것을 제공하는 것이 더 좋았을 것입니다. – rpattabi

-2

iv.clearAnimation(); 애니메이션을 멈추기 위해서

관련 문제