目前我知道的有三种方法:
代码如下 | 复制代码 |
ImageView imageView = (ImageView)findViewById(R.id.xxx);方法一:使用Runnalbe()来加载 imageView.setBackgroundResource(R.anim.xxxxx); final AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground(); imageView.post(new Runnable() { @Override public void run() { animationDrawable.start(); } }); |
方法二:使用AsyncTask异步加载启动
代码如下 | 复制代码 |
imageView.setBackgroundResource(R.anim.xxxxx); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.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 ""; } } |
方法三:通过添加addOnPreDrawListener来自动加载
代码如下 | 复制代码 |
imageView.setBackgroundResource(R.anim.xxxxx); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); imageView.getViewTreeObserver().addOnPreDrawListener(preDrawListener); OnPreDrawListener preDrawListener = new OnPreDrawListener(){ @Override public boolean onPreDraw() { animationDrawable.start(); return true; //必须要有这个true返回 } }; |