2012-12-08 2 views
2

내 앱에 문제가 있습니다. 애니메이션을 반복하지만 onAnimationRepeat 함수 (AnimationListener 클래스)가 호출되지 않을 때 ImageView를 수정하고 싶습니다. Android (API 7) - 애니메이션 : onAnimationRepeat

을 재현하기 위해 샘플입니다

  • 새 프로젝트 "테스트"를 작성 (안드로이드 2.1, API 7)
  • res 폴더 안에 폴더 anim 만들기 (I 패키지 이름 com.test.test을 사용) 이러한 라인

fade_in.xml

을 다음은 함께 fade_in.xml를 만들 ctivity 코드 :

package com.test.test; 

import android.app.Activity; 
import android.graphics.Color; 
import android.graphics.PorterDuff.Mode; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.view.animation.Animation.AnimationListener; 
import android.widget.Button; 
import android.widget.ImageView; 

public class TestActivity extends Activity { 

    Button button = null; 
    ImageView image = null; 

    Animation animation = null; 


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

     button = (Button)findViewById(R.id.button1); 
     image = (ImageView)findViewById(R.id.imageView1); 

     animation = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     animation.setAnimationListener(new AnimationListener() {    

      int repeatNumber = 0; 

      @Override 
      public void onAnimationStart(Animation animation) {Log.i("start", "start");} 

      @Override 
      public void onAnimationEnd(Animation animation) {Log.i("end", "end");} 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       repeatNumber++; 
       Log.i("repeat", String.valueOf(repeatNumber)); 
       switch(repeatNumber) 
       { 
       case 1:image.setColorFilter(Color.RED, Mode.MULTIPLY);break; 
       case 2:image.setColorFilter(Color.BLUE, Mode.MULTIPLY);break; 
       case 3:image.setColorFilter(Color.YELLOW, Mode.MULTIPLY);break; 
       default:break; 
       } 
      } 
     }); 

     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) {image.startAnimation(animation);} 
     }); 

    } 
} 

그리고 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 

ImageView 색상이 변경되지 않습니다 ... 당신이 나를 도와 드릴까요?

+1

재설정 할 수없는 카운터 변수 값을 확인하십시오. – Pratik

+0

Log.i ("repeat", String.valueOf (repeatNumber)); => logcat에는 나타나지 않습니다. repeatNumber는 샘플 용으로 만 만들어졌습니다. – user1887611

답변

3

메서드 AnimationUtils.loadAnimation()은 애니메이션 배열을 반환합니다.

이 배열의 항목에 이벤트를 설정해야합니다.

당신은 당신이 쓸 수 있도록 명령

animation = AnimationUtils.loadAnimation(this, R.anim.fade_in); 

후 AnimationSet

AnimationSet animation = null; 

을 선언 할 수 있습니다 :이 작동

for (Animation a : animation.getAnimations()) 
     a.setAnimationListener(new AnimationListener() { 

      @Override 
      public void onAnimationStart(Animation animation) {} 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       Log.i("repeat", "repeat"); 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) {} 
     }); 

.

+0

animation = (AnimationSet) AnimationUtils.loadAnimation (this, R.anim.fade_in); 나는 그것이 완벽하게 작동한다는 것을 확인한다. (나는 당신의 해결책을 에드 루로 슈에게 제출할 것이다!). 대단히 감사합니다! – user1887611

0

애니메이션을 반복하려면 애니메이션의 반복 모드와 반복 발생 횟수를 설정해야합니다.

anim.setRepeatMode(Animation.INFINITE); 
anim.setRepeatCount(5); 
+1

XML 파일 대신 코드를 사용하려고 시도했지만 아무 것도 변경하지 않습니다. android : repeatCount = "3"은 xml 파일에서 작동하지만 리스너의 onAnimationRepeat 함수는 호출되지 않습니다. – user1887611

0

나는 동일한 문제에 직면 해 있었고 많은 리소스와 애니메이션 문제 답변을 읽었습니다. 모두가 AnimationSet 및 반복 횟수 문제에 대해 불평하고있었습니다. 그래서 나는 XML에서 태그를 제거하고 그것은 효과가 있었다. OnRepeat와 onEnd 둘 다 지금 전화 받고 있습니다.

관련 문제