2012-09-13 2 views
0

RotateAnimationImageView으로 지정했습니다. 그래서, 몇번 반복 한 후에 애니메이션을 멈추고 싶습니다. 시나리오는 다음과 같습니다 :동적으로 RotateAnimations 매개 변수 변경

첫 번째 애니메이션은 애니메이션이 끝난 후 -25도에서 25도까지 시작됩니다.이 애니메이션은 -24에서 24 사이에서 반대로 변경되어야하며 ... 0에서 0에 도달하면 취소해야합니다. .

int intervalSize = -25; 

    RotateAnimation r = new RotateAnimation(intervalSize, intervalSize, pivotX, pivotY); 
    r.setDuration(3000); 
    r.setStartOffset(0); 
    r.setRepeatMode(RotateAnimation.REVERSE); 
    r.setRepeatCount(RotateAnimation.INFINITE); 
    startAnimation(r); 
    r.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      if (intervalSize == 0) 
       animation.cancel(); 
      intervalSize--; 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

     } 
    }); 

아무쪼록이 문제에 어떻게 대응할 수 있습니까? 사전 :

답변

1

에서

덕분에 나는 당신의 문제는 각 애니메이션 반복에 글로벌 VAR 간격의 크기를 변경하는 동안, 반복되는 애니메이션이 VAR보고, 오히려 원시적되지 않는다는 생각 당신이 말했을 때 전달 된 정수 : new RotateAnimation (intervalSize, intervalSize, pivotX, pivotY);

즉, 애니메이션은 생성 된 이후로 항상 -25를 가지며 이후 intervalSize var에 대한 변경 사항을 알지 못합니다.

은 당신이 좋아하는 일을 할 수있을 거라고 당신이 이상적으로 원하는 달성하기

@Override 
     public void onAnimationRepeat(Animation animation) { 
      if (intervalSize == 0){ 
       animation.cancel(); 
      } else { 
       ((RotationAnimation)animation).setFromDegress(intervalSize); 
       ((RotationAnimation)animation).setToDegress(intervalSize); 
      } 
     } 

그러나 RotationAnimation에 해당 속성에 대한 setter 메소드가있는 것처럼 슬프게도, 그것은 보이는 나던. 따라서 onAnimationEndEvent를 사용하여 새 intervalSizeValue로 새 Animation을 만들 수 있습니다. 다음과 같이 :

onAnimationEnd 이벤트를 사용하고 반복 애니메이션이 아닌 일회성 애니메이션을 사용하십시오. 끝나면 onAnimationEnd 이벤트는 새 intervalSize로 새 RotationAnimation을 생성해야합니다.

package com.example.rotationtest; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.RotateAnimation; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    private TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // the textView we will rotate 
     tv = (TextView) this.findViewById(R.id.textView1); 

     /*** 
     * we want to have the correct measured size of the view we are going to animate 
     * as we want to do a rotation around it's centerpoint. 
     * But, we cant get the measured size of a view until Layout has happened... 
     * So use a LayoutListener to know when layout is done 
     * But, beware that this is often called back on more than once, 
     * so remove the listener after it is called first time 
     */ 
     tv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       initAnimation(); 
       tv.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       // above is deprecated. in API16+ use tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);  
      } 
     }); 
    } 

    private int mRotationAbsDegrees = 25; 
    private int mCurrentFromDegrees; 
    private int mCurrentToDegrees; 

    private void initAnimation(){ 
    mCurrentFromDegrees = -1 * mRotationAbsDegrees; 
    mCurrentToDegrees = mRotationAbsDegrees; 
    makeNewAnimation(); 
    } 

    private void makeNewAnimation(){ 
    RotateAnimation r = new RotateAnimation(mCurrentFromDegrees, mCurrentToDegrees, tv.getMeasuredWidth()/2, tv.getMeasuredHeight()/2); 
     r.setDuration(3000); // TODO: might want to reduce the time as we get closer to zero mRotationAbsDegrees 
     r.setStartOffset(0); 
     //r.setRepeatMode(RotateAnimation.REVERSE); 
     //r.setRepeatCount(RotateAnimation.INFINITE); 
     tv.startAnimation(r); 
     r.setAnimationListener(new AnimationListener() { 

      @Override public void onAnimationStart(Animation animation) {} 

      @Override public void onAnimationRepeat(Animation animation) {} 

      @Override 
      public void onAnimationEnd(Animation animation) { 
      // if we have run down the mRotationAbsDegrees to zero, stop animating 
      if (mRotationAbsDegrees <= 0){ 
       return; 
      } 
      if (mCurrentFromDegrees < 0){ 
       // reverse the from to 
       mCurrentFromDegrees = -1*mCurrentFromDegrees; 
       mCurrentToDegrees = -1*mCurrentToDegrees; 
      } else { 
       // reduce the mRotationAbsDegrees 
       mRotationAbsDegrees--; 
       mCurrentFromDegrees = -1 * mRotationAbsDegrees; 
       mCurrentToDegrees = mRotationAbsDegrees; 
      } 
      makeNewAnimation(); 
      } 
     }); 
    } 
} 
+0

당신이 당신의 제안에 대한 몇 가지 코드를 게시 할 수 : 여기

는 방법에 전형적인 안드로이드하여 HelloWorld 응용 프로그램에서 텍스트 뷰 지정을 애니메이션 작업 예입니다? –

+1

이전 제안의 작동 예제로 업데이트되었습니다. – mmeyer

관련 문제