Android圆形散开动画

1 先看效果



主要就是五颗按钮向五个方向散开(我这里是按钮,也可以放成其它控件)。


2 实现

2.1 Android动画知识

http://blog.csdn.net/guolin_blog?viewmode=contents 请看这个博客,博主讲的非常详细。

2.2 直接上代码

希望代码是自解释的。

package com.example.animationbutton;

import android.app.Activity;
import android.content.Context;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.TypeEvaluator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

public class MainActivity extends Activity {

	/**
	 * 头像。
	 */
	private ImageView imageview;
	/**
	 * 屏幕宽度。
	 */
	private int window_width;
	/**
	 * 屏幕高度。
	 */
	private int window_height;
	/**
	 * 圆型按钮的宽度。
	 */
	private int image_width;
	/**
	 * 五角星。
	 */
	private ImageView wujiaoxing;
	/**
	 * 星品。
	 */
	private ImageView shop;
	/**
	 * 粉丝圈。
	 */
	private ImageView fans;
	/**
	 * 星踪迹。
	 */
	private ImageView news;
	/**
	 * 星表情。
	 */
	private ImageView expression;
	/**
	 * 星动时刻。
	 */
	private ImageView trails;
	/**
	 * 星盟logo。
	 */
	private ImageView istar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置全屏。
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		final View view = View.inflate(this, R.layout.activity_main, null);
		setContentView(view);
		AlphaAnimation aa = new AlphaAnimation(0.7f, 1.0f);
		aa.setDuration(500);
		view.startAnimation(aa);
		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				// ObjectAnimator fadeIn = ObjectAnimator.ofFloat(imageview,
				// "alpha", 0f, 1f);
				int startangle = 54;
				int length = (int) ((window_width
						/ (Math.sin(72 * Math.PI / 180)) - image_width) / 2 - image_width / 5);
				ValueAnimator expressionanim = zhixian(expression, startangle,
						length);
				ValueAnimator trailsanim = zhixian(trails, startangle + 72,
						length);
				ValueAnimator newsanim = zhixian(news, startangle + (72 * 2),
						length);
				ValueAnimator shopanim = zhixian(shop, startangle + (72 * 3),
						length);
				ValueAnimator fansanim = zhixian(fans, startangle + (72 * 4),
						length);
				AnimatorSet animSet = new AnimatorSet();

				ObjectAnimator fadeIn = ObjectAnimator.ofFloat(imageview,
						"alpha", 0f, 1f);
				ObjectAnimator fadeOut = ObjectAnimator.ofFloat(istar, "alpha",
						1f, 0f);

				animSet.playTogether(fadeIn, fadeOut, shopanim, fansanim,
						newsanim, expressionanim, trailsanim);
				animSet.setDuration(1000);
				animSet.start();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}

		});

		WindowManager wm = (WindowManager) getBaseContext().getSystemService(
				Context.WINDOW_SERVICE);
		window_width = wm.getDefaultDisplay().getWidth();
		window_height = wm.getDefaultDisplay().getHeight() - 20;
		image_width = window_width / 4;

		imageview = (ImageView) findViewById(R.id.head);
		RelativeLayout.LayoutParams paras = new RelativeLayout.LayoutParams(
				image_width, image_width);
		paras.leftMargin = window_width / 2 - image_width / 2;
		paras.topMargin = window_height / 2 - image_width / 2;
		imageview.setLayoutParams(paras);

		wujiaoxing = (ImageView) findViewById(R.id.wujiaoxing);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT); // , 1是可选写的
		lp.setMargins(image_width * 5 / 8, 0, image_width * 5 / 8, 0);
		wujiaoxing.setLayoutParams(lp);
		initbuttons();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	/**
	 * 沿直线运动。
	 * 
	 * @param view
	 *            要移动的对象。
	 * @param direction
	 *            方向 +1 正方形,-1 负方向。
	 * @param length
	 */
	public ValueAnimator zhixian(final View view, final int angle,
			final int length) {
		ValueAnimator valueAnimator = new ValueAnimator();
		valueAnimator.setObjectValues(new PointF(0, 0));
		valueAnimator.setInterpolator(new LinearInterpolator());
		valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
			// fraction = t / duration
			@Override
			public PointF evaluate(float fraction, PointF startValue,
					PointF endValue) {
				Log.v("znz", "znz ---> " + fraction);
				PointF point = new PointF();
				point.x = (float) (fraction * length * Math.cos(angle * Math.PI
						/ 180));
				point.y = (float) (fraction * length * Math.sin(angle * Math.PI
						/ 180));
				return point;
			}
		});
		valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				PointF point = (PointF) animation.getAnimatedValue();
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
						image_width, image_width);
				params.leftMargin = (int) point.x
						+ (window_width / 2 - image_width / 2); // Your
																// coordinate
				params.topMargin = (int) point.y
						+ (window_height / 2 - image_width / 2); // Your Y
																	// coordinate
				Log.v("znz", "point.x ---> " + point.x);
				Log.v("znz", "point.y ---> " + point.y);
				view.setLayoutParams(params);
			}
		});
		return valueAnimator;
	}

	/**
	 * 初始化五颗按钮。
	 */
	private void initbuttons() {
		shop = (ImageView) findViewById(R.id.shop);
		fans = (ImageView) findViewById(R.id.fans);
		news = (ImageView) findViewById(R.id.news);
		expression = (ImageView) findViewById(R.id.expression);
		trails = (ImageView) findViewById(R.id.trails);
		istar = (ImageView) findViewById(R.id.istartlogo);
		RelativeLayout.LayoutParams paras = new RelativeLayout.LayoutParams(
				image_width, image_width);
		// Your coordinate
		paras.leftMargin = window_width / 2 - image_width / 2;
		// Your Y coordinate
		paras.topMargin = window_height / 2 - image_width / 2;
		shop.setLayoutParams(paras);
		fans.setLayoutParams(paras);
		news.setLayoutParams(paras);
		expression.setLayoutParams(paras);
		trails.setLayoutParams(paras);
		istar.setLayoutParams(paras);
	}
}

2.3 项目下载

郭霖的专栏


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值