热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android自定义View—带有动画的Dialog

MainActivity如下:packagecc.testview1;importandroid.os.Bundle;importandroid.app.Activity

MainActivity如下:

package cc.testview1;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 自定义Dialog,在Dialog中有动画(旋转动画或者帧动画)效果
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//第一种-->rotate动画
LoadingDialogFirst loadingDialogFirst=new LoadingDialogFirst(this,R.style.dialog);
loadingDialogFirst.show();

//第二种-->frame动画
//LoadingDialogSecond loadingDialogSecOnd=new LoadingDialogSecond(this,R.style.dialog);
//loadingDialogSecond.show();

}
}


LoadingDialogFirst如下:

package cc.testview1;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class LoadingDialogFirst extends Dialog {
private ImageView mLoadingImageView;
private Animation mLoadingAnimation;
public LoadingDialogFirst(Context context, boolean cancelable,OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public LoadingDialogFirst(Context context, int theme) {
super(context, theme);
}
public LoadingDialogFirst(Context context) {
super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView);
setContentView(loadingView);
}

@Override
public void show() {
super.show();
mLoadingAnimation=AnimationUtils.loadAnimation(getContext(), R.anim.loadinganimfirst);
mLoadingImageView.startAnimation(mLoadingAnimation);
}
@Override
public void dismiss() {
super.dismiss();
mLoadingAnimation.cancel();
}
}

LoadingDialogSecond如下:

package cc.testview1;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class LoadingDialogSecond extends Dialog {
private ImageView mLoadingImageView;
private AnimationDrawable mLoadingAnimationDrawable;
public LoadingDialogSecond(Context context, boolean cancelable,OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public LoadingDialogSecond(Context context, int theme) {
super(context, theme);
}
public LoadingDialogSecond(Context context) {
super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView);
mLoadingImageView.setImageResource(R.anim.loadinganimsecond);
setContentView(loadingView);
}

@Override
public void show() {
super.show();
//注意将动画的启动放置在Handler中.否则只可看到第一张图片
new Handler(){}.postDelayed(new Runnable() {
@Override
public void run() {
mLoadingAnimatiOnDrawable=(AnimationDrawable) mLoadingImageView.getDrawable();
mLoadingAnimationDrawable.start();
}
}, 10);
}
@Override
public void dismiss() {
super.dismiss();
//结束帧动画
mLoadingAnimatiOnDrawable=(AnimationDrawable) mLoadingImageView.getDrawable();
mLoadingAnimationDrawable.stop();
}
}


main.xml如下:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
> android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerHorizOntal="true"
android:layout_marginTop="30dip"
/>


loading.xml如下:


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
android:id="@+id/loadingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..." />
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loadingTextView"
android:src="@drawable/ic_launcher"
/>


loadinganimfirst.xml如下:



android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>


loadinganimsecond.xml如下:


xmlns:android="http://schemas.android.com/apk/res/android"
android:Oneshot="false">







推荐阅读
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
  • 本文介绍如何在应用程序中使用文本输入框创建密码输入框,并通过设置掩码来隐藏用户输入的内容。我们将详细解释代码实现,并提供专业的补充说明。 ... [详细]
  • 本文介绍如何通过SQL查询从JDE(JD Edwards)系统中提取所有字典数据,涵盖关键表的关联和字段选择。具体包括F0004和F0005系列表的数据提取方法。 ... [详细]
  • 本文详细介绍了如何通过命令行启动MySQL服务,包括打开命令提示符窗口、进入MySQL的bin目录、输入正确的连接命令以及注意事项。文中还提供了更多相关命令的资源链接。 ... [详细]
author-avatar
pfshi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有