是的,我可以创建带有 2 张图片的 ToggleButton(开、关),但我想创建带有 3-5 张图片的 ToggleButton。
例如,什么时候关闭,我单击:
- 关闭图片
- 中图
- 图片上
它什么时候开启,我点击:
- 图片上
- 中图
- 关闭图片
所以它就像帧动画,我可以将其与 ImageView 一起使用。
EDIT:
您可以使用帧动画 http://developer.android.com/guide/topics/resources/animation-resource.html#Frame:
In 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();
您还可以使用价值动画师 http://developer.android.com/reference/android/animation/ValueAnimator.html。我还没有对此进行测试,但是您应该能够将类似的内容放入按钮的 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)