第一步:自定义progressDialog的样式 progress_dialog_layout.xml
[html] view plain copy
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/icon_progress_dialog" />
第二步:在styles.xml文件中定义ProgressDialog的主题为:无边框全透明背景
[html] view plain copy
第三部:在anim文件夹下,定义旋转动画的样式,这里定义了动画旋转时间为0.7秒,从0到359度,若设置成360度在停止时会出现停顿现象。动画的插值器为先加速后减速,旋转的中心点为imageView的自身中心点。然后repeateCount为重复次数,这里设置为 -1 ,表示无限重复。动画progress_rotate.xml 如下:
[html] view plain copy
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" />
这里顺便讲解下动画的属性,加深下印象:
android:fromDegrees 起始的角度度数
android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针
android:pivotX 旋转中心的X坐标
浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心
android:pivotY 旋转中心的Y坐标
浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心
android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。
android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,
android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止
android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效
android:detachWallpaper 表示是否在壁纸上运行
android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。
normal保持内容当前的z轴顺序
top运行时在最顶层显示
bottom运行时在最底层显示
在操作开始之前调用
if (drawableAnim != null) {
imageView.startAnimation(drawableAnim);
}
操作完成时调用 drawableAnim.clearAnimation();
第四部:继承AlertDialog实现自定义的ProgressDialog,如下:d
[java] view plain copy
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* 自定义过场动画,主要用户数据加载时,显示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressAlertDialog extends AlertDialog {
private ImageView progressImg;
//旋转动画
private Animation animation;
public ProgressAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog_layout);
//点击imageview外侧区域,动画不会消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_img);
//加载动画资源
animation = AnimationUtils.loadAnimation(getContext(), R.anim.progress_rotate);
//动画完成后,是否保留动画最后的状态,设为true
animation.setFillAfter(true);
}
/**
* 在AlertDialog的 onStart() 生命周期里面执行开始动画
*/
@Override
protected void onStart() {
super.onStart();
if( animation != null){
progressImg.startAnimation(animation);
[java] view plain copy
}
}
/**
* 在AlertDialog的onStop()生命周期里面执行停止动画
*/
@Override
protected void onStop() {
super.onStop();
progressImg.clearAnimation();
}
}
效果gif图如下:
第二种使用 帧动画实现自定义ProgressDialog,
第一步:也是定义动画布局progress_drawable_dialog_layout.xml文件,如下:
[html] view plain copy
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/drawable_anim" />
第二步:在drawable文件下面定义帧动画,这就比旋转动画简单点,如下只有两个熟悉,单帧图片和它显示的时间:
[java] view plain copy
android:duration="200" />
android:duration="200" />
android:duration="200" />
android:duration="200" />
android:duration="200" />
android:duration="200" />
android:duration="200" />
第三部:也是使用旋转动画中的AlertDialog主题;
第四部:继承AlertDialog实现自定义的帧动画ProgressDialog:
[java] view plain copy
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.ImageView;
/**
* 自定义过场动画,主要用户数据加载时,显示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressDrawableAlertDialog extends AlertDialog {
private ImageView progressImg;
//帧动画
private AnimationDrawable animation;
public ProgressDrawableAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_drawable_dialog_layout);
//点击imageview外侧区域,动画不会消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_drawable_img);
//加载动画资源
animation = (AnimationDrawable) progressImg.getDrawable();
}
/**
* 在AlertDialog的 onStart() 生命周期里面执行开始动画
*/
@Override
protected void onStart() {
super.onStart();
animation.start();
}
/**
* 在AlertDialog的onStop()生命周期里面执行停止动画
*/
@Override
protected void onStop() {
super.onStop();
animation.stop();
}
}