新建项目:

http://www.cnblogs.com/hongten/gallery/image/112163.html

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112162.html

p1.png是自己添加进去的,当然也可以使用其他图片

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/layoutId"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 >
8 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 >
12 <!-- 添加图片 -->
13 <Button
14 android:id="@+id/btn_add"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="添加图片"
18 />
19 <!-- 删除图片 -->
20 <Button
21 android:id="@+id/btn_delete"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="删除图片"
25 />
26 </LinearLayout>
27 <!-- 显示图片 -->
28 <ImageView
29 android:id="@+id/iv_image"
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:layout_centerInParent="true"
33 android:layout_marginTop="120dip"
34 android:layout_marginLeft="50dip"
35 android:src="@drawable/p1"
36 />
37 </RelativeLayout>

Main.java

 1 package com.b510;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.view.View.OnClickListener;
9 import android.view.animation.AlphaAnimation;
10 import android.view.animation.Animation;
11 import android.view.animation.Animation.AnimationListener;
12 import android.widget.Button;
13 import android.widget.ImageView;
14
15 public class Main extends Activity {
16 private static final String TAG="Main";
17 /** 添加图片 */
18 private Button addButton;
19 /** 删除图片 */
20 private Button deleteButton;
21 /** 显示图片 */
22 private ImageView imageView;
23 /** RaletvieLayout布局,他是包含了</br>Button,ImageView控件,定义在main.xml文件中 */
24 private ViewGroup viewGroup;
25
26 /** Called when the activity is first created. */
27 @Override
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.main);
31 // 从main.xml文件中找到对应的控件
32 addButton = (Button) findViewById(R.id.btn_add);
33 deleteButton = (Button) findViewById(R.id.btn_delete);
34 imageView = (ImageView) findViewById(R.id.iv_image);
35 viewGroup = (ViewGroup) findViewById(R.id.layoutId);
36
37 deleteButton.setOnClickListener(new OnClickListener() {
38
39 @Override
40 public void onClick(View v) {
41 // 申明一个AlphaAnimation对象,从完全不透明到完全透明
42 AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
43 // 设置动画持续时间为2秒钟
44 alphaAnimation.setDuration(2000);
45 // 执行动画前,延迟0.5秒钟
46 alphaAnimation.setStartOffset(500);
47 //为Animation对象设置监听器
48 alphaAnimation.setAnimationListener(new AnimationListener() {
49 @Override
50 public void onAnimationStart(Animation animation) {
51 Log.i(TAG, "start");
52 }
53
54 @Override
55 public void onAnimationRepeat(Animation animation) {
56 Log.i(TAG, "repeat");
57 }
58
59 @Override
60 public void onAnimationEnd(Animation animation) {
61 Log.i(TAG, "end");
62 //从viewGroup中移除imageView
63 viewGroup.removeView(imageView);
64 }
65 });
66 imageView.startAnimation(alphaAnimation);
67 }
68 });
69
70 addButton.setOnClickListener(new OnClickListener() {
71 @Override
72 public void onClick(View v) {
73 // 申明一个AlphaAnimation对象,从完全透明到完全不透明
74 AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
75 // 设置动画持续时间为2秒钟
76 alphaAnimation.setDuration(2000);
77 // 执行动画前,延迟0.5秒钟
78 alphaAnimation.setStartOffset(500);
79 viewGroup.addView(imageView);
80 // 启动动画
81 imageView.startAnimation(alphaAnimation);
82 }
83 });
84 }
85 }

运行效果

1.初始化

http://www.cnblogs.com/hongten/gallery/image/112164.html

2.点击删除图片按钮

http://www.cnblogs.com/hongten/gallery/image/112165.html

3.点击添加图片按钮

http://www.cnblogs.com/hongten/gallery/image/112166.html

4.后台运行情况

http://www.cnblogs.com/hongten/gallery/image/112167.html

当我们点击删除按钮的时候,android系统会自动调用onAnimationStart()方法,再调用onAnimationEnd()方法。

9.5  AlphaAnimation类:透明度变化动画类

AlphaAnimation类是Android系统中的透明度变化动画类,用于控制View对象的透明度变化,该类继承于Animation类。AlphaAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是AlphaAnimation构造方法。

【基本语法】public AlphaAnimation (float fromAlpha, float toAlpha)

参数说明

fromAlpha:开始时刻的透明度,取值范围0~1。

toAlpha:结束时刻的透明度,取值范围0~1。

