我已经创建了一个动画切换按钮,就像有人在此问题中一样。它可以正常工作,但是有一个小问题,当第一次创建布局时,我的切换按钮背景是animationdrawable的第一帧(应该是它的最后一帧)。第一次点击它后,它便可以正常工作。
编辑:toggle_pause_anim.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/pause1"
android:duration="50"/>
...
<item
android:drawable="@drawable/pause20"
android:duration="50"/>
</animation-list>
toggle_pause_anim.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/play1"
android:duration="50"/>
...
<item
android:drawable="@drawable/pause20"
android:duration="50"/>
</animation-list>
toggle_play_pause_anim.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_play_anim" android:state_checked="false"/>
<item android:drawable="@drawable/toggle_pause_anim" android:state_checked="true"/>
</selector>
toggle_play_pause_bg:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+android:id/background"
android:drawable="@android:color/transparent"/>
<item
android:id="@+android:id/toggle"
android:drawable="@drawable/toggle_play_pause_anim"/>
</layer-list>
ToggleButton
在我的layout.xml中:
<ToggleButton
android:id="@+id/play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_play_pause_bg"
android:textOff=""
android:textOn="" />
不,我在创建时确实将checked设置为false。
这是我所得到的视频。问题是第一次加载布局时(图标应处于播放状态,而不是暂停状态)。如您所见,第一次更改后,它便可以正常工作。
好吧,由于没有人回答,所以我进行了搜索并最终找到了解决方案。
所以这是窍门:
我的layout.xml中的ToggleButton:
<ToggleButton
android:id="@+id/play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_play_pause_anim"
android:textOff=""
android:textOn="" />
事实证明,图层可绘制不是必需的,并且在Java代码中,在onCreate
方法中添加了以下几行:
toggleBtn = (ToggleButton) findViewById(R.id.play_pause);
StateListDrawable stateListDrawable = (StateListDrawable) toggleBtn.getBackground();
AnimationDrawable animationDrawable = (AnimationDrawable) stateListDrawable.getCurrent();
animationDrawable.selectDrawable(animationDrawable.getNumberOfFrames() - 1);
现在它加载并运行正常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句