前言
? ? ????Android開發(fā)中,App的設計是為了用戶的更好的使用體驗椭住,所以衍生出了很多開發(fā)者們自定義的控件,可炫酷可簡約字逗,都是為了用戶的使用體驗為第一考慮標準京郑。當然,App的性能要求是硬核標準葫掉,而應用的設計同樣也是用戶們喜歡app的一大理由些举。
實現(xiàn)效果
源碼
MainActivity
public class MainActivity extends AppCompatActivity {
private ImageViewicon_home;
? ? private ImageViewicon_menu;
? ? private RelativeLayoutlevel1;
? ? private RelativeLayoutlevel2;
? ? private RelativeLayoutlevel3;
? ? private boolean isShowLevel3 =true;
? ? private boolean isShowLevel2 =true;
? ? private boolean isShowLevel1 =true;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? icon_home = findViewById(R.id.icon_home);
? ? ? ? icon_menu = findViewById(R.id.icon_menu);
? ? ? ? level1 = findViewById(R.id.level1);
? ? ? ? level2 = findViewById(R.id.level2);
? ? ? ? level3 = findViewById(R.id.level3);
? ? ? ? MyOnClickListener myOnClickListener =new MyOnClickListener();
? ? ? ? icon_home.setOnClickListener(myOnClickListener);
? ? ? ? icon_menu.setOnClickListener(myOnClickListener);
? ? ? ? level1.setOnClickListener(myOnClickListener);
? ? ? ? level2.setOnClickListener(myOnClickListener);
? ? ? ? level3.setOnClickListener(myOnClickListener);
? ? }
class MyOnClickListenerimplements View.OnClickListener {
@Override
? ? ? ? public void onClick(View v) {
switch (v.getId()) {
case R.id.level1:
Toast.makeText(MainActivity.this, "level1", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.level2:
Toast.makeText(MainActivity.this, "level2", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.level3:
Toast.makeText(MainActivity.this, "level3", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.icon_home:
if (isShowLevel2) {
//隱藏二級菜單
? ? ? ? ? ? ? ? ? ? ? ? isShowLevel2 =false;
? ? ? ? ? ? ? ? ? ? ? ? Tools.hideView(level2);
? ? ? ? ? ? ? ? ? ? ? ? if (isShowLevel3) {
//隱藏三級菜單
? ? ? ? ? ? ? ? ? ? ? ? ? ? isShowLevel3 =false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? Tools.hideView(level3, 200);
? ? ? ? ? ? ? ? ? ? ? ? }
}else {
//如果都是隱藏的,二級菜單顯示
//顯示二級菜單
? ? ? ? ? ? ? ? ? ? ? ? isShowLevel2 =true;
? ? ? ? ? ? ? ? ? ? ? ? Tools.showView(level2);
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? ? ? case R.id.icon_menu:
if (isShowLevel3) {
//隱藏
? ? ? ? ? ? ? ? ? ? ? ? isShowLevel3 =false;
? ? ? ? ? ? ? ? ? ? ? ? Tools.hideView(level3);
? ? ? ? ? ? ? ? ? ? }else {
//顯示
? ? ? ? ? ? ? ? ? ? ? ? isShowLevel3 =true;
? ? ? ? ? ? ? ? ? ? ? ? Tools.showView(level3);
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? }
}
}
@Override
? ? public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode ==KeyEvent.KEYCODE_MENU){
//如果一級俭厚,二級户魏,三級菜單是顯示的就全部隱藏
? ? ? ? ? ? if(isShowLevel1){
isShowLevel1 =false;
? ? ? ? ? ? ? ? Tools.hideView(level1);
? ? ? ? ? ? ? ? if(isShowLevel2){
//隱藏二級菜單
? ? ? ? ? ? ? ? ? ? isShowLevel2 =false;
? ? ? ? ? ? ? ? ? ? Tools.hideView(level2,200);
? ? ? ? ? ? ? ? ? ? if(isShowLevel3){
//隱藏三級菜單
? ? ? ? ? ? ? ? ? ? ? ? isShowLevel3 =false;
? ? ? ? ? ? ? ? ? ? ? ? Tools.hideView(level3,400);
? ? ? ? ? ? ? ? ? ? }
}
}else{
//如果一級,二級菜單隱藏挪挤,就顯示
//顯示一級菜單
? ? ? ? ? ? ? ? isShowLevel1 =true;
? ? ? ? ? ? ? ? Tools.showView(level1);
? ? ? ? ? ? ? ? //顯示二級菜單
? ? ? ? ? ? ? ? isShowLevel2 =true;
? ? ? ? ? ? ? ? Tools.showView(level2,200);
? ? ? ? ? ? }
return true;
? ? ? ? }
return super.onKeyDown(keyCode, event);
? ? }
}
Tools
public static void hideView(ViewGroup view) {
? ? ? ? hideView(view,0);
? ? }
public static void showView(ViewGroup view) {
showView(view,0);
? ? }
? ? public static void hideView(ViewGroup view, int startDelay) {
? ? ? ? ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotation",0,180);
? ? ? ? animator.setDuration(500);
? ? ? ? animator.setStartDelay(startDelay);
? ? ? ? animator.start();
? ? ? ? view.setPivotX(view.getWidth()/2);
? ? ? ? view.setPivotY(view.getHeight());
? ? }
public static void showView(ViewGroup view, int startDelay) {
? ? ? ? ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotation",180,360);
? ? ? ? animator.setDuration(500);
? ? ? ? animator.setStartDelay(startDelay);
? ? ? ? animator.start();
? ? ? ? view.setPivotX(view.getWidth()/2);
? ? ? ? view.setPivotY(view.getHeight());
? ? }
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity">
? ? ? ? android:id="@+id/level3"
? ? ? ? android:layout_width="280dp"
? ? ? ? android:layout_height="140dp"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:background="@drawable/level3">
? ? ? ? ? ? android:id="@+id/channel1"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginLeft="8dp"
? ? ? ? ? ? android:src="@drawable/channel1" />
? ? ? ? ? ? android:id="@+id/channel2"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_above="@id/channel1"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginLeft="33dp"
? ? ? ? ? ? android:src="@drawable/channel2" />
? ? ? ? ? ? android:id="@+id/channel3"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_above="@id/channel2"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginLeft="63dp"
? ? ? ? ? ? android:src="@drawable/channel3" />
? ? ? ? ? ? android:id="@+id/channel4"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? ? ? android:layout_marginTop="8dp"
? ? ? ? ? ? android:src="@drawable/channel4" />
? ? ? ? ? ? android:id="@+id/channel7"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginRight="8dp"
? ? ? ? ? ? android:src="@drawable/channel7" />
? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? android:id="@+id/channel6"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_above="@id/channel7"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginRight="33dp"
? ? ? ? ? ? android:src="@drawable/channel6" />
? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? android:id="@+id/channel5"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_above="@id/channel6"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginRight="63dp"
? ? ? ? ? ? android:src="@drawable/channel5" />
? ? ? ? android:id="@+id/level2"
? ? ? ? android:layout_width="180dp"
? ? ? ? android:layout_height="90dp"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:background="@drawable/level2">
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginLeft="8dp"
? ? ? ? ? ? android:src="@drawable/icon_search" />
? ? ? ? ? ? android:id="@+id/icon_menu"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? ? ? android:layout_marginTop="8dp"
? ? ? ? ? ? android:src="@drawable/icon_menu" />
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? ? ? android:layout_alignParentRight="true"
? ? ? ? ? ? android:layout_marginBottom="8dp"
? ? ? ? ? ? android:layout_marginRight="8dp"
? ? ? ? ? ? android:src="@drawable/icon_myyouku" />
? ? ? ? android:id="@+id/level1"
? ? ? ? android:layout_width="100dp"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:background="@drawable/level1">
? ? ? ? ? ? android:id="@+id/icon_home"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_centerInParent="true"
? ? ? ? ? ? android:src="@drawable/icon_home" />
</RelativeLayout>