2012-01-30 65 views
1

我已经完成了这些不起作用?如何在android中的onCreate()中启动动画?

public class AnimationActivity extends Activity{ 
      private AnimationDrawable yourAnimation; 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       // TODO Auto-generated method stub 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.animation); 
       getIntentValues(); 
       final ImageView imageView = (ImageView) findViewById(R.id.animation_iv); 
       imageView.setBackgroundResource(R.drawable.loadinganim); 
       yourAnimation = (AnimationDrawable) imageView.getBackground(); 
       yourAnimation.start(); 
    } 

RES \抽拉\ loading.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/v1" android:duration="160" /> 
    <item android:drawable="@drawable/v4" android:duration="160" /> 
    <item android:drawable="@drawable/v7" android:duration="160" /> 
    <item android:drawable="@drawable/v8" android:duration="160" /> 
    <item android:drawable="@drawable/v9" android:duration="160" /> 
    <item android:drawable="@drawable/v10" android:duration="160" /> 
    <item android:drawable="@drawable/v11" android:duration="160" /> 
    </animation-list> 

RES \布局\ animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    > 
<ImageView 
android:id="@+id/animation_iv" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 

动画开始于ImageView的的onClick()。如果有的话一个人有想法帮助我。

+0

我的代码或例子对您有用吗? – Praveenkumar 2012-01-31 04:01:09

回答

3
  view.setImageResource(R.anim.framer); 
      view.post(new Runnable() { 
      public void run() {    
       AnimationDrawable animationDrawable = (AnimationDrawable)view.getDrawable(); 
       animationDrawable.start(); 
      } 
      }); 

这对我工作尝试!!!!

+0

我怎么能在几秒钟后停止它 – sai 2012-01-30 12:57:00

+0

它工作正常。但几秒钟后,我想转到下一个活动。 – sai 2012-01-30 13:06:31

+0

我不明白你的意思,几秒钟后去下一个活动,但要停止它,你可以使用animationDrawable.stop(); – 2012-02-01 05:54:12

1

似乎在onCreate方法运行时Drawable没有完全初始化,因此启动动画将不起作用。

onWindowFocusChanged方法开始动画。这可悲的只记录在Animation Drawable Guide而不是相应的Java文档中。

我在这个问题上提交了一个Documentation Bug。如果您希望Android团队改进文档,请将问题列出。

0

好像你在做错cast.Your背景是StateListDrawable,而不是AnimationDrawable。有类似的问题here。希望它会有所帮助。

2

是的,你的方法是正确的,但是,你需要实现这一点 -

public void onWindowFocusChanged (boolean hasFocus) 
{ 
    super.onWindowFocusChanged(hasFocus); 
    AnimationDrawable yourAnimation = 
     (AnimationDrawable) animation.getBackground(); 
    if(hasFocus) { 
     frameAnimation.start(); 
    } else { 
     frameAnimation.stop(); 
    } 
} 

想在这个example

0

onEnterAnimationComplete()

“活动不能在此期间绘制的窗口动画为了知道什么时候开始绘制是安全的,他们可以重写这个方法,当输入的动画完成时这个方法会被调用。

您可以重写此方法并在此方法中启动您的动画。

+0

这工作对我来说,不知道为什么downvoted。 – 2016-11-23 12:09:20