Android带动画效果的弹窗

在网络加载数据的时候通常需要很多时间,这个时候程序里面经常需要写一个提示正在加载数据的弹窗,这篇文章用两种方式实现带动画效果的Dialog:帧动画实现和GIF动态图实现,它们都能达到动画的效果

第一种、帧动画实现

自定义一个Dialog,先看一下布局文件dialog_animation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="80dp"
    android:paddingRight="80dp"
    android:paddingTop="20dp" >

    <ImageView
        android:id="@+id/dialog_animation_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/dialog_animation_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="正在加载数据。。。"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

</LinearLayout>

ImageView用来显示动画,TextView用于提示用户我们正在加载数据

public static Dialog createAnimationDailog(final Context context) {
		final Dialog dialog = new Dialog(context, R.style.dialog);
		dialog.setContentView(R.layout.dialog_animation);
		ImageView animationView = (ImageView) dialog
				.findViewById(R.id.dialog_animation_img);
		animationView.setBackgroundResource(R.drawable.animation_dialog);
		AnimationDrawable animationDrawable = (AnimationDrawable) animationView
				.getBackground();
		animationDrawable.start();
		final TextView textView = (TextView) dialog
				.findViewById(R.id.dialog_animation_txt);
		Animation animation = AnimationUtils.loadAnimation(context,
				R.anim.animation_dialog_txt);
		// animation的循环播放模式不起作用,只能手动的在动画播放完之后再播放一次
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationEnd(Animation arg0) {
				Animation anim = AnimationUtils.loadAnimation(context,
						R.anim.animation_dialog_txt);
				textView.startAnimation(anim);
				anim.setAnimationListener(this);
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

		});
		// 绑定动画
		textView.startAnimation(animation);
		return dialog;
	}

ImageView中的帧动画文件,很简单的,就三张图片顺序播放。新建res/drawable/animation_dialog.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/girl1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/girl2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/girl3"
        android:duration="100"/>

</animation-list>

TextView使用的补间动画文件。新建res/anim/animation_dialog_txt.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillBefore="true"
    android:fillEnabled="true"
    android:repeatMode="restart" >

    <translate
        android:duration="1000"
        android:fromXDelta="-20%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0%" />
    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toXDelta="20%" />

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="1" />
    <alpha
        android:duration="200"
        android:fromAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="0.8" />

</set>

效果图

截屏效果太差,实际动画是很流畅的,相信我。

第二种、GIF动态图实现

这种是通过播放GIF动态图来达到动画效果。

首先这需要一个第三方的jar包,GifView.jar,将其下载之后导入项目

新建布局文件dialog_gif.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingLeft="80dp"
    android:paddingRight="80dp"
    android:paddingTop="20dp" >

    <com.ant.liao.GifView
        android:id="@+id/dialog_gifView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/dialog_gif_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="正在加载数据。。。"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

</LinearLayout>
public static Dialog createGIFDialog(final Context context) {
		final Dialog dialog = new Dialog(context, R.style.dialog);
		dialog.setContentView(R.layout.dialog_gif);
		GifView gifView = (GifView) dialog.findViewById(R.id.dialog_gifView);
		gifView.setGifImage(R.drawable.ride);
		gifView.showAnimation();
		final TextView textView = (TextView) dialog
				.findViewById(R.id.dialog_gif_txt);
		Animation animation = AnimationUtils.loadAnimation(context,
				R.anim.animation_dialog_txt);

		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationEnd(Animation arg0) {
				Animation anim = AnimationUtils.loadAnimation(context,
						R.anim.animation_dialog_txt);
				textView.startAnimation(anim);
				anim.setAnimationListener(this);
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

		});
		// 绑定动画
		textView.startAnimation(animation);
		return dialog;
	}

效果图

源码下载

时间: 2024-08-07 12:35:46

Android带动画效果的弹窗的相关文章

写一个android带动画效果的TabHost(类似微博客户端的切换效果)

先上图: 这个Layout是: <?xml version="1.0" encoding="UTF-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" andro

Android学习笔记(25):带动画效果的View切换ViewAnimator及其子类

ViewAnimator可以实现带动画效果的View切换,其派生的子类是一些带动画效果切换功能的组件. ViewAnimator支持的XML属性: Attribute Name Description android:animateFirstView 设置显示第一个View组件时是否使用动画 android:inAnimation 设置显示组件时使用的动画 android:outAnimation 设置隐藏组件时使用的动画 1. ViewSwitcher视图切换组件. 添加视图的方法: 由Vie

带动画效果的抽屉菜单栏

带动画效果的抽屉菜单栏 带动画效果的抽屉菜单栏,将android L 中drawer-indicator/back-arrow移植到低版本Android系统中. 下载地址:http://www.devstore.cn/code/info/960.html 运行截图:

/*带动画效果的hover*/

1 <!DOCTYPE html> 2 /*带动画效果的hover*/ 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 <style> 8 .ele{ 9 background-color: #dddddd;/*带动画效果的hover*/ 10 } 11 .ele:hover{

带动画效果的jQuery手风琴

带动画效果的jQuery特效手风琴是一款带动画效果的手风琴作品,非常实用,可以用在新闻列表.FAQ等模块,默认的是打开第一个选项,查看其它的时候直接点击加号按钮就展开. 源码地址:http://www.huiyi8.com/sc/11120.html 带动画效果的jQuery手风琴

android anim 动画效果(转)

动画效果编程基础--AnimationAndroid      动画类型      Android的animation由四种类型组成      XML中    alpha    渐变透明度动画效果    scale    渐变尺寸伸缩动画效果    translate    画面转换位置移动动画效果    rotate    画面转移旋转动画效果        JavaCode中    AlphaAnimation    渐变透明度动画效果    ScaleAnimation    渐变尺寸伸缩

CSS3+Jquery实现带动画效果的下拉选择框

CSS3+JQuery实现带动画效果的下拉选择框. 元素结构为: 1 <div class="box"> 2 <p>this is the first li</p> 3 <div id="blank"></div> 4 <ul> 5 <li class="selected">this is the first li</li> 6 <li >

Android 使用动画效果后的控件位置处理 类似系统通知栏下拉动画

Android的动画的使用,请参考.Android的动画,在设计方面,我有点不太理解,觉得这样搞很怪,因为在控件动画后,即使设置了停留在动画结束时的位置,我们也确实看到了控件停在那个位置,但其实该控件的真实位置还是在原来动画前的那里.举个例子,如果有个Button,你给它设置了动画,让它移动到其他位置,当移动完成后,你会发现,点击Button没有任何效果,而在Button原来的位置,就是动画前的位置点击,明明没有任何控件,却看到了点击Button的效果.不知道Google为什么要这样设计.解决思

android anim 动画效果

动画效果编程基础--AnimationAndroid       动画类型       Android的animation由四种类型组成       XML中    alpha    渐变透明度动画效果    scale    渐变尺寸伸缩动画效果    translate    画面转换位置移动动画效果    rotate    画面转移旋转动画效果          JavaCode中    AlphaAnimation    渐变透明度动画效果    ScaleAnimation    渐