这个很有趣的指标通过AnimCheckBox实现,下载地址:https://github.com/lguipeng/AnimCheckBox
代码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- app:circle_color 点击后的背景颜色设置 -->
<!-- app:stroke_color 线条颜色和点击后的勾勾颜色设置 -->
<!-- app:stroke_width 线条宽度设置 -->
<com.github.lguipeng.library.animcheckbox.AnimCheckBox
android:id="@+id/checkBox"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
app:circle_color="#1976D2"
app:stroke_width="4dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkBox"
android:layout_centerHorizontal="true"
android:paddingTop="20dp"
android:text="button" />
</RelativeLayout>
MainActivity.java:
package com.zzw.testanimcheckbox;
import com.github.lguipeng.library.animcheckbox.AnimCheckBox;
import com.github.lguipeng.library.animcheckbox.AnimCheckBox.OnCheckedChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean temp=true;
private AnimCheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox = (AnimCheckBox) findViewById(R.id.checkBox);
// 设置默认显示为勾还是圈
checkBox.setChecked(temp);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onChange(boolean checked) {
if(checked==true){
Toast.makeText(getApplicationContext(), "true", 0).show();
}else{
Toast.makeText(getApplicationContext(), "false", 0).show();
}
}
});
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(temp==true){
temp=false;
}else{
temp=true;
}
//当点击按钮判断值temp变化了的时候,checkBox的随之变化,并且显示出动画效果,
//后面如果是false的话,动画就不会显示,并且画面不会出现变化
checkBox.setChecked(temp,true);
}
});
}
}