【问题标题】:Android Absolute Layout/Parameters (Moving Widgets During Runtime)Android 绝对布局/参数(在运行时移动小部件)
【发布时间】:2011-04-21 13:16:27
【问题描述】:

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

我知道它可能会利用

 AbsoluteLayout.LayoutParams

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

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

【问题讨论】:

    标签: android runtime position absolutelayout layoutparams


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2010-10-07
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多