首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建动画ToggleButton?

如何创建动画ToggleButton?
EN

Stack Overflow用户
提问于 2013-09-25 15:05:06
回答 1查看 4K关注 0票数 3

是的,我可以用2张图片创建ToggleButton (开,关),但是我想要创建一个3-5图片的ToggleButton。

例如,何时关闭,我单击:

  1. 离像
  2. 中间图象
  3. 图片上

它什么时候开着,我点击:

  1. 图片上
  2. 中间图象
  3. 离像

因此,它就像框架动画,我可以使用与持续时间与ImageView。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-25 15:25:58

编辑:

您可以使用帧动画:In res/drawable/myanim.xml

代码语言:javascript
复制
<?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>

然后,您可以将此动画用作普通绘图:

代码语言:javascript
复制
<ImageView android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/myanim"/>

要启动动画,请执行以下操作

代码语言:javascript
复制
AnimationDrawable backgroundDrawable = (AnimationDrawable) image.getDrawable();
backgroundDrawable.start();

您还可以使用价值动画。我还没有对此进行测试,但是您应该能够将类似的内容放入您按钮的onClick处理程序中:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19008555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档