views:

273

answers:

4

I am trying to swap the position of two buttons. my swapping code looks as below.

private void exchangeButtons(Button btn1, Button btn2) { // Create the animation set

    AnimationSet exchangeAnimation = new AnimationSet(true);
    TranslateAnimation translate = new TranslateAnimation( Animation.RELATIVE_TO_SELF, btn2.getLeft(),
                  Animation.RELATIVE_TO_SELF, btn1.getLeft(),
                  Animation.RELATIVE_TO_SELF, btn2.getRight(),
                  Animation.RELATIVE_TO_SELF, btn1.getRight()
    );
    translate.setDuration(500);
    exchangeAnimation.addAnimation(translate);

// int fromX = btn1.getLeft(); // int fromY = btn1.getRight(); // int toX = btn2.getLeft(); // int toY = btn2.getRight();

    Log.d("ArrangeMe", "view1 pos:" + btn1.getLeft() + ", " +btn1.getRight() + "view2 pos:" + btn2.getLeft() + ", " + btn2.getRight());

    AnimationSet exchangeAnimation1 = new AnimationSet(true);
    TranslateAnimation translate1 = new TranslateAnimation( Animation.RELATIVE_TO_SELF, btn1.getLeft(),
                  Animation.RELATIVE_TO_SELF, btn2.getLeft(),
                  Animation.RELATIVE_TO_SELF, btn1.getRight(),
                  Animation.RELATIVE_TO_SELF, btn2.getRight()
    );
 translate1.setDuration(500);
 exchangeAnimation1.addAnimation(translate1);

 // EXECUTE
 btn1.startAnimation(exchangeAnimation);
 btn2.startAnimation(exchangeAnimation1);

}

I call the code as below

exchangeButtons(button1, button2);

my layout looks as below

what happens when i execute the code is,

instead of the buttons exchanging their positions, they just disappear for sometime[may be 500 ms] and reappear as they were originally.

how to resolve this problem ? will it work properly in device ?

regards

+1  A: 

Android's TranslateAnimation doesn't reposition your components, it just animates the transition you want to do - after that, it's up to you to change the layout params of your component (take a look at this post).

P.S. You can use directly the translate animation, without using a whole new animation set. Another good thing will be to create the animations when your layout is inflated and to override onSizeChanged(int, int, int, int) to re-create them using the new dimensions.

What's the layout you are using?

Dimitar Dimitrov
A: 

Thanks,

I am using linearlayout inside main layout

secondly, if i have buttons arranged in more than one row [say 4 x 4] can i animate button moving from one row to another row [ basically i wanna randomize, and suffling animation kinda thing] ?

regards

Jagat
A: 

I tried to implement reading from this post, but it seems the ABSoluteLayout is deprecated. How to make it work in latest sdk ?

regards

Jagat
A: 

I am still stuck ! googlegroups did not reply either

I changed the code to private void exchangeButtons(final Button btn1, final Button btn2) { // Create the animation set

    // AnimationSet exchangeAnimation = new AnimationSet(true);

// TranslateAnimation translate = new TranslateAnimation( Animation.ABSOLUTE, btn1.getLeft(), // Animation.ABSOLUTE, btn2.getLeft(), // Animation.ABSOLUTE, btn1.getRight(), // Animation.ABSOLUTE, btn2.getRight() // ); TranslateAnimation translate = new TranslateAnimation(0, 0, btn2.getLeft() - btn1.getLeft(), btn2.getRight() - btn1.getRight()); translate.setDuration(500); //exchangeAnimation.addAnimation(translate); // int fromX = btn1.getLeft(); // int fromY = btn1.getRight(); // int toX = btn2.getLeft(); // int toY = btn2.getRight();

    Log.d("ArrangeMe", "view1 pos:" + btn1.getLeft() + ", " +btn1.getRight() + "view2 pos:" + btn2.getLeft() + ", " + btn2.getRight());

    // AnimationSet exchangeAnimation1 = new AnimationSet(true);

// TranslateAnimation translate1 = new TranslateAnimation( Animation.ABSOLUTE, btn2.getLeft(), // Animation.ABSOLUTE, btn1.getLeft(), // Animation.ABSOLUTE, btn2.getRight(), // Animation.ABSOLUTE, btn1.getRight() // ); TranslateAnimation translate1 = new TranslateAnimation(0, 0, btn1.getLeft() - btn2.getLeft(), btn1.getRight() - btn2.getRight()); translate1.setDuration(500); // exchangeAnimation1.addAnimation(translate1);

 //btn2.setLayoutParams(new LayoutParams());
 //defining a listener
 translate1.setAnimationListener(new AnimationListener(){

            public void onAnimationEnd(Animation animation) {

                   // !!!!!! Without this thread or just a call to its content the view goes to its ancient coordinates after animation
                    Thread th = new Thread(new Runnable(){
                            public void run() {
                                    btn2.setLayoutParams(new AbsoluteLayout.LayoutParams(btn2.getLayoutParams().width,btn2.getLayoutParams().height, btn1.getLeft(), btn1.getRight()));
                            }
                    });
                    th.run();
            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationStart(Animation animation) {
            }
    });

 // EXECUTE
 btn1.startAnimation(translate/*exchangeAnimation*/);
 btn2.startAnimation(translate1/*exchangeAnimation1*/);

}

but it crashes in the thread's run method

any help is appreciated

Jagat