2011-04-21 24 views
1

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

我知道这可能是利用了

AbsoluteLayout.LayoutParams 

params.addRule希望。但请做护理教我

例如:
_ __ _ __ _ __ _ __ _ __ _ __ _ __(首页画面)
| [button]
| -
| -
| -
| -
| -
| -
| -
| >
| [按钮]
_ __ _ __ _ __ _ __ _ __ _ __ _ __(下屏)

回答

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> 

下面是一个使用上面的彼此动画序列(@阿尼姆/ 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" /> 

所以,你可以创建你然后将它们放入您的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; 
} 
+0

谢谢!这应该很有帮助 – 2011-04-21 05:22:39

相关问题