package com.loaderman.youku;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView ivHome;
private ImageView ivMenu;
private RelativeLayout rlLevel1;
private RelativeLayout rlLevel2;
private RelativeLayout rlLevel3;
private boolean isLevel3Show = true;//标记布局是否显示
private boolean isLevel2Show = true;//标记布局是否显示
private boolean isLevel1Show = true;//标记布局是否显示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivHome = (ImageView) findViewById(R.id.iv_home);
ivMenu = (ImageView) findViewById(R.id.iv_menu);
rlLevel1 = (RelativeLayout) findViewById(R.id.rl_level1);
rlLevel2 = (RelativeLayout) findViewById(R.id.rl_level2);
rlLevel3 = (RelativeLayout) findViewById(R.id.rl_level3);
ivHome.setOnClickListener(this);
ivMenu.setOnClickListener(this);
//如果给第三层布局加点击事件, 会导致home和menu按钮无法点击
//解决办法: 将第三层布局写在最底下, 第一层写在最上面, 就不会挡住点击事件了
rlLevel3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_home:
System.out.println("home clicked!!!");
//如果显示则隐藏;反之亦然
if (isLevel2Show) {
AnimUtils.hide(rlLevel2);
isLevel2Show = false;
//如果level3显示, 也要隐藏
if (isLevel3Show) {
AnimUtils.hide(rlLevel3, 200);
isLevel3Show = false;
}
} else {
AnimUtils.show(rlLevel2);
isLevel2Show = true;
}
break;
case R.id.iv_menu:
System.out.println("menu clicked!!!");
//如果显示则隐藏;反之亦然
if (isLevel3Show) {
AnimUtils.hide(rlLevel3);
isLevel3Show = false;
} else {
AnimUtils.show(rlLevel3);
isLevel3Show = true;
}
break;
default:
break;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
//拦截物理按键
//拦截不了: home键, 电源键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("当前按键:" + keyCode);
//物理菜单键
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (isLevel1Show) {
AnimUtils.hide(rlLevel1);
isLevel1Show = false;
if (isLevel2Show) {
AnimUtils.hide(rlLevel2, 200);
isLevel2Show = false;
}
if (isLevel3Show) {
AnimUtils.hide(rlLevel3, 300);
isLevel3Show = false;
}
} else {
AnimUtils.show(rlLevel1);
AnimUtils.show(rlLevel2, 200);
isLevel1Show = true;
isLevel2Show = true;
}
return true;//表示已经处理完事件
}
return super.onKeyDown(keyCode, event);
}
}
package com.loaderman.youku;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
public class AnimUtils {
//利用重载, 可以在不改变原来api的情况下, 增添新的参数
public static void show(ViewGroup view) {
show(view, 0);
}
public static void hide(ViewGroup view) {
hide(view, 0);
}
//隐藏, 旋转动画
public static void hide(ViewGroup view, long delay) {
//围绕底边中心点旋转
RotateAnimation anim = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1);
anim.setDuration(500);
anim.setFillAfter(true);//动画结束后保持住当时状态
anim.setStartOffset(delay);//延时多长时间之后再启动动画
view.startAnimation(anim);
//禁用所有按钮的点击事件
int childCount = view.getChildCount();//子控件个数
for (int i = 0; i < childCount; i++) {
View child = view.getChildAt(i);//获取第i个子控件
child.setEnabled(false);//禁用
}
}
//显示, 旋转动画
public static void show(ViewGroup view, long delay) {
//围绕底边中心点旋转
RotateAnimation anim = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1);
anim.setDuration(500);
anim.setFillAfter(true);//动画结束后保持住当时状态
anim.setStartOffset(delay);//延时多长时间之后再启动动画
view.startAnimation(anim);
//启用所有按钮的点击事件
int childCount = view.getChildCount();//子控件个数
for (int i = 0; i < childCount; i++) {
View child = view.getChildAt(i);//获取第i个子控件
child.setEnabled(true);//启用
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.loaderman.youku.MainActivity">
<RelativeLayout
android:id="@+id/rl_level3"
android:layout_width="280dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3">
<ImageView
android:id="@+id/iv_channel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:src="@drawable/channel1"/>
<ImageView
android:id="@+id/iv_channel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_channel1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="35dp"
android:src="@drawable/channel2"/>
<ImageView
android:id="@+id/iv_channel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_channel2"
android:layout_marginBottom="12dp"
android:layout_marginLeft="70dp"
android:src="@drawable/channel3"/>
<ImageView
android:id="@+id/iv_channel4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="@drawable/channel4"/>
<ImageView
android:id="@+id/iv_channel7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@drawable/channel7"/>
<ImageView
android:id="@+id/iv_channel6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_channel7"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="35dp"
android:src="@drawable/channel6"/>
<ImageView
android:id="@+id/iv_channel5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_channel6"
android:layout_alignParentRight="true"
android:layout_marginBottom="12dp"
android:layout_marginRight="70dp"
android:src="@drawable/channel5"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_level2"
android:layout_width="180dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:src="@drawable/icon_search"/>
<ImageView
android:id="@+id/iv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="@drawable/icon_menu"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@drawable/icon_myyouku"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1"
>
<ImageView
android:id="@+id/iv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/icon_home"/>
</RelativeLayout>
</RelativeLayout>