2011-04-21 46 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