Android 绝对布局/参数(在运行时移动小部件)

发布于 2024-11-02 22:09:33 字数 916 浏览 1 评论 0原文

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

我知道它可能希望使用

 AbsoluteLayout.LayoutParams

params.addRule 。但请务必教我

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆消瘦 2024-11-09 22:09:33

来自 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文