效果图
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" >
<RelativeLayout
android:id="@+id/level1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1" >
<ImageButton
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/icon_home" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/level2"
android:layout_width="180dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2" >
<ImageButton
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:background="@drawable/icon_search" />
<ImageButton
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/icon_menu" />
<ImageButton
android:id="@+id/myyouku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="@drawable/icon_myyouku" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/level3"
android:layout_width="290dp"
android:layout_height="130dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3" >
<ImageButton
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="6dip"
android:layout_marginLeft="12dip"
android:background="@drawable/channel1" />
<ImageButton
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/c1"
android:layout_marginBottom="6dp"
android:layout_marginLeft="30dp"
android:background="@drawable/channel2" />
<ImageButton
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/c2"
android:layout_marginBottom="6dp"
android:layout_marginLeft="65dp"
android:background="@drawable/channel3" />
<ImageButton
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:background="@drawable/channel4" />
<ImageButton
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c6"
android:layout_alignParentRight="true"
android:layout_marginBottom="6dip"
android:layout_marginRight="65dp"
android:background="@drawable/channel5" />
<ImageButton
android:id="@+id/c6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c7"
android:layout_alignParentRight="true"
android:layout_marginBottom="6dip"
android:layout_marginRight="30dip"
android:background="@drawable/channel6" />
<ImageButton
android:id="@+id/c7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="6dip"
android:layout_marginRight="12dip"
android:background="@drawable/channel7" />
</RelativeLayout>
</RelativeLayout>
Java动态控制显示代码
package com.myself.myyouku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private ImageButton ibHome;
private ImageButton ibMenu;
private boolean isLevel2Showing = true;
private boolean isLevel3Showing = true;
private RelativeLayout level2Layout;
private RelativeLayout level3Layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ibHome = (ImageButton) this.findViewById(R.id.home);
ibMenu = (ImageButton) this.findViewById(R.id.menu);
level2Layout = (RelativeLayout) this.findViewById(R.id.level2);
level3Layout = (RelativeLayout) this.findViewById(R.id.level3);
ibHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isLevel2Showing){
//二级菜单 隐藏--->显示
MyAnimation.StartAnimationIN(level2Layout, 500);
}else{
if(isLevel3Showing){
//三级菜单 显示--->隐藏
MyAnimation.StartAnimationOUT(level3Layout, 500, 0);
isLevel3Showing = !isLevel3Showing;
//延迟500毫秒启动
MyAnimation.StartAnimationOUT(level2Layout, 500, 500);
}else{
//二级菜单 显示--->隐藏
MyAnimation.StartAnimationOUT(level2Layout, 500, 0);
}
}
isLevel2Showing = !isLevel2Showing;
}
});
ibMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isLevel3Showing){
//三级菜单 隐藏---->显示
MyAnimation.StartAnimationIN(level3Layout, 500);
}
else{
//三级菜单 显示---->隐藏
MyAnimation.StartAnimationOUT(level3Layout, 500, 0);
}
isLevel3Showing = !isLevel3Showing;
}
});
}
}
动画效果实现类
package com.myself.myyouku;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
public class MyAnimation {
// 入动画
public static void StartAnimationIN(ViewGroup viewGroup, int duration) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
viewGroup.getChildAt(i).setVisibility(View.VISIBLE);
viewGroup.getChildAt(i).setClickable(true);// 可以点击
viewGroup.getChildAt(i).setFocusable(true);// 可以获得焦点
}
Animation animation;
/*
* fromDegrees 初始角度 toDegrees 结束角度 pivotXType x轴的参照物 pivotXValue
* 参照x轴的参照物 的哪个位置进行旋转 pivotYType Y轴的参照物 pivotYValue 参照Y轴的参照物 的哪个位置进行旋转
*/
animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);// 停留在 动画运行结束的位置
animation.setDuration(duration);// 动画运行时间
viewGroup.startAnimation(animation);
}
// 出动画
public static void StartAnimationOUT(final ViewGroup viewGroup, int duration, int startOffSet) {
Animation animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);// 停留在 动画运行结束的位置
animation.setDuration(duration);// 动画运行时间
animation.setStartOffset(startOffSet);// 动画延迟启动时间
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
for (int i = 0; i < viewGroup.getChildCount(); i++) {
viewGroup.getChildAt(i).setVisibility(View.GONE);
viewGroup.getChildAt(i).setClickable(false);// 不可以点击
viewGroup.getChildAt(i).setFocusable(false);// 不可以获得焦点
}
viewGroup.setVisibility(View.GONE);
}
});
viewGroup.startAnimation(animation);
}
}