Animator属性动画
主函数
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
//判断是否弹出
private boolean flag = true;
//设置卫星菜单半径
private int radius = 500;
//定义菜单图标
private int[] res = new int[]{ R.id.id_img_a,R.id.id_img_b,R.id.id_img_c,R.id.id_img_d,R.id.id_img_e,R.id.id_img_f,R.id.id_img_g,R.id.id_img_h
};
//定义菜单列表
private List<ImageView> list = new ArrayList<ImageView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化菜单图标
for (int i = 0; i < res.length; i++) {
ImageView imageView = (ImageView) findViewById(res[i]);
imageView.setOnClickListener(this);
list.add(imageView);
}
}
/**
* 设置菜单点击事件
* @param view
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.id_img_a:
if (flag) {
startAnim();
flag = false;
} else {
closeAnim();
flag = true;
}
break;
case R.id.id_img_b:
break;
case R.id.id_img_c:
break;
case R.id.id_img_d:
break;
case R.id.id_img_e:
break;
case R.id.id_img_f:
break;
case R.id.id_img_g:
break;
case R.id.id_img_h:
break;
}
}
/**
* 关闭菜单
*/
private void closeAnim() {
for (int i = 1; i < res.length; i++) {
ObjectAnimator animator1 = ObjectAnimator.ofFloat(list.get(i), "translationX", (float)(radius*Math.cos((i-1)*Math.PI/12)), 0F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(list.get(i), "translationY", (float)(radius*Math.sin((i-1)*Math.PI/12)), 0F);
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1, animator2);
set.setDuration(500);
set.start();
}
}
/**
* 弹出菜单
*/
private void startAnim() {
for (int i = 1; i < res.length; i++) {
//设置x方向位移
ObjectAnimator animator1 = ObjectAnimator.ofFloat(list.get(i), "translationX", 0F, (float)(radius*Math.cos((i-1)*Math.PI/12)));
//设置y方向位移
ObjectAnimator animator2 = ObjectAnimator.ofFloat(list.get(i), "translationY", 0F, (float)(radius*Math.sin((i-1)*Math.PI/12)));
//初始化动画集合
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1, animator2);
set.setDuration(500);
set.setInterpolator(new BounceInterpolator());
set.start();
}
}
}
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hpf.animatordemo.MainActivity">
<ImageView
android:id="@+id/id_img_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/b"/>
<ImageView
android:id="@+id/id_img_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/c"/>
<ImageView
android:id="@+id/id_img_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/d"/>
<ImageView
android:id="@+id/id_img_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/e"/>
<ImageView
android:id="@+id/id_img_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/f"/>
<ImageView
android:id="@+id/id_img_g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/g"/>
<ImageView
android:id="@+id/id_img_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:src="@mipmap/h"/>
<ImageView
android:id="@+id/id_img_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@mipmap/a"/>
</FrameLayout>
Animation补间动画
稍后更新