2013-09-25 97 views
2

是的,我可以用2張圖片創建ToggleButton(開,關)但是我想用3-5張圖片創建一個切換按鈕。如何創建動畫ToggleButton?

例如,當是它了,我點擊:

  1. 關畫面
  2. 畫中游
  3. 在畫面

而當它是不是在和我點擊:

  1. 在圖片
  2. 畫中游
  3. OFF圖片

所以它像幀動畫,我可以用ImageView的時間使用。

+0

你到目前爲止嘗試過什麼嗎?例如,玩STYLES? – bofredo

回答

1

編輯:

您可以使用Frame Animation: 在res/drawable/myanim.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pic_one" android:duration="50"/> 
    <item android:drawable="@drawable/pic_two" android:duration="50" /> 
    <item android:drawable="@drawable/pic_three" android:duration="50" /> 
</animation-list> 

然後可以使用這個動畫作爲一個普通的繪製:

<ImageView android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/myanim"/> 

開始你做動畫

AnimationDrawable backgroundDrawable = (AnimationDrawable) image.getDrawable(); 
backgroundDrawable.start(); 

您還可以使用Value Animator。我沒有測試過這個,但是你應該可以把這樣的東西放到你的按鈕的onClick處理器中:

int[] backgrounds = ...;//ids of the backgrounds for the button 
ValueAnimator anim = ValueAnimator.ofInt(0, backgrounds.length); 
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     int i = (Integer) animation.getAnimatedValue(); 
     int backgroundId = backgrounds[i]; 
     yourButton.setBackgroundResource(backgroundId); 
    } 
}); 
anim.setDuration(500); //0.5 seconds 
anim.start(); 
+1

是的,但我認爲,我可以用ToggleButton風格來做到這一點。 –

+0

例如,我不使用圖片作爲「開」或「關」背景,我可以使用其他圖片的動畫。 –

+0

@АлексейМаксимов好的,我編輯了我的答案,添加了框架動畫的示例 –