解决办法:
用((Animatable)imageView.getDrawable()).start()方式来启动。
举个栗子:
在加载页面的时候弹出dialog进行动画播放。
1、首先在资源文件drawble种创建一个动画文件:
文件名:anim_loading
为什么anim_loading文件不放在anim文件中呢?因为逐帧动画的xml文件本来就是属于drawble资源,如果放在anim文件夹android studio会报错:
Element animation-list must be declared 这样的异常,但是在eclipse中不会,因为android studio 中对资源的检查比较严格,是什么样的资源就应
该放在什么资源文件下,虽然报错但是不影响编译。
<?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:duration="40" android:drawable="@drawable/icon_progressbar1" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar2" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar3" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar4" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar5" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar6" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar7" /> <item android:duration="40" android:drawable="@drawable/icon_progressbar8" /> </animation-list>2、创建布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/black" android:gravity="center" android:orientation="vertical"><!--用ImageView来装载动画资源--> <ImageView android:id="@+id/oa_progress" android:layout_width="50dp" android:layout_height="60dp" android:src="@drawable/anim_loading" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:paddingBottom="@dimen/common_dimen_10dp" android:paddingLeft="@dimen/common_dimen_10dp" android:paddingRight="@dimen/common_dimen_10dp" android:text="正在拼命加载。。。"/> </LinearLayout> </FrameLayout>java代码:static Dialog dialog = null;public static void showProgressDialog(final Activity mContext){ if (mContext != null) { mContext.runOnUiThread(new Runnable() { @Override public void run() { if (dialog == null) { final View progressView = mContext.getLayoutInflater().inflate(R.layout.progress_dialog_in_oa, null);//获取imageview,因为他装载的是逐帧动画资源 final ImageView imageView = (ImageView)progressView.findViewById(R.id.oa_progress);//强制转换成Animatable类型
final Animatable animatable = ((Animatable)imageView.getDrawable());
dialog = new Dialog(mContext,R.style.CustomProgressDialog); dialog.setContentView(progressView); dialog.setCanceledOnTouchOutside(false);dialog.setOnKeyListener(new OnKeyListener() {@Override public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) { if (arg2.getKeyCode() == KeyEvent.KEYCODE_BACK) { animatable .stop(); dialog = null; } return false; } }); if(animatable != null){ animatable.start(); } dialog.show(); } } }); } }为什么能把imageView.getDrawable()强制转换成 Animatable呢?
java.lang.Object继承 | |||
↳ | android.graphics.drawable.Drawable | ||
↳ | android.graphics.drawable.DrawableContainer | ||
↳ | android.graphics.drawable.AnimationDrawable |
看下 AnimationDrawable 的作用官方文档说明是:
用于创建逐帧动画的对象,由一系列可绘制对象定义,可用作View对象的背景。
创建逐帧动画的最简单的方法是将XML文件中的动画定义在res / drawable /文件夹中,并将其设置为View对象的背景。然后,调用
start()
运行动画。