android - 动画切换按钮

标签 android toggle android-animation

我已经创建了动画切换,如果 android:checked="true" 它可以完美运行,即:它可以从 1-9 播放 drawable 但如果 android:checked="false" 它卡在 drawable 10 但它应该从 10-20 开始播放。从那时起它工作完美但是当应用程序启动时它有上述问题。

为什么它卡在 drawable 10 而实际上它应该在 20 因为那是最终图像。(关闭)目前因为它卡在 10 它看起来它仍然是 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>

/////////////////

主.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

<?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

<?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

    <?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

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”

关于android - 动画切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17990603/

相关文章:

java - 使用广播在摇晃时启动应用程序

java - 将当前项目位置传递给内部类?

javascript - 我的 javascript showhide 代码上的奇怪错误

javascript - 汉堡包菜单切换 Javascript

Android - RecyclerView 动画 notifyItemChanged 在 notifyItemRemoved 之后

java - Android - 使用 AnimatorSet 缩放动画

android - 键盘弹出窗口在android中向上滚动完整布局

javascript - jquery 单独切换

android - 没有调用 start 的 ViewPropertyAnimator,有区别吗?

android - eclipse 中的谷歌播放服务错误