onCreate Animation无法正常工作

时间:2015-02-28 15:05:18

标签: java android

我的主屏幕上有一个动画,它会在应用程序启动时显示。

我在onCreate方法中添加了一个动画。

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button exit = (Button)findViewById(R.id.exit);
    Animation animButtonEnter = AnimationUtils.loadAnimation(this, R.anim.button_enter);
    exit.startAnimation(animButtonEnter);
}

button_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">

<scale
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="100"
    android:duration="700"/>

<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:startOffset="100"
    android:duration="500"/>

<translate
    android:fromYDelta="200%"
    android:toYDelta="0%"
    android:startOffset="100"
    android:duration="700"/>

我尝试了这个,但动画不起作用。

我搜索了这个,但它说我必须使用onWindowFocusChanged方法。

我是否必须使用onWindowFocusChanged方法来执行此操作?

我想对此代码使用onCreate方法。

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

更改

Animation animButtonEnter = AnimationUtils.loadAnimation(this, R.anim.button_enter);

Animation animButtonEnter = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.button_enter);