代码写的随意,没有优化。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/coordinatorLayout" android:background="#333"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal"> <CheckBox android:id="@+id/cb1" android:button="@null" android:background="@drawable/selctor_video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone"/> <CheckBox android:id="@+id/cb2" android:button="@null" android:background="@drawable/selctor_mic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone"/> <CheckBox android:id="@+id/cb3" android:button="@null" android:background="@drawable/selctor_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone"/> <CheckBox android:id="@+id/cb4" android:button="@null" android:background="@drawable/selctor_bluetooth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone"/> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:background="@drawable/selctor_open" android:layout_centerInParent="true"/> </RelativeLayout> </LinearLayout>
public class MainActivity extends AppCompatActivity { private Button login; private CheckBox cb, cb1, cb2, cb3, cb4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb = findViewById(R.id.cb); cb1 = findViewById(R.id.cb1); cb2 = findViewById(R.id.cb2); cb3 = findViewById(R.id.cb3); cb4 = findViewById(R.id.cb4); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cb.isChecked()){ startAnimator(cb1,"translationY", 150); startAnimator(cb2,"translationX", 150); startAnimator(cb3,"translationY", -150); startAnimator(cb4,"translationX", -150); } else { closeAnimator(cb1, "translationY", 0); closeAnimator(cb2, "translationX", 0); closeAnimator(cb3, "translationY", 0); closeAnimator(cb4, "translationX", 0); } } }); } private void startAnimator(View view, String s, int v) { ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, s, v).setDuration(500); ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 0,1).setDuration(500); animator1.start(); animator2.start(); view.setVisibility(View.VISIBLE); } private void closeAnimator(View view, String s, int v) { ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, s, v).setDuration(500); ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 1,0).setDuration(500); animator1.start(); animator2.start(); } }