android5.0之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;
}
}
});
}
}
参考:android群英传