android开发之Animation(五)

android开发之Animation的使用(五)

本博文主要讲述的是Animation中的AnimationLisenter的使用方法,以及此类的一些生命周期函数的调用,代码实例如下:

MainActivity.java:

package com.example.animationlistener;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends Activity {

private ViewGroup viewGroup = null;

private Button addButton = null;

private Button removeButton = null;

private ImageView imageView = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

addButton = (Button)findViewById(R.id.addButton);

addButton.setOnClickListener(new AddButtonListener());

removeButton = (Button)findViewById(R.id.removeButton);

removeButton.setOnClickListener(new RemoveButtonListener());

viewGroup = (ViewGroup)findViewById(R.id.layout_id);

imageView = (ImageView)findViewById(R.id.myImage);

}

//删除imageView控件

class RemoveButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//创建一个淡出效果的动画对象

AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);

alphaAnimation.setDuration(2000);

//给Animation对象设置监听器

alphaAnimation.setAnimationListener(new RemoveAnimationListener());

imageView.startAnimation(alphaAnimation);

}

}

//添加ImageView控件

class AddButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);

alphaAnimation.setDuration(2000);

//在当前Activity中创建一个ImageView控件

ImageView addImageView = new ImageView(MainActivity.this);

//给ImageView设置ID

addImageView.setId(R.id.myImage);

//给ImageView控件设置图片资源

addImageView.setImageResource(R.drawable.ic_launcher);

//viewGroup表示的是main.xml中的整个布局

//将imageView添加到布局中

viewGroup.addView(addImageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

//alphaAnimation.setAnimationListener(new addAnimationListener());

imageView = addImageView;

addImageView.startAnimation(alphaAnimation);

}

}

//AnimationLinstener主要是在Animation动画效果的开始和结束等周期时,会调用其中的相关函数

class RemoveAnimationListener  implements AnimationListener{

//当Animation效果结束后调用此方法

@Override

public void onAnimationEnd(Animation animation) {

// TODO Auto-generated method stub

System.out.println("Animation end");

//将ImageView在布局中删除

viewGroup.removeView(imageView);

}

//当animation效果重复执行时调用此方法

@Override

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

System.out.println("Animation repeat");

}

//当animation效果开始执行时调用此方法

@Override

public void onAnimationStart(Animation animation) {

// TODO Auto-generated method stub

System.out.println("Animation start");

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

主布局文件main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/layout_id"

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=".MainActivity" >

<TextView

android:id="@+id/myText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

<ImageView

android:id="@+id/myImage"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"

android:layout_below="@id/myText"

/>

<Button

android:id="@+id/addButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/myImage"

android:text="add image"

/>

<Button

android:id="@+id/removeButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/addButton"

android:text="remove image"

/>

</RelativeLayout>

时间: 2024-08-05 22:25:53

android开发之Animation(五)的相关文章

Android开发之Tween(补间动画)完全解析(下)

欢迎转载,转载请注明出处:http://blog.csdn.net/dmk877/article/details/51980734 在上一篇文章中,我们详细讨论了Tween动画的xml的实现以及interpolator的使用,相信通过上篇文章大家对Tween动画的xml属性的配置会有一个详细的理解,当然这篇文章也是承接上篇文章,所以强烈建议先阅读上篇文章:Android开发之Tween(补间动画)完全解析(上),这篇文章将从代码的角度实现上篇文章的效果.如有疑问请留言,如有谬误欢迎批评指正. T

android开发之Animations的使用(三)

android开发之Animations的使用(三) 本博文主要讲述的是,Animations在android开发中的用来循环播放动画的效果: MainActivity.java: package com.example.animationtest3; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.v

Android开发之TextView高级应用

我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.下面就和大家分享一下TextView的一些使用技巧. Android中设置文本样式的几种方法: 1.将android:autoLink属性值设为true.系统会自动识别E-mail.电话.网址等特殊文本. 2.使用Html标签,例如,<font>.<img>等.不要设置 android:autoLink 属性. 3.在Java代码中直接使用Span对象来设置文本样式.这种方法需要将文本

Android开发之MediaRecorder类详解

MediaRecorder类详解 手机一般都有麦克风和摄像头,而Android系统就可以利用这些硬件来录制音视频了. 为了增加对录制音视频的支持,Android系统提供了一个MediaRecorder的类.该类的使用也非常简单,下面让我们来了解一下这个类: 一.类结构: java.lang.Object    ? android.media.MediaRecorder 二.类概述: 用于录制音频和视频的一个类. 三.状态图: 说明: 与MediaPlayer类非常相似MediaRecorder也

Android开发之PopupWindow

/* *  Android开发之PopupWindow * *  Created on: 2011-8-8 *  Author: blueeagle *  Email: [email protected] */ 聪明的人善于总结,记录,不知道这是谁说的了,反正要当一个聪明人,我得先学会总结,记录.最近在Android的学习过程中,发现 PopupWindow也是值得研究一番的一个东东,因此拿过来说道说道.与其相似的就归纳到一起说道吧,那就是AlertDialog和Toast. PopupWind

android开发之AlarmManager的使用方法

android开发之AlarmManager的使用方法 在android开发过程中.经常会用到AlarmManager来定时发送一个广播,或者启动一个Service,又或者启动一个Activity.本文我们会分别介绍,在开发过程中AlarmManager的三种用法. 1.使用alarm来发送一个广播        首先,我们需要创建一个Intent的实例.用来发送广播.代码如下: 需要发送的action可以自己随便定义,以下代码实现每五秒就发送一个CLOCK广播 需要在Manifest中定义一个

android开发之Animations的使用(四)

android开发之Animations的使用(四) 本博文主要讲述的是,animation在layout中的使用.本文是用ListView控件为例子 实现在layout中的使用有两种方法, 第一是直接使用xml文件中的layoutAnimation标签 第二是使用代码实现,使用layoutAnimationController对象完成, 详细代码如下: MainActivity.java: package com.example.animationtest4; import java.util

Android开发之JSON使用

Android开发之JSON使用 今天在论坛看到有不少朋友问关于json的问题,所以想写一篇关于android中使用json的博客. 首先 json是什么 JSON的全称是JavaScript Object Notation,从这里可以看到它源于JavaScript,它采用文本形式体现.比如 {"name":"zhangsan","age":20,"wife":"如花"} 这个简单表示了一个对象,在java

Android开发之bindService()侦听service内部状态

在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 1 public static interface CallBack{ 2 void onDataChange(String data); 3 } 2. 1 private CallBack callBack=null; 2 public void setCallBack(CallBack callB