<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.s.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/b"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/c"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/d" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/e" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/f"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/g"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/add" />
</RelativeLayout>
package com.example.s;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mImageView1;
private ImageView mImageView2;
private ImageView mImageView3;
private ImageView mImageView4;
private ImageView mImageView5;
private ImageView mImageView6;
private ImageView mImageView7;
private List<ImageView>list=new ArrayList<>();
private int radius1=200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mImageView1 = (ImageView) findViewById(R.id.imageView1);
mImageView2 = (ImageView) findViewById(R.id.imageView2);
mImageView3 = (ImageView) findViewById(R.id.imageView3);
mImageView4 = (ImageView) findViewById(R.id.imageView4);
mImageView5 = (ImageView) findViewById(R.id.imageView5);
mImageView6 = (ImageView) findViewById(R.id.imageView6);
mImageView7 = (ImageView) findViewById(R.id.imageView7);
list.add(mImageView2);
list.add(mImageView3);
list.add(mImageView4);
list.add(mImageView5);
list.add(mImageView6);
mImageView7.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageView7:
// TODO 17/10/17
Boolean x= (Boolean) mImageView7.getTag();
if (x==null||x==false){
ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(mImageView7,"rotation",0,45);
objectAnimator.setDuration(500);
objectAnimator.start();
mImageView7.setTag(true);
show();
}else{
ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(mImageView7,"rotation",0,45);
objectAnimator.setDuration(500);
objectAnimator.start();
mImageView7.setTag(false);
clas();
}
break;
default:
break;
}
}
private void clas() {
/***第一步,遍历所要展示的菜单ImageView*/
for (int i = 0; i <list.size() ; i++) {
PointF point=new PointF();
/***第二步,根据菜单个数计算每个菜单之间的间隔角度*/
int avgAngle = (90 / (list.size() - 1));
/**第三步,根据间隔角度计算出每个菜单相对于水平线起始位置的真实角度**/
int angle = avgAngle * i;
//Log.d(TAG, "angle=" + angle);
/**
*
* 圆点坐标:(x0,y0)
* 半径:r
* 角度:a0
* 则圆上任一点为:(x1,y1)
* x1 = x0 + r * cos(ao * 3.14 /180 )
* y1 = y0 + r * sin(ao * 3.14 /180 )
*/
/**第四步,根据每个菜单真实角度计算其坐标值**/
point.x = (float) Math.cos(angle * (Math.PI / 180)) * radius1;
point.y = (float) -Math.sin(angle * (Math.PI / 180)) * radius1;
/**第五步,根据坐标执行位移动画**/
/**
* 第一个参数代表要操作的对象
* 第二个参数代表要操作的对象的属性
* 第三个参数代表要操作的对象的属性的起始值
* 第四个参数代表要操作的对象的属性的终止值
*/
ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(list.get(i), "translationX", point.x, 0);
ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(list.get(i), "translationY", point.y, 0);
/**动画集合,用来编排动画**/
AnimatorSet animatorSet = new AnimatorSet();
/**设置动画时长**/
animatorSet.setDuration(500);
/**设置同时播放x方向的位移动画和y方向的位移动画**/
animatorSet.play(objectAnimatorX).with(objectAnimatorY);
/**开始播放动画**/
animatorSet.start();
}
}
private void show() {
/***第一步,遍历所要展示的菜单ImageView*/
for (int i = 0; i < list.size(); i++) {
PointF point = new PointF();
/***第二步,根据菜单个数计算每个菜单之间的间隔角度*/
int avgAngle = (90 / (list.size() - 1));
/**第三步,根据间隔角度计算出每个菜单相对于水平线起始位置的真实角度**/
int angle = avgAngle * i;
//Log.d(TAG, "angle=" + angle);
/**
*
* 圆点坐标:(x0,y0)
* 半径:r
* 角度:a0
* 则圆上任一点为:(x1,y1)
* x1 = x0 + r * cos(ao * 3.14 /180 )
* y1 = y0 + r * sin(ao * 3.14 /180 )
*/
/**第四步,根据每个菜单真实角度计算其坐标值**/
point.x = (float) Math.cos(angle * (Math.PI / 180)) * radius1;
point.y = (float) -Math.sin(angle * (Math.PI / 180)) * radius1;
/**第五步,根据坐标执行位移动画**/
/**
* 第一个参数代表要操作的对象
* 第二个参数代表要操作的对象的属性
* 第三个参数代表要操作的对象的属性的起始值
* 第四个参数代表要操作的对象的属性的终止值
*/
ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(list.get(i), "translationX", 0, point.x);
ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(list.get(i), "translationY", 0, point.y);
/**动画集合,用来编排动画**/
AnimatorSet animatorSet = new AnimatorSet();
/**设置动画时长**/
animatorSet.setDuration(500);
/**设置同时播放x方向的位移动画和y方向的位移动画**/
animatorSet.play(objectAnimatorX).with(objectAnimatorY);
/**开始播放动画**/
animatorSet.start();
}
}
}