我曾经使用TranslateAnimation过并在视图中上下滑动.
但是,我意识到,即使在我向下滑动视图并使用View.GONE其可见性后,视图仍然能够接收触摸事件.
您可以通过单击按钮使橙色视图从屏幕底部消失来产生相同的问题.然后,当您单击屏幕底部时,您将意识到仍然会触发自定义视图的触摸事件.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int color = getResources().getColor(android.R.color.holo_orange_light);
// construct the RelativeLayout
final RelativeLayout customView = new RelativeLayout(this) {
@Override
public boolean onTouchEvent(MotionEvent event) {
this.setPressed(true);
Log.i("CHEOK", "OH NO! TOUCH!!!!");
return super.onTouchEvent(event);
}
};
customView.setBackgroundColor(color);
final FrameLayout frameLayout = (FrameLayout)this.findViewById(R.id.frameLayout);
frameLayout.addView(customView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 100, Gravity.BOTTOM));
customView.setVisibility(View.GONE);
Button button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (customView.getVisibility() != View.VISIBLE) {
// Slide up!
TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
anim.setFillAfter(true);
anim.setDuration(200);
Log.i("CHEOK", "VISIBLE!!!");
customView.setVisibility(View.VISIBLE);
customView.setAnimation(anim);
customView.setEnabled(true);
} else {
// Slide down!
TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
anim.setFillAfter(true);
anim.setDuration(200);
// HELPME : Not sure why, after I hide the view by sliding it down,
// making it View.GONE and setEnabled(false), it still able to
// receive touch event.
Log.i("CHEOK", "GONE!!!");
customView.setVisibility(View.GONE);
customView.setAnimation(anim);
customView.setEnabled(false);
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
可以在此处找到演示此问题的完整源代码:
https://www.dropbox.com/s/1101dm885fn5hzq/animator_bug.zip
在我将其滑下之后,如何让我的自定义视图不接收触摸事件?
我不清楚为什么你使用setAnimation()而不是startAnimation()因为你没有设置动画的开始时间.
另一方面,我发现在具有相关动画的情况下将视图设置为GONE并不会使其真正"消失".相反,你必须首先摆脱动画使用clearAnimation().
所以,这样的事情:
public void onClick(View v) {
if (customView.getVisibility() != View.VISIBLE) {
// Slide up!
TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
anim.setFillAfter(true);
anim.setDuration(200);
customView.setVisibility(View.VISIBLE);
customView.setEnabled(true);
customView.startAnimation(anim);
} else {
// Slide down!
TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
anim.setFillAfter(true);
anim.setDuration(200);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
customView.clearAnimation();
customView.setVisibility(View.GONE);
customView.setEnabled(false);
}
@Override
public void onAnimationRepeat(Animation animation) {
// nothing to do
}
@Override
public void onAnimationStart(Animation animation) {
// nothing to do
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不记得,但您可能需要post()的内容onAnimationEnd(),而不是立即运行的代码,使其生效正常.
| 归档时间: |
|
| 查看次数: |
1913 次 |
| 最近记录: |