1

因此,对于我正在处理的项目,我需要能够在运行时将小部件向上移动(如反向瀑布)。有人可以举例说明如何在运行时将按钮从屏幕的一个位置移动到更高的位置(使用绝对布局)吗?

我知道它可能利用

 AbsoluteLayout.LayoutParams

params.addRule 希望。但请注意教我

例如:
_ __ _ __ _ __ _ __ _ __ _ __ _ __(屏幕顶部)
| [按钮]
| -
| -
| -
| -
| -
| -
| -
| >
| [按钮]
_ __ _ __ _ __ _ __ _ __ _ __ _ __(屏幕底部)

4

1 回答 1

2

来自http://developerlife.com/tutorials/?p=343

这是一个从左滑动的动画(在视图宽度上从右向左平移),名为“/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>

这是另一个使用上述动画序列的动画序列(@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" />

因此,您可以在 XML 中创建序列并将它们放在您的 Android 项目资源的“/res/anim/some_file.xml”中。您可以在此处获取有关如何创建此 XML 文件的更多详细信息。

您也可以通过代码执行此操作::

  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);

进而:

public static Animation runSlideAnimationOn(Activity ctx, View target) {
  Animation animation = AnimationUtils.loadAnimation(ctx,
                                                     android.R.anim.slide_right);
  target.startAnimation(animation);
  return animation;
}
于 2011-04-21T05:16:27.193 回答