通过Animator实现弧形弹出菜单

//布局文件





    

    

    

    

    

    

    

    

通过Animator实现弧形弹出菜单_第1张图片

效果图;


下面是JAVA代码;

package com.imooc.animator;

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

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

public class ThirdActivity extends Activity implements OnClickListener {

	private int[] resId = { R.id.imageView1, R.id.imageView2, R.id.imageView3,
			R.id.imageView4, R.id.imageView5, R.id.imageView6, R.id.imageView7,
			R.id.imageView8 };

	private List imgList = new ArrayList();

	private boolean isStop = true;
	private int width;
	//转换成角度
	double arc = Math.toRadians(15);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animatorfortest);
		// 初始化控件
		for (int i = 0; i < resId.length; i++) {
			ImageView imgView = (ImageView) findViewById(resId[i]);
			imgView.setOnClickListener(this);
			imgList.add(imgView);
		}

		// 获取屏幕宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels; // 屏幕宽度;

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageView1:
			if (isStop) {
				// 展开动画
				startMyAnimation();
			} else {
				// 收回动画
				stopMyAnimation();
			}
			break;

		default:
			Toast.makeText(ThirdActivity.this, "点击了:" + v.getId(),
					Toast.LENGTH_SHORT).show();
			break;
		}

	}

	// 收回动画
	private void stopMyAnimation() {

		for (int i = 1; i < imgList.size(); i++) {
			
			ObjectAnimator mAnimatorX = ObjectAnimator.ofFloat(imgList.get(i),
					"TranslationX", (float)((width/2)*Math.cos(arc*(i-1))),0F);
			ObjectAnimator mAnimatorY = ObjectAnimator.ofFloat(imgList.get(i),
					"TranslationY", (float)((width/2)*Math.sin(arc*(i-1))),0F);
			
			Log.i("lang", "[i="+i+"]X="+(float)((width/2)*Math.cos(arc*(i-1)))+",Y="+(float)((width/2)*Math.sin(arc*(i-1))));
			
			AnimatorSet set = new AnimatorSet();
			set.playTogether(mAnimatorX,mAnimatorY);
			set.setDuration(500);
			set.start();

			isStop = true;
		}

	}

	// 展开动画
	private void startMyAnimation() {
		for (int i = 1; i < imgList.size(); i++) {
			
			ObjectAnimator mAnimatorX = ObjectAnimator.ofFloat(imgList.get(i),
					"TranslationX", 0f,(float)((width/2)*Math.cos(arc*(i-1))));
			ObjectAnimator mAnimatorY = ObjectAnimator.ofFloat(imgList.get(i),
					"TranslationY", 0f,(float)((width/2)*Math.sin(arc*(i-1))));
			
			Log.i("lang", "[i="+i+"]X="+(float)((width/2)*Math.cos(arc*(i-1)))+",Y="+(float)((width/2)*Math.sin(arc*(i-1))));
			
			AnimatorSet set = new AnimatorSet();
			set.playTogether(mAnimatorX,mAnimatorY);
			set.setDuration(500);
			set.setInterpolator(new BounceInterpolator());
			set.start();
			
			isStop = false;
		}

	}

}

模拟器效果

通过Animator实现弧形弹出菜单_第2张图片


你可能感兴趣的:(Android,APP)