如何创建动画切换按钮? [英] How can I create animated ToggleButton?
本文介绍了如何创建动画切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是的,我可以用2张照片(开,关)创建切换按钮,但我想创建一个切换按钮3-5的照片。
Yes, I can create ToggleButton with 2 pictures(On, Off) But I want to create a ToggleButton with 3-5 pictures.
例如,什么时候了,我点击:
For example, when is it OFF and I click:
- 关闭图片
- 中东图片
- 在图片
而当它是不是在和我点击:
And when is it ON and I click:
- 在图片
- 中东图片
- 关闭图片
因此,它是像帧动画,我可以用ImageView的时间使用。
So it is like frame animation, that I can use with duration with ImageView.
推荐答案
编辑:
您可以使用帧动画:
在 RES /绘制/ 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>
然后就可以使用这个动画作为一个普通的绘制:
You can then use this animation as a plain drawable:
<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();
您也可以使用值动画。我没有测试过这一点,但你应该能够把这样的事情到您按钮的onClick处理程序:
You can also use a Value Animator. I haven't tested this, but you should be able to put something like this into onClick handler of you button:
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();
这篇关于如何创建动画切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文