可以在onCreate或者onResume中start Animation吗?

    最近产品中出了一个问题,在解决的过程中发现了一篇这样的文章:

    http://damianflannery.wordpress.com/2011/06/08/start-animation-in-oncreate-or-onresume-on-android/

   下面是这篇文章的一些观点和实现的一些方法。

    开门见山的讲,如果你在onCreate或者onResume中启动一个动画,那么结果会非常让你失望的。

    解决办法1: 在onCreate或者onResume中启动一个timer,delay一定的时间后启动动画。这样做的缺点是delay的时间太短的话,动画起不来;太长的话用户体验会很差。而且相同delay时间在不同的手机上可能表现还不一样。

  解决办法2: 在onWindowsFocussedChanged方法中启动动画。这个方法会在activity window获得focus或者失去focus时被调用。所以这个方法可以更好的表示出当前的activity是否被用户看到了。

    例子:

private TextView myTextView;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_home);
     myTextView= (TextView)findViewById(R.id.my_textview);
     anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
 }
 
@Override
public void onWindowFocusChanged (boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus)
      myTextView.startAnimation(anim);
}



*hasFoucs* 参数为true表示当前的window已经获得focus。

动画效果定义在下面的文件中 res/anim/fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="1500" />






你可能感兴趣的:(可以在onCreate或者onResume中start Animation吗?)