【实例演示】下面通过代码来演示如何设置一个简单的渐变透明度动画效果。

  1. public class firstActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {               //重载onCreate方法
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象
  8. Button btn1=(Button)findViewById(R.id.button1);             //按钮对象
  9. Button btn2=(Button)findViewById(R.id.button2);
  10. final Animation alphaAnimation=new AlphaAnimation(0.1f,1.0f);   //设置透明度动画效果
  11. btn1.setOnClickListener(new View.OnClickListener() {            //设置监听器
  12. @Override
  13. public void onClick(View v) {
  14. // TODO Auto-generated method stub
  15. alphaAnimation.setDuration(30000);                  //设置持续时间
  16. image.setAnimation(alphaAnimation);             //设置动画
  17. alphaAnimation.startNow();                          //启动动画
  18. }
  19. });
  20. btn2.setOnClickListener(new View.OnClickListener() {            //设置监听器
  21. @Override
  22. public void onClick(View v) {
  23. // TODO Auto-generated method stub
  24. scaleAnimation.cancel();                            //取消动画执行
  25. }
  26. });
  27. }
  28. }

在这段代码中,首先通过AlphaAnimation构造方法创建了一个透明度变化的动画对象。然后,在第一个按钮监听器中设置了动画的持续时间,之后启动该动画。在第二个按钮监听器中取消该动画。读者运行这段代码,将看到图片的透明度由浅入深逐渐变化,如图9.11所示。最后,图片变为完全不透明的时候停止,如图9.12所示。

 
图9.11  透明度渐变动画
 
图9.12  图片原始透明度

Android开发_Animation的更多相关文章

  1. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  2. Android 开发一定要看的15个实战项目

    前言: 虽说网上有太多的Android课程,但是大多都是视频,有Android在线开发环境的几乎没有,但是对于学习Android的人来说拥有在线的Android开发环境是非常好的,可以随时动手操作学习 ...

  3. Android开发学习之路-关于Exception

    Exception在Java中是表示异常的一个类.它是Throwable的子类. 而Exception的子类RuntimeException是一个特殊的异常类,在代码中不需要对此类进行throw,而是 ...

  4. Android开发学习之路-Android中使用RxJava

    RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...

  5. Android开发学习之路-记一次CSDN公开课

    今天的CSDN公开课Android事件处理重难点快速掌握中老师讲到一个概念我觉得不正确. 原话是这样的:点击事件可以通过事件监听和回调两种方法实现. 我一听到之后我的表情是这样的: 这跟我学的看的都不 ...

  6. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  7. Android开发-之监听button点击事件

    一.实现button点击事件的方法 实现button点击事件的监听方法有很多种,这里总结了常用的四种方法: 1.匿名内部类 2.外部类(独立类) 3.实现OnClickListener接口 4.添加X ...

  8. Android 开发环境在 Windows7 下的部署安装

    Android SDK Android SDK 为 Android 应用的开发.测试和调试提了必要的API库和开发工具. ADT Bundle 下载 如果你是一个android 开发新手,推荐你下载使 ...

  9. Android开发之自定义的ListView(UITableViewController)

    Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...

随机推荐

  1. linux题目整理(二)

    1.如何过滤出已知当前目录下oldboy中的所有一级目录(不包含子目录,即只能是一级目录) 方法1:find ./ -type d -maxdepth 1方法2:ls -F方法3:ls -l  | g ...

  2. 阿里最新出的图书《码出高效:Java开发手册》宣传手册图片里出了比较搞笑的错误,大家没有发现?

  3. 洛谷 P1094 纪念品分组【贪心/双指针/最少多少组合法不要求连续的两两捆绑】

    题目描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得 的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品, 并且每组纪念品的 ...

  4. csu1808

    csu1808 题意 n 个点间有 m 条地铁,每条地铁可能属于不同的线路,每条地铁有权值即通过时花费的时间,如果乘坐第 i 条地铁来到地铁站 s,再乘坐第 j 条地铁离开,需要花费额外的时间 \(| ...

  5. Redis单机部署、添加开机自启、配置参数

    1.Redis简介 redis是使用C语言编写的开源的,支持网络,基于内存,可持久性的键值对存储数据库,2013年5月之前,Redis是最流行的键值对存储数据库,Redis采用内存数据集,支持多种数据 ...

  6. CSS 标准发布流程

    随着 CSS 3 的广泛应用,很多新的 CSS 属性层出不穷,有很多陌生的 CSS 属性出现,所以经常需要去学习新的 CSS 属性.新的属性往往介绍文章不多,所以有时候就需要去看看官方文档,此时会发现 ...

  7. linux mysql cluser集群

    管理节点的安装与启动 config.init内容如下 [NDBD DEFAULT] NoOfReplicas=1 #定义在Cluster环境中相同数据的份数,最大为4 [NDB_MGMD] #设置管理 ...

  8. APPENDIX: How to apply the Apache License to your work

    To apply the Apache License to your work, attach the following boilerplate notice, with the fields e ...

  9. ios学习流水账1

    1.UIImageview设边框.圆角 需要引QuartzCore/QuartzCore.h> //设UIImageView边框 CALayer *layer = [m_imgView laye ...

  10. selenium 定位元素方式大全

    starts-with 顾名思义,匹配一个属性开始位置的关键字 contains 匹配一个属性值中包含的字符串 text() 匹配的是显示文本信息,此处也可以用来做定位用 eg //input[sta ...