Android 属性动画实现的扇形菜单效果

属性动画实现的扇形菜单效果,超级简单。直接上图上干货。

Android 属性动画实现的扇形菜单效果_第1张图片

Android 属性动画实现的扇形菜单效果_第2张图片

代码MainActivity:

package com.daidai.sectormenu;

import java.util.ArrayList;
import java.util.List;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private int[] res = {R.id.iv_open, R.id.iv_camera, R.id.iv_music, R.id.iv_place, R.id.iv_sleep, R.id.iv_thought, R.id.iv_with};
	private List imageViewList = new ArrayList();
	private boolean isOpen = false;//菜单是否打开状态
	private final int r = 300;//扇形半径
	private float angle;//按钮之间的夹角
	private final long intervalTime = 100; //菜单展开的时间间隔
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initImageView();
		//计算按钮之间的夹角
		angle=(float)Math.PI/(2*(res.length-1));
		
	}
	
	/**
	 * 初始化View
	 */
	private void initImageView(){
		ImageView imageView = null;
		for(int i = 0; i < res.length; i++){
			imageView = (ImageView) findViewById(res[i]);
			imageView.setOnClickListener(this);
			imageViewList.add(imageView);
		}
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.iv_open:
			if(isOpen){
				closeAnim();
			}else{
				startAnim();
			}
			break;
		default:
			Toast.makeText(MainActivity.this, ""+view.getId(), Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	/**
	 * 展开菜单
	 */
	private void startAnim(){
		ObjectAnimator animatorX = null;
		ObjectAnimator animatorY = null;
		float translationX ;//横坐标偏移距离
		float translationY ;//纵坐标偏移距离
		for(int i = 1; i < res.length; i++){
			translationX = (float) (r*Math.sin(i*angle));
			translationY = (float) (r*Math.cos(i*angle));
			animatorX = ObjectAnimator.ofFloat(imageViewList.get(i),"translationX", 0F, translationX);
			animatorY = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY", 0F, translationY);
			AnimatorSet animSet = new AnimatorSet();
			animSet.playTogether(animatorX, animatorY);
			animSet.setDuration(i * intervalTime);
			animSet.start();
		}
		isOpen = true;
	}
	
	/**
	 * 关闭菜单
	 */
	private void closeAnim(){
		ObjectAnimator animatorX = null;
		ObjectAnimator animatorY = null;
		float translationX ;//横坐标偏移距离
		float translationY ;//纵坐标偏移距离
		for(int i = res.length - 1; i > 0; i--){
			translationX = (float) (r*Math.sin(i*angle));
			translationY = (float) (r*Math.cos(i*angle));
			animatorX = ObjectAnimator.ofFloat(imageViewList.get(i),"translationX", translationX, 0F);
			animatorY = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY", translationY, 0F);
			AnimatorSet animSet = new AnimatorSet();
			animSet.playTogether(animatorX, animatorY);
			animSet.setDuration((res.length - i) * intervalTime);
			animSet.start();
		}
		isOpen = false;
	}
	
	/**
	 * 监听是否按下返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event){
		//如果按下返回键,执行退出操作
		if(keyCode == KeyEvent.KEYCODE_BACK){
			showDialog();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 退出弹框
	 */
	private void showDialog(){
		new AlertDialog.Builder(this)
			.setTitle("退出")
			.setMessage("要退出么?")
			.setNegativeButton("退出", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						finish();
					}
				})
			.setPositiveButton("返回", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						
					}
			}).show();
	}
	

}

 
  

布局activity_main.xml:



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

本人是一名Java开发人员,初次涉及Android开发,希望大家能多多指点。

同时附上源码下载以供参考:http://pan.baidu.com/s/1qWDi0ri

你可能感兴趣的:(Android)