Maison > Questions et réponses > le corps du texte
怎么能让下面的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();
}
大家讲道理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效果,这样看起来自然一些。