標籤:
之前,我已經說了下,關於變換動畫的糅合。在這裡,我將說的是,寫一個動畫集AnimationSet(我們知道,在JAVA中有set容器,而set容器的特點就是:1.無序2.不含重複元素。相當於是數學中的集合)用來存放多個Animation對象,這是JAVA代碼的方法,還有一個方法就是在anim下的xml代碼寫一個集合,用來存放多個動畫,我在這裡用的是第二種方法
anim檔案下的xml代碼
xml代碼
1 <?xml version="1.0" encoding="UTF-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" 3 > 4 <alpha 5 android:duration="2000" 6 android:fromAlpha="0.0" 7 android:toAlpha="1.0" 8 android:fillAfter="true" 9 />10 <alpha11 android:startOffset="2000"12 android:fromAlpha="1.0"13 android:toAlpha="0.0"14 android:duration="2000"15 android:fillBefore="true"16 />17 </set>
布局檔案代碼
xml代碼
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.example.Tween_Animation.Alpha_MainActivity" > 7 8 <Button 9 android:id="@+id/button_scale"10 android:layout_width="fill_parent"11 android:layout_height="wrap_content"12 android:text="@string/button_stringScaleAnimation" />13 14 <LinearLayout15 android:gravity="center"16 android:layout_width="fill_parent"17 android:layout_height="fill_parent"18 android:orientation="vertical" >19 20 <ImageView21 android:id="@+id/imageview_scale"22 android:layout_width="wrap_content"23 android:layout_height="wrap_content"24 android:src="@drawable/ic_launcher" />25 </LinearLayout>26 27 </LinearLayout>
activity代碼
JAVA代碼
1 package com.example.Demo2; 2 3 4 5 import com.example.androidanimation.R; 6 7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.view.animation.Animation;12 import android.view.animation.AnimationUtils;13 import android.widget.Button;14 import android.widget.ImageView;15 16 public class MainActivity extends Activity implements OnClickListener{17 private Button button = null;18 private ImageView imageview = null;19 protected void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.activity_main);22 button = (Button) findViewById(R.id.button_scale);23 imageview = (ImageView) findViewById(R.id.imageview_scale);24 button.setOnClickListener(this);25 }26 public void onClick(View v) {27 Animation animation = AnimationUtils.loadAnimation(this, R.anim.demo2);28 imageview.startAnimation(animation);29 }30 }
經過我們就可以使用一個集合來載入一個動畫了,是不是很神奇啊?
android中的動畫之變化動畫案例2