前言
Android开发中,App的设计是为了用户的更好的使用体验,所以衍生出了很多开发者们自定义的控件,可炫酷可简约,都是为了用户的使用体验为第一考虑标准。当然,App的性能要求是硬核标准,而应用的设计同样也是用户们喜欢app的一大理由。
实现效果
旋转菜单源码
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>
网友评论