Android位移动画translate

移动端开发动画是很重要的一部分,好的动画可以提升用户体验,平时开发中使用比较多的就是简单的位移动画,比如抽屉效果。

xml布局文件




    
    



        
        

        
    


这个文件很简单,就是在一个RelativeLayout布局的页面中,先添加一个灰色背景,然后在背景上添加一个右抽屉效果

在java文件中操作抽屉的显示和小时

    /*显示抽屉*/
    public void showDrawerLayout(){
        this.rightDrawerBackLayout.setVisibility(View.VISIBLE);
        final Animation backAnimation = AnimationUtils.loadAnimation(this,R.anim.drawer_back_anim);
        backAnimation.setFillAfter(true);
        this.rightDrawerBackLayout.startAnimation(backAnimation);
        backAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rightDrawerBackLayout.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        final Animation drawerAnimation = AnimationUtils.loadAnimation(this,R.anim.drawer_animation);
        drawerAnimation.setFillAfter(true);
        this.rightDrawerRecyclerView.startAnimation(drawerAnimation);

        drawerAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rightDrawerRecyclerView.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
    /*隐藏抽屉*/
    public void hiddenDrawerLayout(){
        final Animation backAnimation = AnimationUtils.loadAnimation(this,R.anim.drawer_back_hidden_anima);
        backAnimation.setFillAfter(true);
        this.rightDrawerBackLayout.startAnimation(backAnimation);

        backAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束一定要执行这两个操作,否则显示上有问题
                rightDrawerBackLayout.clearAnimation();
                rightDrawerBackLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        final Animation drawerAnimation = AnimationUtils.loadAnimation(this,R.anim.drawer_hidden_anima);
        drawerAnimation.setFillAfter(true);
        this.rightDrawerRecyclerView.startAnimation(drawerAnimation);

        drawerAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rightDrawerRecyclerView.clearAnimation();;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

这里涉及到四个动画文件,分别为
R.anim.drawer_back_anim



        

R.anim.drawer_animation



    

R.anim.drawer_back_hidden_anima



    

R.anim.drawer_hidden_anima



    

那么怎么生成anim文件呢,步骤为

  1. 选中res右键new->Android resource file
  2. Resource Type选择Animation
  3. Root element选择set
  4. 点击右下角OK即可

参考文章
https://blog.csdn.net/menglong0329/article/details/80637680

你可能感兴趣的:(Android位移动画translate)