热门标签 | 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">







推荐阅读
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本文介绍如何通过SQL查询从JDE(JD Edwards)系统中提取所有字典数据,涵盖关键表的关联和字段选择。具体包括F0004和F0005系列表的数据提取方法。 ... [详细]
  • 本文详细介绍了如何通过命令行启动MySQL服务,包括打开命令提示符窗口、进入MySQL的bin目录、输入正确的连接命令以及注意事项。文中还提供了更多相关命令的资源链接。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
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社区 版权所有