搜尋

首頁  >  問答  >  主體

android - 使用动画隐藏view后,原来的位置空白

怎么能让下面的view顶上来,LinearLayout布局,使用mTopView.setVisibility(View.GONE)下面的布局可以自动上移,为什么动画不可以?

private void hideViews() {
        mTopView.setTranslationY(0);
        mTopView.animate().translationY(-mTopView.getHeight())
                .setInterpolator(new AccelerateInterpolator(2)).setStartDelay(50).setDuration(500).start();
    }
天蓬老师天蓬老师2804 天前541

全部回覆(3)我來回復

  • 迷茫

    迷茫2017-04-17 17:23:49

    你執行完動畫後.setVisibility(View.GONE)了麼

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:23:49

    public class MainActivity extends AppCompatActivity {
        ImageView view1,view2;//linearLayout当中竖直排列的两个imageView
        AlphaAnimation animationOut,animationIn;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            view1 = (ImageView) findViewById(R.id.view_1);
            view2 = (ImageView) findViewById(R.id.view_2);
            initAnimation();
            view1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                       ObjectAnimator.ofInt(view2,"top",view2.getTop()-view1.getHeight()).setDuration(500).start();
                       ObjectAnimator.ofInt(view2,"bottom",view2.getBottom()-view1.getHeight()).setDuration(500).start();
                       view1.startAnimation(animationOut);
                }
            });
            view2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(view1.getVisibility() == View.GONE){
                        ObjectAnimator.ofInt(view2,"top",view2.getTop()+view1.getHeight()).setDuration(500).start();
                        ObjectAnimator.ofInt(view2,"bottom",view2.getBottom()+view1.getHeight()).setDuration(500).start();
                        view1.startAnimation(animationIn);
    
                    }
                }
            });
        }
        private void setClickable(boolean which){
            view1.setClickable(which);
            view2.setClickable(which);
        }
        private void initAnimation(){
            animationOut = new AlphaAnimation(1f,0f);
            animationOut.setDuration(500);
            animationOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    setClickable(false);
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    view1.setVisibility(View.GONE);
                    setClickable(true);
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
            //
            animationIn = new AlphaAnimation(0f,1f);
            animationIn.setDuration(500);
            animationIn.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    setClickable(false);
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    view1.setVisibility(View.VISIBLE);
                    setClickable(true);
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
        }
    }
    

    順便為需要隱藏的view加上了個alpha效果,這樣看起來自然一點。

    回覆
    0
  • 怪我咯

    怪我咯2017-04-17 17:23:49

    用屬性動畫可以改變view本身

    回覆
    0
  • 取消回覆