使用旋转动画和帧动画实现自定义ProgressDialog
现在很多App在等陆或者在一些activity页面跳转的时候,使用ProgressDialog来实现等待加载数据的。然而使用系统的样式是非常没有自身app的特色的,所以这里教大家两种方法来自定义ProgressDialog,这两种方式就是:旋转动画 和 帧动画;源码放在最下面供大家下载
使用旋转动画实现ProgressDialog;
第一步:自定义progressDialog的样式 progress_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/icon_progress_dialog" />
</RelativeLayout>
第二步:在styles.xml文件中定义ProgressDialog的主题为:无边框全透明背景
<resources>
<!--自定义dialog背景全透明无边框theme -->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
第三部:在anim文件夹下,定义旋转动画的样式,这里定义了动画旋转时间为0.7秒,从0到359度,若设置成360度在停止时会出现停顿现象。动画的插值器为先加速后减速,旋转的中心点为imageView的自身中心点。然后repeateCount为重复次数,这里设置为 -1 ,表示无限重复。动画progress_rotate.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)-->
<rotate
android:duration="700"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" />
</set>
这里顺便讲解下动画的属性,加深下印象:
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运行时在最底层显示
在操作开始之前调用
操作完成时调用 drawableAnim.clearAnimation();
第四部:继承AlertDialog实现自定义的ProgressDialog,如下:d
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);
}
}
/**
* 在AlertDialog的onStop()生命周期里面执行停止动画
*/
@Override
protected void onStop() {
super.onStop();
progressImg.clearAnimation();
}
}
第二种使用 帧动画实现自定义ProgressDialog,
第一步:也是定义动画布局progress_drawable_dialog_layout.xml文件,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_drawable_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/drawable_anim" />
</RelativeLayout>
第二步:在drawable文件下面定义帧动画,这就比旋转动画简单点,如下只有两个熟悉,单帧图片和它显示的时间:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/img2"
android:duration="200" />
<item
android:drawable="@mipmap/img3"
android:duration="200" />
<item
android:drawable="@mipmap/img4"
android:duration="200" />
<item
android:drawable="@mipmap/img5"
android:duration="200" />
<item
android:drawable="@mipmap/img6"
android:duration="200" />
<item
android:drawable="@mipmap/img7"
android:duration="200" />
<item
android:drawable="@mipmap/img1"
android:duration="200" />
</animation-list>
第三部:也是使用旋转动画中的AlertDialog主题;
第四部:继承AlertDialog实现自定义的帧动画ProgressDialog:
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();
}
}
小姑图如下,我这里放了,4张一个人走路的不同动作,你可以找几张帧图片,实现自定义动作图片和显示时间,效果如下:
q其中主activity的layout文件和java代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirst.refreshdialog.MainActivity">
<Button
android:id="@+id/start_rotate_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载旋转进度条" />
<Button
android:id="@+id/start_drawable_anim_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载帧动画进度条" />
</LinearLayout>
package com.example.myfirst.refreshdialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
//定义旋转动画Dialog
private ProgressAlertDialog progress1;
//定义帧动画dialot
private ProgressDrawableAlertDialog progress2;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
//初始化进度条dialog
progress1 = new ProgressAlertDialog(this);
//开始选择的过场动画
findViewById(R.id.start_rotate_progress).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//让进度条显示出来
progress1.show();
//10秒钟后停止动画
handler.postDelayed(new Runnable() {
@Override
public void run() {
progress1.dismiss();
}
}, 10000);
}
});
progress2 = new ProgressDrawableAlertDialog(this);
//开始选择的过场动画
findViewById(R.id.start_drawable_anim_progress).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//让进度条显示出来
progress2.show();
//10秒钟后停止动画
handler.postDelayed(new Runnable() {
@Override
public void run() {
progress2.dismiss();
}
}, 10000);
}
});
}
}
免积分偶 点击打开链接下载示例
祝大家工作顺利,学习进步!