在之前的优酷app中国有个这样的自定义控件动画,如下图:
旋转中心是三个半圆所公共的圆心!!
在这样一个RelativeLayout布局中 有三层菜单,由最小的方形到最大的方形,三个相对布局组成;其中的各个菜单按钮都是通过相对布局搭配到对应半圆图层的~;
创建一个AnimationUtil 类,作为自定义控件的动画方法类,在主活动中进行对应调用;
public class AnimUtil {
public static int animCount = 0;//当前执行动画数量
public static void closeMenu(RelativeLayout rl,int startOffset)//关闭菜单方法
{
//取得RelativeLayout的子view
for (int i = 0; i < rl.getChildCount(); i++) {
rl.getChildAt(i).setEnabled(false);//设置不可触摸
}
//pivotXType, 现对于自身,相对于
//pivotXValue, pivotYValue 相对于宽度和高度的0.几?
// RotateAnimation animation1 = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
RotateAnimation animation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1);
animation.setDuration(1000);//这就是 旋转中心
animation.setFillAfter(true);
animation.setStartOffset(startOffset);
animation.setAnimationListener(new MyanimationListener());
rl.startAnimation(animation);
}
public static void showMenu(RelativeLayout rl,int startOffset)//打开菜单方法
{
for (int i = 0; i < rl.getChildCount(); i++) {
rl.getChildAt(i).setEnabled(true);//设置不可触摸
}
RotateAnimation animation = new RotateAnimation(-180, -0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1);
animation.setDuration(1000);
animation.setFillAfter(true);
animation.setStartOffset(startOffset);
animation.setAnimationListener(new MyanimationListener());
rl.startAnimation(animation);
}
static class MyanimationListener implements AnimationListener{
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
animCount++;
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
animCount--;
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
}
MainActivity中的具体代码:
public class MainActivity extends Activity implements OnClickListener {
/**
* view旋转了180度,如何判断是逆时针
* 基于自身底侧中心的点
*/
private ImageView iv_home;
private ImageView iv_menu;
private String tag = MainActivity.class.getSimpleName();//类名
private boolean isShowLevel2 = true;//是否选择二级菜单
private boolean isShowLevel3 = true;
private boolean isShowMenu = true;//包括1,2,3级菜单
private RelativeLayout level1,level2,level3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListener();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_MENU) {
if (isShowMenu) {
int startOffset = 0;
//需要关闭所有的菜单
if (isShowLevel3) {
AnimUtil.closeMenu(level3, startOffset);
isShowLevel3 = false;
startOffset+=200;
}if (isShowLevel2) {
AnimUtil.closeMenu(level2, startOffset);
isShowLevel2 = false;
startOffset+=200;
}
AnimUtil.closeMenu(level1, startOffset);
}else {
//显示所有的菜单
AnimUtil.showMenu(level1,0);
AnimUtil.showMenu(level2,200);
isShowLevel2 = true;
AnimUtil.showMenu(level3, 400);
isShowLevel3 = true;
}
isShowMenu=!isShowMenu;
return true;
}
return super.onKeyDown(keyCode, event);
}
private void initListener() {
// TODO Auto-generated method stub
iv_home.setOnClickListener(this);
iv_menu.setOnClickListener(this);
}
private void initViews() {
// TODO Auto-generated method stub
iv_home = (ImageView) findViewById(R.id.iv_home);
iv_menu = (ImageView) findViewById(R.id.iv_menu);
level1 = (RelativeLayout) findViewById(R.id.level1);
level2 = (RelativeLayout) findViewById(R.id.leve2);
level3 = (RelativeLayout) findViewById(R.id.level_3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iv_home:
if (AnimUtil.animCount!=0) {
//有动画在执行
return;
}
if (isShowLevel2) {
//隐藏该view
int startOffset = 0;
if (isShowLevel3) {
AnimUtil.closeMenu(level3,startOffset);
isShowLevel3 = false;
startOffset+=200;
}
Log.e(tag, "执行隐藏操作");
AnimUtil.closeMenu(level2,startOffset);
}else {
//需要显示
AnimUtil.showMenu(level2,0);
Log.e(tag,"执行显示操作");
}
isShowLevel2 = !isShowLevel2;
break;
case R.id.iv_menu:
if (AnimUtil.animCount!=0) {
//有动画在执行
return;
}
if (isShowLevel3) {
AnimUtil.closeMenu(level3,0);
isShowLevel3 = false;
}else {
AnimUtil.showMenu(level3,0);
isShowLevel3 = true;
}
break;
default:
break;
}
}
}