2012-06-19 4 views
2

onClick을 호출 할 때 버튼에 애니메이션을 만드는 방법에 대한 답을 찾지 못했습니다. 나는 custom_btn.xml과 같이있어 :Android에서 OnClick의 버튼 애니메이션을 표시 하시겠습니까?

<?xml version="1.0" encoding="utf-8"?> 
<selector 
xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_pressed="true" android:drawable="@drawable/btn_pressed"></item> 
     <item android:state_focused="true" android:drawable="@drawable/btn_on"></item> 
     <item android:drawable="@drawable/btn"></item> 
</selector> 

및 애니메이션과 같이 btn_pressed.xml에 있습니다

내 문제는 내가 올바른 코드를 찾을 수 없습니다 것입니다
<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:drawable="@drawable/btn_on_1" android:duration="30" /> 
     <item android:drawable="@drawable/btn_on_2" android:duration="30" /> 
     <item android:drawable="@drawable/btn_on_3" android:duration="30" /> 
     <item android:drawable="@drawable/btn_on_4" android:duration="30" /> 
     <item android:drawable="@drawable/btn_on_5" android:duration="30" /> 

</animation-list> 

여기에 onClickListener에 들어가기 :

 button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      //-- what is the proper animation call that would go here 
     to make btn_pressed.xml cycle only once when pressed? 

     } 
    }); 

감사합니다! 안드로이드 문서에서

답변

2

: http://developer.android.com/guide/topics/resources/animation-resource.html

예 :

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 
</animation-list> 

이 응용 프로그램 코드는보기의 배경으로 애니메이션을 설정합니다 : 고해상도/ANIM/rocket.xml에 저장 XML 파일 , 애니메이션 재생 :

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 
rocketImage.setBackgroundResource(R.drawable.rocket_thrust); 

rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
rocketAnimation.start(); 
+0

감사합니다. 당신이 준 링크가 도움이되었습니다. 내가해야 할 일은 모두 android : oneshot = "true"를 btn_pressed.xml에 추가하는 것입니다. – kirktoon1882

관련 문제