新建项目:

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  图片原始透明度

最新文章

  1. redis 集群配置实战
  2. ASP.NET Repeater 绑定 DropDownList Calendar 选择日期
  3. Android中方便好用的倒计时类
  4. bzoj 2821 分块处理
  5. Java工具类 Apache Commons:commons-lang
  6. 【计算几何初步-线段相交+并查集】【HDU1558】Segment set
  7. 解决win10开机出现C:\WIndows\system32\config\systemprofile\Desktop不可用 问题
  8. C#异步Socket示例
  9. ●BZOJ 1692 [Usaco2007 Dec]队列变换
  10. UVA140-Bandwidth(搜索剪枝)
  11. vuejs怎么在服务器部署?(知乎)
  12. Hibernate关联关系配置(一对多,一对一,多对多)
  13. MVC母版页_Layout.cshtml
  14. Linux命令(五)免密码远程登录和配置别名
  15. oracle直接读写ms sqlserver数据库(二)配置透明网关
  16. Fair CodeForces - 987D (bfs)
  17. CoderForce 140C-New Year Snowmen(贪心)
  18. soj1010. Zipper
  19. mysql重复数据查询
  20. 5、RabbitMQ-订阅模式 Publish/Subscribe

热门文章

  1. 【转载】Photoshop-制作图片圆角2种方法
  2. 关于ScrollView嵌套RecyclerView出现item显示不全的问题
  3. J.U.C并发框架源码阅读(三)ReentrantLock
  4. 用了GradientDrawable后,当点击控件时,控件大小发生变化
  5. 使用GEANT4的模拟技术1
  6. Handler嵌套--可以
  7. EF for oracle中无法读取配置 显示无法open问题解决方式
  8. OpenFire匿名登陆
  9. Android开发必须知道SERVICE的10件事
  10. LNMP第一部分环境搭建