android - 使用动画隐藏view后,原来的位置空白
天蓬老师
天蓬老师 2017-04-17 17:21:49
0
3
530

怎么能让下面的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();
    }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
迷茫

Have you setVisibility(View.GONE) after executing the animation

大家讲道理
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) {

            }
        });
    }
}

By the way, I added an alpha effect to the view that needs to be hidden, so that it looks more natural.

刘奇

Use attribute animation to change the view itself

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template