Android自定义View示例(四)—带有动画的Dialog

简介: MainActivity如下: package cc.testview1;import android.os.Bundle;import android.

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如下:

<RelativeLayout 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"
    >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dip"
        />
     
</RelativeLayout>


loading.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <TextView
        android:id="@+id/loadingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="loading..." />

    <ImageView
        android:id="@+id/loadingImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/loadingTextView"
        android:src="@drawable/ic_launcher" 
        />

</RelativeLayout>


loadinganimfirst.xml如下:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate 
       android:fromDegrees="90"
       android:toDegrees="-90"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="4000"
       android:repeatCount="infinite"
       android:repeatMode="reverse"
   />
</set>


loadinganimsecond.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/photo1" android:duration="500" />
    <item android:drawable="@drawable/photo2" android:duration="500" />
    <item android:drawable="@drawable/photo3" android:duration="500" />
</animation-list>



相关文章
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
644 30
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
537 65
Android自定义view之网易云推荐歌单界面
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
975 84
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
617 65
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
418 26
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
358 3
|
Android开发 开发者 UED
安卓开发中自定义View的实现与性能优化
【10月更文挑战第28天】在安卓开发领域,自定义View是提升应用界面独特性和用户体验的重要手段。本文将深入探讨如何高效地创建和管理自定义View,以及如何通过代码和性能调优来确保流畅的交互体验。我们将一起学习自定义View的生命周期、绘图基础和事件处理,进而探索内存和布局优化技巧,最终实现既美观又高效的安卓界面。
353 5
|
XML 搜索推荐 Android开发
安卓开发中的自定义View组件实践
【8月更文挑战第30天】探索Android世界,自定义View是提升应用界面的关键。本文以简洁的语言带你了解如何创建自定义View,从基础到高级技巧,一步步打造个性化的UI组件。
|
机器学习/深度学习 人工智能 算法
探索AI在医疗影像分析中的应用探索安卓开发中的自定义View组件
【7月更文挑战第31天】随着人工智能技术的飞速发展,其在医疗健康领域的应用日益广泛。本文将聚焦于AI技术在医疗影像分析中的运用,探讨其如何通过深度学习模型提高诊断的准确性和效率。我们将介绍一些关键的深度学习算法,并通过实际代码示例展示这些算法是如何应用于医学影像的处理和分析中。文章旨在为读者提供对AI在医疗领域应用的深刻理解和实用知识。
249 0
|
Go Android开发 数据格式
Android开发之自定义View(二)
在 Android开发之自定义View(一)中,讲解了最复杂的一种自定义View,本次将剩下的两种讲完~~~ go,go,go 继承原有的控件,在原有控件基础上进行修改,如TextView,这种方式常见且简单。
1112 0

热门文章

最新文章