android开发之Animation(五)
android开发之Animation的使用(五)
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;
}
}
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(五)的更多相关文章
- Android开发之Java集合类性能分析
对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...
- Android开发之PopupWindow
/* * Android开发之PopupWindow * * Created on: 2011-8-8 * Author: blueeagle * Email: liujiaxiang@g ...
- android开发之Animations的使用(二)
android开发之Animations的使用(二) 本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提 ...
- Android开发之TextView高级应用
Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...
- Android开发之MdiaPlayer详解
Android开发之MdiaPlayer详解 MediaPlayer类可用于控制音频/视频文件或流的播放,我曾在<Android开发之基于Service的音乐播放器>一文中介绍过它的使用. ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之Git配置
Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
随机推荐
- Nuxt开发经验分享
Nuxt开发经验分享 本文章基于starter-template模板进行讲解,面向有vue-cli开发经验的宝宝 vue init nuxt-community/starter-template ...
- 字符串中连续出现最多的子串 & 字符串中最长反复子串
字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀 ...
- POJ 1281 MANAGER
MANAGER Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description O ...
- Android与server通信的方法之中的一个(json)效率不高安全性不好
http通信.以xml或者json为载体.相互通信数据. Android对于http的网络通信,提供了标准的java接口--httpURLConnection接口.以及apache的接口--httpc ...
- 17、lambda表达式
一.简介 lambda表达式允许你通过表达式来代替功能接口,lambda表达式就和方法一样,它提供了一个正常的参数列表和一个使用这些参数的主体(body,可以是一个表达式或一个代码块),它还增强了集合 ...
- updatefile.sh - Linux下代码更新脚本
以下写的是一个关于文件上传的代码shell脚本 该篇文章主要有下面几个方面的考虑: 1.文章主要用于在Linux下代码包批量上传: 2.将被覆盖的代码备份做备份,用于兴许做问题查看或者代码的回退(回退 ...
- bzoj3436: 小K的农场(差分约束)
3436: 小K的农场 题目:传送门 题解: 查分基础: t==1 a>=b+c t==2 b>=a-c t==3 a>=b+0 b>=a+0 跑最长路一A 代码: #i ...
- Redis 安装与简单示例 <第一篇>【转】
一.Redis的安装 Redis下载地址如下:https://github.com/dmajkic/redis/downloads 解压后根据自己机器的实际情况选择32位或者64位.下载解压后图片如下 ...
- xBIM 基础09 WeXplorer 基本应用
系列目录 [已更新最新开发文章,点击查看详细] 在本教程中,将学习如何创建最基本和最直接的查看器. 除了展示建筑模型外,不做其他任何操作.它将只使用内置导航,但是不会对按钮做出事件响应. &l ...
- javascript对象的深度克隆
在做项目的时候需要向对象里面添加新属性,又不想修改原对象.于是就写: var newObj = oldObj,但是新对象属性改变后就对象也会跟着改变,这是因为无论是新对象还是旧对象,指向的内存地址都是 ...