4

I have a RelativeLayout with an ImageView inside it. ImageView is positioned randomly inside a layout. I'm trying to apply translate animation, which will translate the ImageView from it's current position (x,y) to the center of screen (x,y).

I have tried something like code below, but it doesn't do the trick.

<translate 
    android:fromXDelta="0%"
    android:toXDelta="50%"
    android:fromYDelta="0%"
    android:toYDelta="50%"
    android:duration="500"/>

Help appreciated ;)

  • Do you want to translate image from left,right,top,bottom ? Is that so ? – GrIsHu Mar 04 '13 at 08:11
  • Yes that's right. From it's current position, to the center (x-wise and y-wise) –  Mar 04 '13 at 08:17
  • 1
    [link](http://stackoverflow.com/questions/10276251/how-to-animate-a-view-with-translate-animation-in-android) to the same question – gfernandes Mar 04 '13 at 08:36

1 Answers1

0

MY Code: Understand...

Point windowSize = new Point();

    int totalWidth;


    //obtem o tamanho da tela que estamos trabalhando no momento
    if(Build.VERSION.SDK_INT > 12) {

        windowManager.getDefaultDisplay().getSize(windowSize);
        totalWidth = windowSize.x;
    }
    else
        totalWidth = windowManager.getDefaultDisplay().getWidth();

    //vamos animar ao centro
    final int totalWidthFinal = ((totalWidth / 2)  - (int)getDimension(R.dimen.fab_actions_spacing)) * -1;
    mFloatingActionButton.post(new Runnable() {

        @Override
        public void run() {

            int translationX =  totalWidthFinal;


            ViewPropertyAnimator.animate(mFloatingActionButton).setDuration(300).translationX(translationX);
        }
    });
Cícero Moura
  • 2,027
  • 1
  • 24
  • 36