2011-06-15 50 views
0

我有一個觀點的佈局是這樣的:安卓:放大/縮小查看隨着時間的推移

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="@color/light_gray" 
    android:padding="5dip"> 

    <View android:id="@+id/fixedSpace" android:layout_width="fill_parent" 
     android:layout_height="50dip" android:background="@color/aqua" 
     android:layout_alignParentBottom="true" android:clickable="true" 
     android:onClick="onClickStartAnimation" /> 

    <View android:id="@+id/dynamicSpace" android:layout_width="fill_parent" 
     android:layout_height="200dip" android:background="@color/lime" 
     android:layout_above="@id/fixedSpace" /> 


    <View android:id="@+id/remainingSpace" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:background="@color/pink" 
     android:layout_alignParentTop="true" android:layout_above="@id/dynamicSpace" /> 


</RelativeLayout> 

我想實現基本上是一個放大/縮小的dynamicSpace行爲在時間。與動畫我可以產生以下:

t = 1時:

t=1

t = 2時:

t=2

T = 3:

t=3

但是,這並沒有真正調整我的意見,特別是dynamicSpaceremainingSpace。它只是動畫dynamicSpace移動的視圖。但「容器」視圖從一開始就佔用了空間。

正確的是石灰色dynamicSpace以0px開頭,粉紅色的remainingSpace接管,因此中間沒有灰色空間。

回答

0

Scale the View

既然你說你正在做它隨着時間的推移,這聽起來像一個LinearInterpolator是最好的。

+0

我確實使用過動畫。但是,這並不能解決真正增長或縮小實際觀點的問題。它只是爲它製作動畫。將在我的問題中提供更多信息。 – znq 2011-06-16 09:35:00

+0

爲什麼你不能在動畫之後重新調整視圖的大小(因爲你知道你有多大的動畫效果)? – hwrdprkns 2011-06-16 12:41:20

+0

這不起作用,因爲這是一個調整大小的步驟。但是我真正想要的是隨着時間的推移調整大小。動畫並沒有真正調整任何東西(他們看起來像是這樣)。 – znq 2011-06-16 12:44:09

0

編輯: 我試着用AsyncTask線替換下面的線程,它更光滑。我認爲關鍵是我一直的線程在後臺運行,只是使用它時,我想調整的東西,從而減少開銷


創建自定義AnimationListener並把代碼在onAnimationRepeat方法調整視圖。

然後做一個虛擬動畫,並將動畫重複設置爲無限。當視圖已經達成了動畫的最終大小,設置重複計數爲零(再次onAnimationRepeat):

class ResizeAnimationListener implements AnimationListener{ 

int finalHeight; // max Height 
int resizeAmount; // amount to resize each time 
View view; // view to resize 

public ResizeAnimationListener(int finalHeight; View view, int resizeAmount) { 
    super(); 
    finalHeight; = finalHeight; 
    this.resizeAmount = resizeAmount; 
    this.view = view; 

} 

@Override 
public void onAnimationEnd(Animation animation) { 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 

    int newHeight; 
    int currentHeight; 

    current = view.getMeasuredHeight(); 

    newHeight= currentHeight+ resizeAmount; 
    if(newHeight> finalHeight){ 
     // check if reached final height 

     // set new height to the final height 
     newHeight = finalHeight; 
     // set repeat count to zero so we don't have any more repeats 
     anim.setRepeatCount(0); 

    } 

    // set new height 
    LayoutParams params = view.getLayoutParams();           
    params.height = newHeight; 
    v.setLayoutParams(params);          

} 

@Override 
public void onAnimationStart(Animation animation) { 

} 

}; 

class DummyAnimation extends Animation{} 


float frameRate = 1000/30;        
DummyAnimation anim = new DummyAnimation(); 
anim.setDuration((long)frameRate);       
anim.setRepeatCount(Animation.INFINITE); 
ResizeAnimationListener animListener = new ResizeAnimationListener(((View)view.getParent()).getHeight(), view, 25); 
anim.setAnimationListener(animListener); 

view.startAnimation(anim); 

我做了我自己的應用程序這項工作。然而,我調整視圖的視圖(當我調整視圖的大小時,因此在屏幕上移動)似乎失靈了。可能與反覆調整大小有關,而不是其他任何事情,但只是一個警告。也許別人知道爲什麼?

相關問題