android 利用属性动画实现酷炫的圆形菜单

废话不哆嗦,直接上代码,反正也差不多没人看,就自己记录下咯




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());

资源以及demo的地址: DEMO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值