废话不哆嗦,直接上代码,反正也差不多没人看,就自己记录下咯
package com.example.testroundmenu; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.RotateAnimation; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; public class Main extends Activity { //下标分别是从下到上,从左到右 private RelativeLayout rl_1, rl_2, rl_3; private ImageView iv_rl1_home, iv_rl2_1, iv_rl2_2, iv_rl2_3, iv_rl3_1, iv_rl3_2, iv_rl3_3, iv_rl3_4, iv_rl3_5, iv_rl3_6, iv_rl3_7; private boolean isMidleMenuShow = true; private boolean isOutMenuShow = true; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } private void initView() { MyListener listener = new MyListener(); // 初始化控件,简单起见,并没有直接在xml中声明点击函数 rl_1 = (RelativeLayout) findViewById(R.id.rl_1); rl_2 = (RelativeLayout) findViewById(R.id.rl_2); rl_3 = (RelativeLayout) findViewById(R.id.rl_3); iv_rl1_home = (ImageView) findViewById(R.id.iv_home); iv_rl2_1 = (ImageView) findViewById(R.id.iv_search); iv_rl2_2 = (ImageView) findViewById(R.id.iv_menu); iv_rl2_3 = (ImageView) findViewById(R.id.iv_myyouku); iv_rl3_1 = (ImageView) findViewById(R.id.iv_out_1); iv_rl3_2 = (ImageView) findViewById(R.id.iv_out_2); iv_rl3_3 = (ImageView) findViewById(R.id.iv_out_3); iv_rl3_4 = (ImageView) findViewById(R.id.iv_out_4); iv_rl3_5 = (ImageView) findViewById(R.id.iv_out_5); iv_rl3_6 = (ImageView) findViewById(R.id.iv_out_6); iv_rl3_7 = (ImageView) findViewById(R.id.iv_out_7); // 添加点击监听 iv_rl1_home.setOnClickListener(listener); iv_rl2_1.setOnClickListener(listener); iv_rl2_2.setOnClickListener(listener); iv_rl2_3.setOnClickListener(listener); iv_rl3_1.setOnClickListener(listener); iv_rl3_2.setOnClickListener(listener); iv_rl3_3.setOnClickListener(listener); iv_rl3_4.setOnClickListener(listener); iv_rl3_5.setOnClickListener(listener); iv_rl3_6.setOnClickListener(listener); iv_rl3_7.setOnClickListener(listener); } /** * @author kk_imgod imageview 的监听函数 */ public class MyListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.iv_home: if (isMidleMenuShow) { isMidleMenuShow = false; animatrHiden(rl_2, 500); //如果最外层的菜单还在显示的话,那就一起隐藏 if(isOutMenuShow) { animatrHiden(rl_3, 1000); isOutMenuShow = false; } } else { isMidleMenuShow = true; animatrShow(rl_2, 500); } showToast("HOME"); break; case R.id.iv_menu: showToast("我的菜单"); if(isOutMenuShow) { animatrHiden(rl_3, 1000); isOutMenuShow = false; } else { animatrShow(rl_3, 1000); isOutMenuShow = true; } break; case R.id.iv_search: showToast("我的搜索"); break; case R.id.iv_myyouku: showToast("我的优酷"); break; case R.id.iv_out_1: showToast("第一个"); break; case R.id.iv_out_2: showToast("第二个"); break; case R.id.iv_out_3: showToast("第三个"); break; case R.id.iv_out_4: showToast("第四个"); break; case R.id.iv_out_5: showToast("第五个"); break; case R.id.iv_out_6: showToast("第六个"); break; case R.id.iv_out_7: showToast("第七个"); break; default: break; } } } /** * @param text * 显示吐司的文本 */ public void showToast(String text) { Toast.makeText(Main.this, text, Toast.LENGTH_SHORT).show(); } /** * 让视图动画显示出来,180-360度 */ public void animatrShow(View view, long durction) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 180f, 360f ); //设置旋转点 view.setPivotX(view.getWidth() / 2); view.setPivotY(view.getHeight()); objectAnimator.setDuration(durction).start(); } /** * 让视图动画隐藏起来 0-180度 */ public void animatrHiden(View view, long durction) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f); //设置动画的旋转点 view.setPivotX(view.getWidth() / 2); view.setPivotY(view.getHeight()); objectAnimator.setDuration(durction).start(); } //下面的这种隐藏和显示动画的效果是传统动画,没有交互效果 /** * @param view 0-180让视图隐藏 */ public static void startAnimout(RelativeLayout view) { // TODO Auto-generated method stub RotateAnimation rotateAnimation = new RotateAnimation(0, 180, view.getWidth() / 2, view.getHeight()); rotateAnimation.setDuration(500); rotateAnimation.setFillAfter(true); view.startAnimation(rotateAnimation); } /** * @param view 180-360让视图出现 */ public static void startAnimin(RelativeLayout view) { // TODO Auto-generated method stub RotateAnimation rotateAnimation = new RotateAnimation(180, 360, view.getWidth() / 2, view.getHeight()); rotateAnimation.setDuration(500); rotateAnimation.setFillAfter(true); view.startAnimation(rotateAnimation); } }关键性代码:设置旋转点
//设置旋转点 view.setPivotX(view.getWidth() / 2); view.setPivotY(view.getHeight());