美文网首页
仿老版本优酷Menu

仿老版本优酷Menu

作者: JokerLiang | 来源:发表于2016-09-06 13:47 被阅读0次

效果图:


YoukuMenu.gif

1:写布局文件

2:创建旋转动画工具类

public class rotateAnimation {

    public static int mAnimationNum = 0;
    //旋转出去的动画
    public static void rotateoutAnimation(RelativeLayout layout ,long delay){

        //拿到布局文件中所有的子对象,禁用
        int childCount = layout.getChildCount();
        for (int i = 0 ; i <childCount; i++){
            layout.getChildAt(i).setEnabled(false);
        }
        RotateAnimation ra = new RotateAnimation(
                0f,-180f, //开始角度,结束角度
                Animation.RELATIVE_TO_SELF,0.5f,// 相对的x坐标点(指定旋转中心x值)
                Animation.RELATIVE_TO_SELF,1.0f// 相对的y坐标点(指定旋转中心y值)
        );
        ra.setDuration(500);
        ra.setFillAfter(true);//设置动画停留在结束为止
        ra.setStartOffset(delay);
        ra.setAnimationListener(new MyAnimationListener()); //添加动画事件监听器
        layout.startAnimation(ra);
    }

    //旋转进入的动画
    public static void rotateinAnimation(RelativeLayout layout , long delay){
        //拿到布局文件中所有的子对象,启用
        int childCount = layout.getChildCount();
        for (int i = 0 ; i <childCount; i++){
            layout.getChildAt(i).setEnabled(true);
        }
        RotateAnimation ra = new RotateAnimation(
                -180f,0f, //开始角度,结束角度
                Animation.RELATIVE_TO_SELF,0.5f,// 相对的x坐标点(指定旋转中心x值)
                Animation.RELATIVE_TO_SELF,1.0f// 相对的y坐标点(指定旋转中心y值)
        );
        ra.setDuration(500);
        ra.setFillAfter(true);//设置动画停留在结束为止
        ra.setStartOffset(delay); //设置动画延时
        ra.setAnimationListener(new MyAnimationListener()); //添加动画事件监听器
        layout.startAnimation(ra);
    }

    //添加动画监听器

    static class MyAnimationListener implements Animation.AnimationListener{

        @Override
        public void onAnimationStart(Animation animation) {
            mAnimationNum ++;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mAnimationNum --;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}

3:

public class MainActivity extends AppCompatActivity {

    private ImageButton icon_home;
    private ImageButton icon_menu;
    private RelativeLayout level1;
    private RelativeLayout level2;
    private RelativeLayout level3;
    private Boolean level3Animation = true ;
    private Boolean level2Animation = true ;
    private Boolean level1Animation = true ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initui();
        initdata();
    }

    private void initui() {
        icon_home = (ImageButton) findViewById(R.id.icon_home);
        icon_menu = (ImageButton) findViewById(R.id.icon_menu);
        level1 = (RelativeLayout) findViewById(R.id.level1);
        level2 = (RelativeLayout) findViewById(R.id.level2);
        level3 = (RelativeLayout) findViewById(R.id.level3);
    }

    private void initdata() {
        icon_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rotateAnimation.mAnimationNum >0){
                    //说明正在有动画进行
                    return;
                }else {
                    if (level3Animation){
                        //执行出去的动画
                        rotateAnimation.rotateoutAnimation(level3,0);
                        level3Animation = false ;
                    }else {
                        //执行进来的动画
                        rotateAnimation.rotateinAnimation(level3,0);
                        level3Animation = true ;
                    }
                }

            }
        });

        icon_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rotateAnimation.mAnimationNum>0){
                    return;
                }else {
                    if (level2Animation){
                        //执行出去的动画
                        if (level3Animation){
                            rotateAnimation.rotateoutAnimation(level3,0);
                            rotateAnimation.rotateoutAnimation(level2,500);
                            level2Animation = false ;
                            level3Animation = false ;
                        }else {
                            rotateAnimation.rotateoutAnimation(level2,0);
                            level2Animation = false ;
                        }
                    }else {
                        //执行进来的动画
                        rotateAnimation.rotateinAnimation(level2,0);
                        level2Animation = true ;
                    }
                }
            }
        });
    }

    /*
    * 监听按钮
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //keyCode 事件码
        long delay = 0 ;
        if (keyCode==KeyEvent.KEYCODE_MENU){
            if (level1Animation){
                //一个个转出去
                if (level3Animation){
                    rotateAnimation.rotateoutAnimation(level3,delay);
                    level3Animation = false ;
                    delay += 200 ;
                }
                if (level2Animation){
                    rotateAnimation.rotateoutAnimation(level2,delay);
                    level2Animation = false ;
                    delay += 200;
                }
                rotateAnimation.rotateoutAnimation(level1,delay);
            }else {
                //依次转进来
                rotateAnimation.rotateinAnimation(level1,0);
                rotateAnimation.rotateinAnimation(level2,200);
                level2Animation = true;
                rotateAnimation.rotateinAnimation(level3,400);
                level3Animation = true;
            }
            level1Animation = !level1Animation;
            return true ; // 表示消费了当前时间 ,MENU键不会被其他事件消费
        }
        return super.onKeyDown(keyCode, event);
    }
}

相关文章

网友评论

      本文标题:仿老版本优酷Menu

      本文链接:https://www.haomeiwen.com/subject/akudettx.html