1

So for the project I'm working on I need to be able to move widgets up the screen during runtime (like a reverse waterfall). Could someone please show an example of how to move a button from one position of the screen to a higher position (using an absolute layout) during runtime?

I know it probably makes use of

 AbsoluteLayout.LayoutParams

or params.addRule hopefully. But please do care to teach me

For Example:
_____________________(Top Of Screen)
| [button]
| -
| -
| -
| -
| -
| -
| -
| >
| [button]
_____________________(Bottom Of Screen)

shoconinja
  • 195
  • 1
  • 3
  • 11

1 Answers1

2

From http://developerlife.com/tutorials/?p=343

Here’s a slide-from-left animation (translate from right to left across the width of the view), named “/res/anim/slide_right.xml”:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="150" />
</set>

Here’s another animation sequence that uses the one above (@anim/slide_right.xml -> “/res/anim/slide_right.xml”):

<?xml version="1.0" encoding="utf-8"?>

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="10%"
        android:order="reverse"
        android:animation="@anim/slide_right" />

So you can create your sequences in XML and put them in the “/res/anim/some_file.xml” of your Android project resources. You can get more details on how to create this XML file here.

You can also do this by code::

  AnimationSet set = new AnimationSet(true);

  Animation animation = new AlphaAnimation(0.0f, 1.0f);
  animation.setDuration(100);
  set.addAnimation(animation);

  animation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
      Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
  );
  animation.setDuration(500);
  set.addAnimation(animation);

  LayoutAnimationController controller =
      new LayoutAnimationController(set, 0.25f);
  button.setLayoutAnimation(controller);

and then:

public static Animation runSlideAnimationOn(Activity ctx, View target) {
  Animation animation = AnimationUtils.loadAnimation(ctx,
                                                     android.R.anim.slide_right);
  target.startAnimation(animation);
  return animation;
}
Waza_Be
  • 39,407
  • 49
  • 186
  • 260