2013-09-25 95 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

@АлексейМаксимов好的,我编辑了我的答案,添加了框架动画的示例 –