项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷。google搜了下。记录一下。

这个AnimationDrawable.start不能直接写在onClick,onStart,onResume里面,是无效的,无法启动动画,只能写在比如事件监听当中。


以下有几种运行AnimationDrawable的方式。

第一种:在事件监听中start AnimationDrawable 下面一个例子举例 当一个视图树将要绘制时产生事件


AnimationDrawable ad;
ImageView iv = (ImageView) findViewById(R.id.animation_view);
iv.setBackgroundResource(R.drawable.animation);
ad = (AnimationDrawable) iv.getBackground();
iv.getViewTreeObserver().addOnPreDrawListener(opdl);

OnPreDrawListener opdl=new OnPreDrawListener(){
@Override
public boolean onPreDraw() {
ad.start();
return true; //注意此行返回的值
}

};


第二种方式启动动画:(在Activity启动时会自动运行动画)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();
RunAnim runAnim=new RunAnim();
runAnim.execute("");

class RunAnim extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
if (!animationDrawable.isRunning())
{
animationDrawable.stop();
animationDrawable.start();
}
return "";
}
}


第三种方式启动动画:(在Activity启动时会自动运行动画)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();
image.post(new Runnable()
{
@Override
public void run()
{
animationDrawable.start();
}
});

第四种方式启动动画:(在Activity启动时会自动运行动画)
ImageView image = (ImageView) findViewById(R.id.animation_view);
image.setBackgroundResource(R.anim.oldsheep_wait);
animationDrawable = (AnimationDrawable) image.getBackground();

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
animationDrawable.start();
super.onWindowFocusChanged(hasFocus);
}


更多相关文章

  1. Android(安卓)ApiDemos示例解析(97):Views->Animation->Push
  2. 【Android】关于FLAG_ACTIVITY_NEW_TASK 和 taskAffinity
  3. Android入门:Activity四种启动模式
  4. android 滚动手势动画
  5. android中的shadows
  6. android位移动画的两种实现方式
  7. Android(安卓)组件之Service解析
  8. Android命令行启动app应用
  9. Android(安卓)下拉刷新,上拉加载动画,这一个就够了

随机推荐

  1. 使用Bottom Navigation Activity实现Andr
  2. AndroidMenifest.xml中android:sharedUse
  3. Android属性动画源代码解析(超详细)
  4. This Android SDK requires Android Deve
  5. Android Robotium自动化测试入门
  6. [Android Training视频系列]1.1 Creating
  7. 修改Button的样式!
  8. android:layout_gravity 和 android:grav
  9. Android SDK 21.1 发布,Android 开发工具
  10. Android UI开发专题