Unable to fully view GONE after using TranslateAnimation

I used TranslateAnimation and moved up and down in kind.

However, I understand, although after I shift the view and use View.GONE in its visibility, the view is still able to receive a touch event.

You can create the same problem by pressing the button so that the orange color view disappears at the bottom of the screen. Then, when you click on the bottom of the screen, you will realize that the touch event of the user view is still triggered.

 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); } } }); } } 

The full source code to demonstrate this problem can be found here:

https://www.dropbox.com/s/1101dm885fn5hzq/animator_bug.zip

How can I make my custom view so as not to receive a touch event after I slide it down?

+1
source share
1 answer

I donโ€™t understand why you are using setAnimation() instead of startAnimation() , because it doesnโ€™t show that you set the start time of the animation.

In another note, I found that setting the GONE view when it has an associated animation does not really make it โ€œGONEโ€. Instead, you should get rid of the animation first using clearAnimation() .

So something like this:

 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 } } } } 

I donโ€™t remember, but you may need to post() contents of onAnimationEnd() instead of running the code immediately to use it effectively.

+5
source

All Articles