public class MainActivity extends Activity implements View.OnClickListener{
private int imageRes[] = new int[]{
R.id.iv_a,
R.id.iv_b,
R.id.iv_c,
R.id.iv_d,
R.id.iv_e
};
private boolean isShow;
private MediaPlayer mMediaPlayer;
private List<ImageView> imageViewList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
//创建MediaPlayer来播放声音
mMediaPlayer = MediaPlayer.create(this,R.raw.voice_pink05);
for(int i = 0;i < imageRes.length;i++){
ImageView imageView = (ImageView) findViewById(imageRes[i]);
imageView.setOnClickListener(this);
imageViewList.add(imageView);
}
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.iv_a:
if(!isShow){
startAnim();
isShow = true;
} else {
closeAnim();
isShow = false;
}
break;
default:
Toast.makeText(MainActivity.this,v.getId()+"",Toast.LENGTH_SHORT).show();;
break;
}
}
/**
* 关闭动画
*/
private void closeAnim() {
btnReverseRotate();
for (int i = 1; i < imageViewList.size(); i++) {
ObjectAnimator anim = ObjectAnimator.ofFloat(imageViewList.get(i),
"translationX",-i*100,0F);
anim.setDuration(500);
anim.setStartDelay(200);
anim.setInterpolator(new DecelerateInterpolator());
anim.start();
}
}
/**
* 按钮正向旋转
*/
private void btnPositiveRotate(){
RotateAnimation mRotateAnimation = new RotateAnimation(0F,45F,50,50);
mRotateAnimation.setFillAfter(true);
mRotateAnimation.setDuration(500);
imageViewList.get(0).startAnimation(mRotateAnimation);
}
/**
* 按钮反向旋转
*/
private void btnReverseRotate(){
RotateAnimation mRotateAnimation = new RotateAnimation(45F,0F,50,50);
mRotateAnimation.setFillAfter(true);
mRotateAnimation.setDuration(500);
imageViewList.get(0).startAnimation(mRotateAnimation);
}
/**
* 单击按钮播放声音
*/
private void playSound() {
mMediaPlayer.start();
}
/**
* 菜单展开动画
*/
private void startAnim() {
playSound();
btnPositiveRotate();
for (int i = 1; i < imageViewList.size(); i++) {
ObjectAnimator anim = ObjectAnimator.ofFloat(
imageViewList.get(i),"translationX",0F,-i * 100);
anim.setDuration(500);
anim.setStartDelay(200);
anim.setInterpolator(new BounceInterpolator());
anim.start();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.release();//释放资源
}
}
activity_main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="co.huiqu.animationtest.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|right"
>
<ImageView
android:id="@+id/iv_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/b"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="100dp"
/>
<ImageView
android:id="@+id/iv_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/c"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="100dp"
/>
<ImageView
android:id="@+id/iv_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/d"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="100dp"
/>
<ImageView
android:id="@+id/iv_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/e"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="100dp"
/>
<ImageView
android:id="@+id/iv_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="100dp"
/>
</FrameLayout>
</LinearLayout>