package com.sorgs.animtest;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int[] res = {R.id.iv_a, R.id.iv_b, R.id.iv_c, R.id.iv_d, R.id.iv_e, R.id.iv_f, R.id.iv_g, R.id.iv_h};
private List<ImageView> imageViewList = new ArrayList<>();
* 菜单打开或者关闭的标志
*/
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int re : res) {
ImageView imageView = (ImageView) findViewById(re);
imageView.setOnClickListener(this);
imageViewList.add(imageView);
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_a:
if (flag) {
startAnim();
} else {
emdAnim();
}
break;
default:
Toast.makeText(getApplication(), "click" + view.getId(), Toast.LENGTH_SHORT).show();
break;
}
}
* 关闭菜单动画
*/
private void emdAnim() {
for (int i = 1; i < res.length; i++) {
PropertyValuesHolder Y, X;
Y = PropertyValuesHolder.ofFloat("translationY", 0);
X = PropertyValuesHolder.ofFloat("translationX", 0);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageViewList.get(i), X, Y);
animator.setDuration(500);
animator.setStartDelay(i * 300);
animator.setInterpolator(new BounceInterpolator());
animator.start();
flag = true;
}
}
* 打开菜单动画
*/
private void startAnim() {
float x, y, n = imageViewList.get(0).getMeasuredHeight() * 2;
for (int i = 1; i < res.length; i++) {
int count = res.length - i;
float angle = (float) (Math.PI * 180 / 180);
x = (float) (n * Math.sin(angle / (res.length - 1) * count));
y = (float) (n * Math.cos(angle / (res.length - 1) * count));
PropertyValuesHolder Y, X;
Y = PropertyValuesHolder.ofFloat("translationY", y);
X = PropertyValuesHolder.ofFloat("translationX", x);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageViewList.get(i), X, Y);
animator.setDuration(500);
animator.setStartDelay(i * 300);
animator.setInterpolator(new BounceInterpolator());
animator.start();
flag = false;
}
}
}