Android中动画有两种,今天我们介绍一种的一种:帧动画。
1.其实我们上网浏览网页,会发现有很多动画,其中有很多都运用的帧动画,也就是把一张一张图片(帧)拼接出来,连续播放。首先我们要准备要播放的图片,在res /anim/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/loading1"
android:duration="150"/>
<item
android:drawable="@drawable/loading2"
android:duration="150"/>
<item
android:drawable="@drawable/loading3"
android:duration="150"/>
<item
android:drawable="@drawable/loading4"
android:duration="150"/>
<item
android:drawable="@drawable/loading5"
android:duration="150"/>
</animation-list>
2.在布局文件中用ImageView控件作为动画的载体 (res/layout/loading.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/loadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..." />
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
3.在CustomFrameDialog设置点击dialog外面对话框不取消
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View loadingView = View.inflate(getContext(), R.layout.loading, null);
loadingImageView = (ImageView) loadingView.findViewById(R.id.loadingImageView);
loadingImageView.setImageResource(R.anim.loading);
setContentView(loadingView);
//点击dialog外面对话框不取消
setCanceledOnTouchOutside(false);
}
4.重写dialog的show方法和dismiss方法
@Override
public void show() {
super.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loadingAnimationDrawable = (AnimationDrawable) loadingImageView.getDrawable();
loadingAnimationDrawable.start();//开启动画,每隔10毫秒切换一张图片
}
}, 10);
}
@Override
public void dismiss() {
super.dismiss();
loadingAnimationDrawable = (AnimationDrawable) loadingImageView.getDrawable();
loadingAnimationDrawable.stop();//停止动画
}
效果图