关于path按钮的延生

最近在公司实在闲的无聊,遂找点儿事来做做。之前一直想弄弄path的那个那妞没有时间。最近研究了一下。不废话先上图吧


好吧,图不是动图,有点看不出来,这是在pad上运行的。

开始解释吧。效果大概是这个的实现的:将n个不同的按钮叠在同一个位置,在开始为它们计算设计动画:

先贴代码吧:

package com.alanz.path;

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

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

public class ButtonViewGroup extends RelativeLayout {
	private boolean isOut = false;
	private final static int RADIUS = 200;
	public final static String DEGREE_360 = "360";
	List<AnimationSet> outAnimationSets = new ArrayList<AnimationSet>();
	List<AnimationSet> inAnimationSets = new ArrayList<AnimationSet>();

	public ButtonViewGroup(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public ButtonViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ButtonViewGroup(Context context) {
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		init();
	}

	private void init() {
		int childCount = this.getChildCount();
		if (childCount < 4)
			throw new RuntimeException("子控件的数目必须要不小于4:" + childCount);
		initAnimation(true);
		initAnimation(false);
		for (int i = 0; i < childCount; i++) {
			this.getChildAt(i).setOnClickListener(
					new ButtonClickListener(i, this.getChildCount() - 1));
		}
	}

	class ButtonClickListener implements OnClickListener {
		private int index = -1;
		private int maxIndex;

		public ButtonClickListener(int index, int maxindex) {
			this.index = index;
			maxIndex = maxindex;
		}

		@Override
		public void onClick(View view) {
			if (index == maxIndex) {

				for (int i = 0; i < getChildCount() - 1; i++) {
					final View buttonView = getChildAt(i);
					AnimationSet set = null;
					if (isOut) {
						set = outAnimationSets.get(i);
					} else {
						set = inAnimationSets.get(i);
					}
					buttonView.startAnimation(set);
				}
				if (isOut) {
					isOut = false;
				} else {
					isOut = true;
				}

			} else {
			}
		}
	}

	private void initAnimation(final boolean isout) {
		final View mainView = getChildAt(getChildCount() - 1);
		int count = getChildCount() - 2;
		int d = Integer.parseInt((String) getTag());
		if (d == 360) {
			count = count +1;
		}
		if (d > 360) {
			if (d % 360 == 0) {
				d = 360;
			} else {
				d %= 360;
			}
		}
		for (int i = 0; i < getChildCount() - 1; i++) {
			final View buttonView = getChildAt(i);
			float x = 0, y = 0;
			// if(getTag() != null){
			double degree;
			degree = Math.PI / 180 * d * i / count;
			//
			x = (float) (RADIUS * Math.cos(degree));
			y = (float) (RADIUS * Math.sin(degree)) * -1;
			// }
			// }else{
			// if (i == 0) {
			// x = RADIUS;
			// y = 0;
			// } else if (i == getChildCount() - 2) {
			// x = 0;
			// y = -RADIUS;
			// } else {
			// double degree = Math.PI/2 * i
			// / count;
			// x = (float) (RADIUS * Math.cos(degree));
			// y = (float) (RADIUS * Math.sin(degree)) * -1;
			// }
			// }
			if (isout) {
				x = -x;
				y = -y;
			}
			TranslateAnimation translateAnimation = new TranslateAnimation(0,
					x, 0, y);
			final AnimationSet outAfterSet = new AnimationSet(true);
			outAfterSet.setInterpolator(new AccelerateDecelerateInterpolator());
			outAfterSet.addAnimation(translateAnimation);
			outAfterSet.setDuration(500 + i * 50);
			outAfterSet.setFillAfter(true);
			final float startX = x;
			final float startY = y;
			final int index = i;
			outAfterSet.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation arg0) {
					if (index == 0) {
						mainView.setEnabled(false);
					}
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {

				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					if (index == getChildCount() - 2) {
						mainView.setEnabled(true);
					}
					buttonView.clearAnimation();
					if (isout) {
						buttonView.layout(mainView.getLeft(),
								mainView.getTop(), mainView.getLeft()
										+ buttonView.getWidth(),
								mainView.getTop() + buttonView.getHeight());
					} else {
						buttonView.layout(
								mainView.getLeft() + (int) startX,
								mainView.getTop() + (int) startY,
								mainView.getLeft() + (int) startX
										+ buttonView.getWidth(),
								mainView.getTop() + (int) startY
										+ buttonView.getHeight());
					}
					// buttonView.startAnimation(outAfterSet);
				}
			});

			if (isout) {
				outAnimationSets.add(outAfterSet);
			} else {
				inAnimationSets.add(outAfterSet);
			}
		}
	}
}

先看,有时间再解释吧


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值