2015-04-02 54 views
0

我想使用Property Animation來演示流星動畫,但失敗了。我的演示很簡單: 1.創建一個具有啓動按鈕的MainActivity。 2.單擊開始按鈕以啓動MeteorActivity 3. MeteorActivity具有相對佈局內容視圖,其中包含位於佈局中間/中間的簡單星形ImageView。 4.在MeteorActivity的onResume方法中,我啓動屬性動畫以將ImageView的「top」屬性從0更改爲1000. 問題在於,星星從上邊緣向下滑動直到其到達原始位置佈局)。它消失了!任何人都可以幫忙嗎?爲什麼ImageView在動畫中消失?

MeteorActivity:

public class MeteorActivity extends Activity implements ValueAnimator.AnimatorUpdateListener{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_meteor); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    startMeteor(); 
} 

private void startMeteor() { 
    ImageView imageView = (ImageView) findViewById(R.id.starImageView); 
    ObjectAnimator anim = ObjectAnimator.ofInt(imageView, "top", 0, 1000); 
    anim.setDuration(10000); 
    anim.addUpdateListener(this); 
    anim.start(); 
} 

@Override 
public void onAnimationUpdate(ValueAnimator valueAnimator){ 
    ImageView imageView = (ImageView) findViewById(R.id.starImageView); 
    Log.d("ImageView:Height:", Integer.valueOf(imageView.getTop()).toString()); 
} 

}

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/starImageView" 
    android:src="@drawable/meteor" 
    android:layout_centerHorizontal="true"/> 

+0

你能分享佈局xml代碼嗎? – 2015-04-02 05:40:34

+0

佈局xml補充,謝謝, – iceagle 2015-04-02 06:22:45

回答

1

嗨,請與下面我發佈更新您的佈局,讓我們試試這個

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/starImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

還有一點,當你需要停止動畫讓我知道。

謝謝。

+0

謝謝,現在它不再消失,但它開始從原來的位置下降。我希望它從0下降。你能再次幫忙嗎? – iceagle 2015-04-02 12:17:15

+0

嘗試了幾次修改,但失敗了。強烈懷疑財產動畫是否能夠做到這一點。也許我可以用手動onDraw重新繪製更好。 – iceagle 2015-04-02 12:38:20