2012-10-28 2 views
1

나는 다음과 같은 코드 샘플 프로그램을 애니메이션을 시도하고있다 :애니메이션 드로어 블 AnimationDrawable

AnimationDrawable animation; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.loading); 

    ImageView animatedImage = (ImageView) findViewById(R.id.animation); 
    animatedImage.setBackgroundResource(R.drawable.animate_bag); 
    animation = (AnimationDrawable) animatedImage.getBackground(); 
} 


public void startAnimate(View v) 
{ 
    if (animation != null) 
     animation.start();   
} //eof OnClick 

XML 파일은 다음과 같습니다 내가 가진 문제가 있다는 것입니다

<Button android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Animate" 
     android:onClick="startAnimate" /> 
    <ImageView 
     android:id="@+id/animation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/animate_bag" /> 


</LinearLayout> 

animatedImage.getBackground()는 null을 반환합니다.

감사합니다, 사이먼이 당신을 도울 것입니다

+0

안녕하세요. 나이가 들었지만 어떤 해결책을 찾았습니까? 감사 – Youssef

답변

0

희망 :-) 당신의 힌트를 주셔서 감사합니다, 내의 getBackground()가 null을 반환하는 경우

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

    final ImageView animatedImage = (ImageView) findViewById(R.id.animation); 
    animatedImage.setBackgroundResource(R.drawable.animate_bag); 


    animatedImage.post(new Runnable() { 

     @Override 
     public void run() { 
      AnimationDrawable frameAnimation = (AnimationDrawable) animatedImage.getDrawable(); 
      if (frameAnimation != null) { 
       frameAnimation.start(); 
      } 
     } 
    }); 
} 
3

), getDrawable() 메서드를 사용합니다.

//animation = (AnimationDrawable) animatedImage.getBackground(); 
animation = (AnimationDrawable) animatedImage.getDrawable();