Question

I have an ImageButton with TranslateX Animation Right to Left like Merquee, So it animated right to left. Now when i click on that nothing happened. Actually click is not perform and click only perform on real position of imagebutton.

what to do any suggestion? greatly appriciate... Thanks

Was it helpful?

Solution

Use ObjectAnimator(For later version then Honeycomb) for Animatinfg Your Objects, You can use follwing code for references:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout1);
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    ObjectAnimator mover = ObjectAnimator.ofFloat(layout, "translationX",
            width, -width);
    mover.setDuration(10000);
    mover.setRepeatMode(Animation.INFINITE);
    mover.setRepeatCount(Animation.INFINITE);
    mover.start();

If you Are Using Api lower Then the HoneyComb(Like Gingerbread) then Use this Library: http://nineoldandroids.com/

It will Working As its Working in my devices.

OTHER TIPS

As of now, the Animations which alter a View's matrix Only change the co-ordinates where the view is drawn, and not the actual location of View in Layout. So, its just a canvas transform when onDraw() of that View is being called.

So, you can setTranslationX(100) and view will be drawn a 100 pixels to right. But, the click area (getHitRect()) is still on the same place which was assigned to view on layout pass.

Or, you can actually place the view where it should be after animation, and run the animation in reverse.

If you want to actually alter the layout, you will have to alter that view's LayoutParams and change width/height/margin on it. Then you will have to requestLayout() on each frame of animation.

Example : This will animate left margin of a view inside a FrameLayout:

  //---assuming animated view is a child of Framelayout---
   FrameLayout parent;
   View animatedChild;

    ValueAnimator animator = new ValueAnimator();
    animator.setFloatValues(0,parent.getWidth()); //--slide out to right--
    animator.setDuration(1000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            FrameLayout.LayoutParams params = animatedChild.getLayoutParams();
            Float margin = (Float) valueAnimator.getAnimatedValue();
            params.leftMargin = margin.intValue();
            animatedChild.requestLayout();
        }
    });

    animator.start();

I run into similar problems before when developing my android app. And I found when animation is running, it is actually making the image layer is flowing around. So you need to click on the original position of the button, which means you may need to click on a black space if the button has started moving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top