2015-04-02 90 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