Android 灵动菜单动画效果

通过ObjectAnimator实现一个灵活的菜单

public class PropertyTest extends Activity implements View.OnClickListener {

	private int[] mRes = { R.id.imageView_a, R.id.imageView_b,
			R.id.imageView_c, R.id.imageView_d, R.id.imageView_e };
	private List<ImageView> mImageViews = new ArrayList<ImageView>();
	private boolean mFlag = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.property);
		for (int i = 0; i < mRes.length; i++) {
			ImageView imageView = (ImageView) findViewById(mRes[i]);
			imageView.setOnClickListener(this);
			mImageViews.add(imageView);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageView_a:
			if (mFlag) {
				startAnim();
			} else {
				closeAnim();
			}
			break;
		default:
			Toast.makeText(PropertyTest.this, "" + v.getId(),
					Toast.LENGTH_SHORT).show();
			break;
		}
	}

	@SuppressLint("NewApi")
	private void closeAnim() {
		ObjectAnimator animator0 = ObjectAnimator.ofFloat(mImageViews.get(0),
				"alpha", 0.5F, 1F);
		ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageViews.get(1),
				"translationY", 200F, 0);
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageViews.get(2),
				"translationX", 200F, 0);
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageViews.get(3),
				"translationY", -200F, 0);
		ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageViews.get(4),
				"translationX", -200F, 0);
		AnimatorSet set = new AnimatorSet();
		set.setDuration(500);
		set.setInterpolator(new BounceInterpolator());
		set.playTogether(animator0, animator1, animator2, animator3, animator4);
		set.start();
		mFlag = true;
	}

	@SuppressLint("NewApi")
	private void startAnim() {
		ObjectAnimator animator0 = ObjectAnimator.ofFloat(mImageViews.get(0),
				"alpha", 1F, 0.5F);
		ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageViews.get(1),
				"translationY", 200F);
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageViews.get(2),
				"translationX", 200F);
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageViews.get(3),
				"translationY", -200F);
		ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageViews.get(4),
				"translationX", -200F);
		AnimatorSet set = new AnimatorSet();
		set.setDuration(500);
                // 插值器使用弹跳效果
               set.setInterpolator(new BounceInterpolator());
		set.playTogether(animator0, animator1, animator2, animator3, animator4);
		set.start();
		mFlag = false;
	}
}
xml部分:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_b"
        android:src="@drawable/b"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_c"
        android:src="@drawable/c"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_d"
        android:src="@drawable/d"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_e"
        android:src="@drawable/e"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_a"
        android:src="@drawable/a"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值