这个很有趣的指标通过AnimCheckBox实现,下载地址:https://github.com/lguipeng/AnimCheckBox
代码:
activity_main.xml:
1 <RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"
2 xmlns:tools&#61;"http://schemas.android.com/tools"
3 xmlns:app&#61;"http://schemas.android.com/apk/res-auto"
4 android:layout_width&#61;"match_parent"
5 android:layout_height&#61;"match_parent" >
6
7
8
9
10
11 <com.github.lguipeng.library.animcheckbox.AnimCheckBox
12 android:id&#61;"&#64;&#43;id/checkBox"
13 android:layout_width&#61;"80dp"
14 android:layout_height&#61;"80dp"
15 android:layout_centerInParent&#61;"true"
16 app:circle_color&#61;"#1976D2"
17 app:stroke_width&#61;"4dp" />
18
19 <Button
20 android:id&#61;"&#64;&#43;id/button"
21 android:layout_width&#61;"wrap_content"
22 android:layout_height&#61;"wrap_content"
23 android:layout_below&#61;"&#64;id/checkBox"
24 android:layout_centerHorizontal&#61;"true"
25 android:paddingTop&#61;"20dp"
26 android:text&#61;"button" />
27
28 RelativeLayout>
MainActivity.java&#xff1a;
1 package com.zzw.testanimcheckbox;
2
3 import com.github.lguipeng.library.animcheckbox.AnimCheckBox;
4 import com.github.lguipeng.library.animcheckbox.AnimCheckBox.OnCheckedChangeListener;
5
6 import android.app.Activity;
7 import android.os.Bundle;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.Toast;
11
12 public class MainActivity extends Activity {
13 private boolean temp&#61;true;
14 private AnimCheckBox checkBox;
15 &#64;Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 checkBox &#61; (AnimCheckBox) findViewById(R.id.checkBox);
21 // 设置默认显示为勾还是圈
22 checkBox.setChecked(temp);
23
24 checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
25 &#64;Override
26 public void onChange(boolean checked) {
27 if(checked&#61;&#61;true){
28 Toast.makeText(getApplicationContext(), "true", 0).show();
29 }else{
30 Toast.makeText(getApplicationContext(), "false", 0).show();
31 }
32 }
33 });
34 findViewById(R.id.button).setOnClickListener(new OnClickListener() {
35
36 &#64;Override
37 public void onClick(View v) {
38 if(temp&#61;&#61;true){
39 temp&#61;false;
40 }else{
41 temp&#61;true;
42 }
43 //当点击按钮判断值temp变化了的时候&#xff0c;checkBox的随之变化&#xff0c;并且显示出动画效果&#xff0c;
44 //后面如果是false的话&#xff0c;动画就不会显示&#xff0c;并且画面不会出现变化
45 checkBox.setChecked(temp,true);
46 }
47 });
48 }
49 }