版权声明:以前的Blog文章合并。原创作品,谢绝转载!否则将追究法律责任。
定义布局文件
<span style="color:#666666;"><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" > <RelativeLayout android:layout_width="100dip" android:layout_height="50dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level1"> <ImageView android:id="@+id/home" android:layout_height="40dp" android:layout_width="40dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_marginBottom="5dp" android:background="@drawable/icon_home"/> </RelativeLayout> <RelativeLayout android:id="@+id/level2" android:layout_width="180dip" android:layout_height="90dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level2"> <ImageView android:id="@+id/icon_search" android:layout_height="30dp" android:layout_width="30dp" android:layout_margin="8dip" android:layout_alignParentBottom="true" android:background="@drawable/icon_search"/> <ImageView android:id="@+id/icon_menu" android:layout_height="30dp" android:layout_width="30dp" android:layout_marginTop="8dp" android:layout_centerHorizontal="true" android:src="@drawable/icon_menu"/> <ImageView android:id="@+id/icon_search" android:layout_height="30dp" android:layout_width="30dp" android:layout_margin="8dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/icon_myyouku"/> </RelativeLayout> <RelativeLayout android:id="@+id/level3" android:layout_width="280dip" android:layout_height="140dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level3"> <ImageView android:id="@+id/channel1" android:layout_height="30dp" android:layout_width="30dp" android:layout_margin="10dip" android:layout_alignParentBottom="true" android:background="@drawable/channel1"/> <ImageView android:id="@+id/channel2" android:layout_height="30dp" android:layout_width="30dp" android:layout_marginLeft="35dp" android:layout_marginBottom="50dp" android:layout_alignParentBottom="true" android:background="@drawable/channel2"/> <ImageView android:id="@+id/channel3" android:layout_height="30dp" android:layout_width="30dp" android:layout_marginLeft="70dp" android:layout_marginBottom="85dp" android:layout_alignParentBottom="true" android:background="@drawable/channel3" /> <ImageView android:id="@+id/channel4" android:layout_height="30dp" android:layout_width="30dp" android:layout_margin="10dip" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@drawable/channel4"/> <ImageView android:id="@+id/channel5" android:layout_height="30dp" android:layout_width="30dp" android:layout_marginRight="70dp" android:layout_marginBottom="85dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/channel5" /> <ImageView android:id="@+id/channel6" android:layout_height="30dp" android:layout_width="30dp" android:layout_marginRight="35dp" android:layout_marginBottom="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/channel6"/> <ImageView android:id="@+id/channel7" android:layout_height="30dp" android:layout_width="30dp" android:layout_margin="10dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/channel7"/> </RelativeLayout> </RelativeLayout></span>动画帮助类
public class ExpandAnim { public static void RotateIn(final ViewGroup group,long duration,long offset){ group.setVisibility(View.VISIBLE); RotateAnimation animation=new RotateAnimation(-180.0f,0.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.0f); animation.setDuration(duration); animation.setStartOffset(offset); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } }); group.startAnimation(animation); } public static void RotateOut(final ViewGroup group,long duration,long offset){ RotateAnimation animation=new RotateAnimation(0.0f,-180.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.0f); animation.setDuration(duration); animation.setStartOffset(offset); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { group.setVisibility(View.GONE); } }); group.startAnimation(animation); } public static ScaleAnimation scaleSubMenu(float toX,float toY){ ScaleAnimation animation=new ScaleAnimation(1.0f, toX, 1.0f, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(500l); animation.setFillAfter(false); return animation; } public static AlphaAnimation alphaSubMenuOut(){ AlphaAnimation animation=new AlphaAnimation(1.0f,0.0f); animation.setDuration(500l); animation.setFillAfter(false); return animation; } }
调用
public class MainActivity extends Activity implements OnClickListener{ private RelativeLayout mLevel2; private RelativeLayout mLevel3; private ImageView mHomeIv; private ImageView mMenuIv; private boolean isLevel2Show=true; private boolean isLevel3Show=true; private ImageView mChannel1; private ImageView mChannel2; private ImageView mChannel3; private ImageView mChannel4; private ImageView mChannel5; private ImageView mChannel6; private ImageView mChannel7; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initUI(); } private void initUI() { mLevel2=(RelativeLayout) findViewById(R.id.level2); mLevel3=(RelativeLayout) findViewById(R.id.level3); mHomeIv=(ImageView) findViewById(R.id.home); mHomeIv.setOnClickListener(this); mMenuIv=(ImageView) findViewById(R.id.icon_menu); mMenuIv.setOnClickListener(this); mChannel1=(ImageView) findViewById(R.id.channel1); mChannel1.setOnClickListener(this); mChannel2=(ImageView) findViewById(R.id.channel2); mChannel2.setOnClickListener(this); mChannel3=(ImageView) findViewById(R.id.channel3); mChannel3.setOnClickListener(this); mChannel4=(ImageView) findViewById(R.id.channel4); mChannel4.setOnClickListener(this); mChannel5=(ImageView) findViewById(R.id.channel5); mChannel5.setOnClickListener(this); mChannel6=(ImageView) findViewById(R.id.channel6); mChannel6.setOnClickListener(this); mChannel7=(ImageView) findViewById(R.id.channel7); mChannel7.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.home: if (!isLevel2Show) { ExpandAnim.RotateIn(mLevel2,500,0); }else { if (isLevel3Show) { ExpandAnim.RotateOut(mLevel3,500,0); ExpandAnim.RotateOut(mLevel2,500,500); isLevel3Show=!isLevel3Show; }else { ExpandAnim.RotateOut(mLevel2,500,0); } } isLevel2Show=!isLevel2Show; break; case R.id.icon_menu: if (isLevel3Show) { ExpandAnim.RotateOut(mLevel3,500,0); }else { ExpandAnim.RotateIn(mLevel3,500,0); } isLevel3Show=!isLevel3Show; break; default: for (int i = 0; i < mLevel3.getChildCount(); i++) { if (mLevel3.getChildAt(i)!=v) { mLevel3.getChildAt(i).startAnimation(ExpandAnim.scaleSubMenu(0.0f, 0.0f)); }else { AnimationSet animationSet=new AnimationSet(true); animationSet.addAnimation(ExpandAnim.scaleSubMenu(3.0f, 3.0f)); animationSet.addAnimation(ExpandAnim.alphaSubMenuOut()); mLevel3.getChildAt(i).startAnimation(animationSet); } } break; } } }运行如下: