Button not responding to Click Event after Translation animation

前端 未结 7 1102
执笔经年
执笔经年 2020-12-01 21:10

I have performed a Translation animation on button ,everything work as i excepted ,but only the problem is after the animation the button not responding to click event pleas

相关标签:
7条回答
  • 2020-12-01 21:37

    Try to use this:

    b.layout(0, -40, b.getWidth(), b.getHeight());
    
    0 讨论(0)
  • 2020-12-01 21:41

    I could achieve what you wanted but you will have manage co-ordinates manually. See if it works for you.

    public class AnimationTestActivity extends Activity {
    
        private Button mButton;
    
        private LinearLayout.LayoutParams params;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mButton = (Button) findViewById(R.id.button);
            mButton.setOnClickListener(mClickListener);
    
            params = (LayoutParams) mButton.getLayoutParams();
        }
    
        private android.view.View.OnClickListener mClickListener = new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 400);
                animation.setDuration(2000);
                animation.setAnimationListener(mAnimationListener);
                v.startAnimation(animation);
            }
        };
    
        private AnimationListener mAnimationListener = new AnimationListener() {
    
            @Override
            public void onAnimationStart(Animation animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                params.topMargin = params.topMargin + 400;
                mButton.setLayoutParams(params);
            }
        };
    }
    
    0 讨论(0)
  • 2020-12-01 21:43

    The animation do not change the View actually Position. even if you setfillAfter(true), The position it can accept click event is the original position.

    0 讨论(0)
  • 2020-12-01 21:45

    I faced this issue and I did fix it just some second ago. So, I think that I should share my solution with you guys.

    • In animation xml file,I removed android:fillAfter="true" when keep android:fillEnabled="true".

    • Register Animation listener, then in onAnimationEnd() method, I call View#Layout() to change the position of the view.

                      int newLeft = (int) (layoutContent.getLeft() + layoutContent.getWidth() * 0.8);
                      layoutContent.layout(newLeft, 
                                          layoutContent.getTop(), 
                                          newLeft + layoutContent.getMeasuredWidth(), 
                                          layoutContent.getTop() + layoutContent.getMeasuredHeight());
      

    In my case, what the animation do is that slides the layoutContent to leftside 80% of width.

    It works fine. Hope this helps.

    @Update: Today, you can use ObjectAnimator on android 3.0 +. If you are developing for android under 3.0, you can find it at support library v.4. ObjectAnimator is bester for animation.

    @Update#2: You can use ViewPropertyAnimator on android api higher 12 version. It provides better performance, and fix problem with click events. Example:

    mButton.animate()
                    .setDuration(TIME)
                    .translationY(VALUE)
                    .start();
    
    0 讨论(0)
  • 2020-12-01 21:46

    This will translate the object in y direction:

    ObjectAnimator mover = ObjectAnimator.ofFloat(v, "translationY", 0, 400);
    mover.start();
    
    0 讨论(0)
  • 2020-12-01 21:49

    If you are handling animations at lower level API (below HoneyComb) your animation actually does not moves the button but only its images.Its click will be at same place where you have placed it in your layout.

    0 讨论(0)
提交回复
热议问题