1

Android 프로젝트에서 간단한 애니메이션을 만들고 싶습니다.Android에서 애니메이션 순서를 회전하십시오.

<ImageView 
     android:id="@+id/pointer_png" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitCenter" 
     android:adjustViewBounds="true" 
     android:layout_gravity="center" 
     android:src="@drawable/pointer_400" /> 

을 그리고 여기 활동 수업 시간에 내 온 클릭 방법이다 : 나는 나의 활동에 이미지를 가지고

public void onStartButtonClick(View view){ 
    AnimationSet animationSet = new AnimationSet(true); 
    animationSet.setInterpolator(new LinearInterpolator()); 
    animationSet.setFillAfter(true); 

    RotateAnimation anim = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    anim.setDuration(4000); 
    animationSet.addAnimation(anim); 

    RotateAnimation anim2 = new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    anim2.setDuration(4000); 
    animationSet.addAnimation(anim2); 

    RotateAnimation anim3 = new RotateAnimation(0.0f, -135.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    anim3.setDuration(4000); 
    animationSet.addAnimation(anim3); 

    RotateAnimation anim4 = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    anim4.setDuration(4000); 
    animationSet.addAnimation(anim4); 

    final ImageView pointer = (ImageView) findViewById(R.id.pointer_png); 
    pointer.startAnimation(animationSet); 
} 

불행하게도, 그 효과는 예기치 않은가. 다음 순서로 이미지를 회전하고 싶습니다.

  1. 4 초 이내에 180도 회전합니다.
  2. 다음 4 초 이내에 -135도 회전.
  3. 다음 4 초 내에 90도 회전하십시오.
  4. 지난 4 초 이내에 -45도 회전.

그러나이 코드 애니메이션은 분명히 16 초보다 짧으며 한 부분 만 구성되어 있습니다 (0 점에서 90도까지 끝났습니다). 아마도 AnimationSet은 모든 애니메이션을 검사하고 시퀀스의 마지막 위치를 계산합니다. AnimationSet (false)을 설정하고 각 RotateAnimation에 별도의 LinearInterpolator를 추가하려고 시도했지만 작동하지 않습니다.

애니메이션을 길게 만들고 모든 회전을 분리하려면 어떻게해야합니까? (각 단계마다 4 단계, 4 초)?

+0

'setStartOffset()'를 사용하여 다른 애니메이션에 시작 오프셋을 추가하십시오. – TR4Android

+0

나는 0, 4000, 8000, 12000의 오프셋을 시도했지만, 최종 결과는 예상치 못했고 예상치 못한 결과였다. 각각의 애니메이션에 AnimationListener를 설정하고 onAnimationEnd 메소드를 오버라이드해야하고 다음 애니메이션을 시작할 필요가 있습니다. 이 시퀀스는 괜찮지 만 더 복잡한 애니메이션은 무엇입니까? – maniek099

답변

0

내 경험에 비추어 볼 때 AnimationSet은 항상 예상대로 작동하지 않으며 a **의 고통이 될 수 있습니다. ViewPropertyAnimator을 사용해 보겠습니다.

다음은 사용 방법의 예입니다.

pointer.animate() 
    .rotation(...) // <- enter rotation values here 
    .setStartDelay(4000) 
    .setInterpolator(new LinearInterpolator()) 
    .setDuration(4000); 

을하거나 AnimationListener을 설정하고 하나가 이전에 완료되면 onAnimationEnd()의 다음 애니메이션을 시작 : 당신도이 같은 startDelay을 설정할 수 있습니다.

나는 내 자신의 방법을 써서이 작업을 수행해야한다면 자발적으로, 이런 일이 (테스트하지) :이 같은

private void rotate(View v, float rotation, int startDelay) { 
    v.animate() 
    .rotation(rotation) // or rotationBy(rotation) whichever suits you better 
    .setStartDelay(startDelay) 
    .setInterpolator(new LinearInterpolator()) 
    .setDuration(4000); 
} 

후 전화를 네 번 :

rotate(pointer, -45, 0); 
rotate(pointer, 90, 4000); 
rotate(pointer, -135, 8000); 
rotate(pointer, 180, 12000);