標籤:
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。
【執行個體示範】下面通過代碼來示範如何設定一個簡單的漸層透明度動畫效果。
- public class firstActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) { //重載onCreate方法
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView對象
- Button btn1=(Button)findViewById(R.id.button1); //按鈕對象
- Button btn2=(Button)findViewById(R.id.button2);
- final Animation alphaAnimation=new AlphaAnimation(0.1f,1.0f); //設定透明度動畫效果
- btn1.setOnClickListener(new View.OnClickListener() { //設定監聽器
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- alphaAnimation.setDuration(30000); //設定期間
- image.setAnimation(alphaAnimation); //設定動畫
- alphaAnimation.startNow(); //啟動動畫
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() { //設定監聽器
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- scaleAnimation.cancel(); //取消動畫執行
- }
- });
- }
- }
在這段代碼中,首先通過AlphaAnimation構造方法建立了一個透明度變化的動畫對象。然後,在第一個按鈕監聽器中設定了動畫的期間,之後啟動該動畫。在第二個按鈕監聽器中取消該動畫。讀者運行這段代碼,將看到圖片的透明度由淺入深逐漸層化,9.11所示。最後,圖片變為完全不透明的時候停止,9.12所示。
Android開發_Animation