animated-selector状态改变的动画效果

先看效果图:

在res/drawable/目录下创建一个animator_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:id="@+id/state_on" android:drawable="@drawable/btn_check_0" android:state_checked="false" />

    <item  android:id="@+id/state_off" android:state_checked="true" android:drawable="@drawable/btn_check_15" />

    <transition  android:fromId="@id/state_on" android:toId="@id/state_off">
        <animation-list>
            <item  android:drawable="@drawable/btn_check_0" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_1" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_2" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_3" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_4" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_5" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_6" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_7" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_8" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_9" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_10" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_11" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_12" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_13" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_14" android:duration="30" />
            <item  android:drawable="@drawable/btn_check_15" android:duration="30" />
        </animation-list>
    </transition>
</animated-selector>

有点类似帧动画

给对应的控件设置,如下:

<ImageView  android:id="@+id/check" android:background="@drawable/animator_selector" android:layout_width="50dp" android:layout_height="50dp" />

代码中:

public class MainActivity extends AppCompatActivity {


    private ImageView iv;
    private static final int[] STATE_CHECKED = new int[]{android.R.attr.state_checked};
    private static final int[] STATE_UNCHECKED = new int[]{};
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.check);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    iv.setImageState(STATE_UNCHECKED, true);
                    flag = false;
                } else {
                    iv.setImageState(STATE_CHECKED, true);
                    flag = true;
                }
            }
        });

    }
}

转自:https://www.ctolib.com/topics-74071.html

发布了29 篇原创文章 · 获赞 49 · 访问量 6610

猜你喜欢

转载自blog.csdn.net/wangsen927/article/details/94781945
今日推荐