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

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

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

全部回复(3)
迷茫

你执行完动画后.setVisibility(View.GONE)了么

大家讲道理
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效果,这样看起来自然一些。

刘奇

用属性动画可以改变view本身

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板