Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 利用屬性動畫實現酷炫的圓形菜單

android 利用屬性動畫實現酷炫的圓形菜單

編輯:關於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());
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved