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>

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

  1. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

  2. Android开发之PopupWindow

      /* *  Android开发之PopupWindow * *  Created on: 2011-8-8 *  Author: blueeagle *  Email: liujiaxiang@g ...

  3. android开发之Animations的使用(二)

    android开发之Animations的使用(二) 本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提 ...

  4. Android开发之TextView高级应用

    Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...

  5. Android开发之MdiaPlayer详解

    Android开发之MdiaPlayer详解 MediaPlayer类可用于控制音频/视频文件或流的播放,我曾在<Android开发之基于Service的音乐播放器>一文中介绍过它的使用. ...

  6. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  7. Android开发之Git配置

    Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...

  8. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  9. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

随机推荐

  1. HDU 4314 Contest 2

    可以知道,逃出的人中,最后一个应当是A+B最长的,这是很容易发现的.那么,最选逃出去的必定是A+B最短的.这符合最优. 于是,可以把各小矮人按A+B的和由大到小排序.定义DP[i][j]为i个人中逃出 ...

  2. POJ 1721

    好像不需要用到开方什么的... 可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环.其实,K次洗牌等于是T^(2^K)了.既然是循环,必定有周期.那么,周期是多 ...

  3. bzoj3626【LNOI2014】LCA

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1266  Solved: 448 [Submit][Stat ...

  4. 【万里征程——Windows App开发】控件大集合2

    以下再来看看一些前面还没有讲过的控件,只是控件太多以至于无法所有列出来,大家仅仅好举一反三啦. Button 前面最经常使用的控件就是Button啦,Button另一个有意思的属性呢.当把鼠标指针放在 ...

  5. Struts2学习(四)利用ajax异步上传

    上一篇说到怎样在struts2中进行上传下载.我们使用了struts的标签通过表单提交的方式,但大家知道表单提交会造成页面总体的刷新,这样的方式很不友好,那我们今天就来说说怎样结合ajax方式进行异步 ...

  6. SQL学习之使用order by 依照指定顺序排序或自己定义顺序排序

    我们通常须要依据客户需求对于查询出来的结果给客户提供自己定义的排序方式,那么我们通常sql须要实现方式都有哪些,參考很多其它资料总结例如以下(不完好的和错误望大家指出): 一.假设我们仅仅是对于在某个 ...

  7. emitter 增强 多条件触发

    ;(function(global ,undefined){ var evts = {} ,onceTag = '__event_once' function emit(event ){ ) if ( ...

  8. 汇编 -- Hook API (MessageBoxW)

    说到HOOK.我看了非常多的资料和教程.无奈就是学不会HOOK.不懂是我的理解能力差.还是你们说的 不够明确,直到我看了下面这篇文章,最终学会了HOOK: http://blog.sina.com.c ...

  9. [雅礼NOIP2018集训 day1]

    现在才来填坑,之后还要陆续补其他几天的,可能前几天真的太颓了 T1: 题目大意:给定一个长度为n的序列,m次询问每次询问给出l,r,询问区间l到r的元素在模k意义下的最大值 数据范围当然是你暴力写不过 ...

  10. HOOK劫持自己

    #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include "detours ...