2013-07-01 69 views
0

當我點擊一個按鈕,我想改變,在500毫秒,按鈕backgourndimage並做一個簡短的動畫。我已經完成了一段代碼,但是我看不到relsult,動畫沒問題,但點擊的圖片按鈕沒有出現。如何更改並查看按鈕背景圖像幾毫秒?

的defalut按鈕圖像 「boutton_a.png」 點擊的按鈕圖像 「button_a_ok.png」

這裏的onclick按鈕代碼:

  if (Button04.getText().equals(Reponse)){ 
        Button01.setBackgroundResource(R.drawable.boutton_a_ok); 
        AnimationSet set=new AnimationSet(true); 
        Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10); 
        animation.setDuration(100); 
        set.addAnimation(animation); 
        Button01.startAnimation(set); 
      }else{ 
        Button01.setBackgroundResource(R.drawable.boutton_a_nok); 
        AnimationSet set=new AnimationSet(true); 
        Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10); 
        animation.setDuration(100); 
        set.addAnimation(animation); 
        Button04.startAnimation(set); 
      } 

我該怎麼辦?

編輯:我已經發布的所有代碼,因爲有2 backgroundressources型動物

+0

添加計時器和運行方法設置背景圖片 –

回答

0

您可以通過創建從XML不同的按鈕狀態的繪製更改按鈕,點擊該按鈕的背景。我這裏是

<selector 
    <item android:drawable="@drawable/button_pressed_yellow" 
      android:state_pressed="true" /> 
    <item android:drawable="@drawable/button_focused_orange" 
      android:state_focused="true" /> 
    <item android:drawable="@drawable/button_normal_green" /> 
</selector> 

保存此文件中繪製文件夾new_button.xml

<Button 
     android:id="@+id/imageButtonSelector" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/new_button" /> 
+0

我已經發布的所有碼。實際上,我想根據字符串'reponse'更改backgroundressource並執行動畫。 – morbak

0

把這個文件在你的資源foldr相似的 「rotate.xml」 RES /動畫/ rotate.xml

和使用此類動畫

public class DownloadButton extends Button 
{ 
    private String state = "Normal"; 
    public DownloadButton(Context context) 
     { 
      super(context); 
     } 
    public DownloadButton(Context context, AttributeSet attrs) 
     { 
      super(context, attrs); 
     } 
    public DownloadButton(Context context, AttributeSet attrs, int defStyle) 
     { 
      super(context, attrs, defStyle); 
     } 
    public void setState(String state) 
     { 
      this.state = state; 
      if (this.state.equals("Downloading")) 
       { 
        ButtonAnimate(); 
       } 
      else if(this.state.equals("Waiting")) 
       { 
        setToWaiting(); 
       } 
      else 
       { 
        clearAnimation(); 
       } 
     } 
    private void setToWaiting() 
     { 
      setBackgroundResource(R.drawable.waiting); 
     } 
    public void ButtonAnimate() 
     { 
      this.startAnimation(AnimationUtils.loadAnimation(MediaSingleton.getInstance().getContext(), R.anim.rotate)); 
     } 
    public String getState() 
     { 
      return state; 
     } 
} 

,並使用此按鈕你的佈局像這樣

<com.yourpackage.DownloadButton 
    android:id="@+id/Downloadbtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="2dp" 
    android:layout_gravity="center_vertical" 
    android:background="@drawable/download" 
    /> 

,並使用這個動畫這樣

Button button=(Button) findViewById(R.id.yourbtnid); 
button.setBackgroundResource(R.drawable.downloading); 
button.setState("Downloading"); 

你做你的動畫好運