动画切换按钮 [英] animated toggle button
问题描述
我已经创建了动画切换,如果android:checked="true"
可以正常工作,即:它在1-9范围内可绘制,但是android:checked="false"
在10范围内可绘制,但应该在10-20范围内播放.但是当应用启动时,会出现上述问题.
I have created animated toggle and it works perfect if android:checked="true"
ie: it plays drawable from 1-9 but if android:checked="false"
its stuck at drawable 10 but it should play from 10-20 .It works perfect from then on but when the app starts it has the above problem .
为什么它停留在可绘制的10上,而实际上它应该是20,因为这是最终图像.(Off)当前停留在可绘制的10上,看起来仍然处于打开状态.
Why is it stuck at drawable 10 when in reality it should be at 20 as that is the final image.(Off) currently as its stuck at 10 it looks like its still ON .
//////////////////
///////////////////
btn_toggle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/animatedbutton_on" />
<item android:state_checked="false" android:drawable="@drawable/animatedbutton_off" />
</selector>
//////////////////
//////////////////
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/ToggleButton01"
android:layout_width="174dp"
android:layout_height="235dp"
android:background="@drawable/btn_toggle_bg"
android:checked="false"
/>
/////////////////////////
//////////////////////////
btn_toggle_bg.xml
btn_toggle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<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/btn_toggle" />
</layer-list>
</LinearLayout>
//////////
animated_button_on.xml
animated_button_on.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="@drawable/1" android:duration="200" />
<item android:drawable="@drawable/2" android:duration="200" />
<item android:drawable="@drawable/3" android:duration="200" />
<item android:drawable="@drawable/4" android:duration="200" />
<item android:drawable="@drawable/5" android:duration="200" />
<item android:drawable="@drawable/6" android:duration="200" />
<item android:drawable="@drawable/7" android:duration="200" />
<item android:drawable="@drawable/8" android:duration="200" />
<item android:drawable="@drawable/9" android:duration="200" />
</animation-list>
////////////////
////////////////
animatedbutton_OFF.xml
animatedbutton_OFF.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="@drawable/a14" android:duration="200" />
<item android:drawable="@drawable/a15" android:duration="200" />
<item android:drawable="@drawable/a16" android:duration="200" />
<item android:drawable="@drawable/a17" android:duration="200" />
<item android:drawable="@drawable/a18" android:duration="200" />
<item android:drawable="@drawable/a19" android:duration="200" />
<item android:draw
able="@drawable/a20" android:duration="200" />
</animation-list>
///////////////
///////////////
CustomToggleButtonDemo.java
CustomToggleButtonDemo.java
public class CustomToggleButtonDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton tg=(ToggleButton)findViewById(R.id.ToggleButton01);
tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
// The toggle is enabled
Toast.makeText(getBaseContext(), "ON", 5).show();
} else {
// The toggle is disabled
Toast.makeText(getBaseContext(), "off", 5).show();
}
}
});
推荐答案
您必须将文件从"animatedbutton_OFF.xml"重命名为"animatedbutton_off.xml". 从"animated_button_on.xml"到"animatedbutton_on.xml"
You must rename your file from "animatedbutton_OFF.xml" to "animatedbutton_off.xml". And from "animated_button_on.xml" to "animatedbutton_on.xml"
这篇关于动画切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!