我正在做自定义搜索栏,如WhatsApp,谷歌应用程序。我正在尝试添加动画,同时显示和关闭视图。我添加动画XML文件来做这些事情。
假设,如果用户按下搜索图标,它将显示带有动画的搜索栏。如果用户按下搜索栏中的关闭图标,它将关闭带有动画的搜索栏。
动画只在第一次发生。第二次没有动画,就像我按下搜索图标一样。
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener{
ImageButton ibSearch;
EditText etSearch;
ImageButton ibBack,ibClose;
CardView cardView;
ObjectAnimator objectanimator;
RelativeLayout search;
Animation animOpenView,animCloseView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ibSearch = (ImageButton)findViewById(R.id.search);
cardView = (CardView) findViewById(R.id.searchBarC);
ibClose = (ImageButton) findViewById(R.id.close);
search = (RelativeLayout) findViewById(R.id.searchLayout);
animOpenView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_left);
animCloseView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_right);
animCloseView.setAnimationListener(this);
animOpenView.setAnimationListener(this);
ibSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardView.setVisibility(View.VISIBLE);
cardView.setAnimation(animOpenView);
}
});
ibClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardView.setVisibility(View.GONE);
cardView.setAnimation(animCloseView);
}
});
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
任何建议都将不胜感激。
答案 0 :(得分:0)
这是因为您的观点已消失
在动画开始之前,您的视图将离开屏幕..
所以欣赏动画只需删除该行..
cardView.setVisibility(View.GONE); // remove this..
cardView.setAnimation(animCloseView);
答案 1 :(得分:0)
在您的活动onAnimationEnd
:
@Override
public void onAnimationEnd(Animation animation) {
cardView.clearAnimation();
}
答案 2 :(得分:0)
问题已解决。实际上,问题是,animCloseView和animOpenView在第一次之后变为空/非空。所以我重新启动了动画。
ibSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardView.setVisibility(View.VISIBLE);
animOpenView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_left); //just added this line
cardView.setAnimation(animOpenView);
}
});
ibClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardView.setVisibility(View.GONE);
animCloseView = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.anim_right); //just added this line
cardView.setAnimation(animCloseView);
}
});
}