標籤:
前面我已經說了變換動畫,並且變換動畫中分為4種情況:透明度動畫、旋轉動畫、縮放動畫、位移動畫。
今天我來說說關於使用變換動畫中的4種類型來實現它們的糅合。
我在這裡主要使用了一個Animation對象中的一個監聽方法--setAnimationListener。這個方法裡面只有一個參數,安卓api給出的這個方法的完整形態--
void android.view.animation.Animation.setAnimationListener(AnimationListener listener)。從中我們可以知道,這個參數需要傳遞一個AnimationListener 類型的參數,我們只需要使用匿名內部類來即可(這裡我的要求簡單,所以使用的是匿名內部類)。當我們使用了匿名內部類後,會出現三個方法。代碼如下JAVA代碼
1 animation1.setAnimationListener(new AnimationListener() { 2 //當動畫開始時調用 3 public void onAnimationStart(Animation animation) { 4 // TODO Auto-generated method stub 5 6 } 7 //當動畫重複時調用 8 public void onAnimationRepeat(Animation animation) { 9 // TODO Auto-generated method stub10 11 }12 //當動畫開始時調用13 public void onAnimationEnd(Animation animation) {14 // TODO Auto-generated method stub15 imageview.startAnimation(animation2);16 }17 });
其中方法animation1和animation2都是Animation對象。
看到這裡我們應該怎麼實現了吧,話不多說,直接貼完整的代碼
anim檔案下的xml代碼
1.alpha.xml代碼(animation1載入的)
1 <?xml version="1.0" encoding="utf-8"?>2 <set xmlns:android="http://schemas.android.com/apk/res/android">3 <alpha 4 android:duration="1000"5 android:fromAlpha="0.1"6 android:toAlpha="1.0"7 />8 </set>
2.translate.xml代碼
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <translate 5 android:duration="1000" 6 android:fromXDelta="10" 7 android:fromYDelta="10" 8 android:toXDelta="100" 9 android:toYDelta="100" />10 11 </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.Demo1; 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.Animation.AnimationListener;13 import android.view.animation.AnimationUtils;14 import android.widget.Button;15 import android.widget.ImageView;16 /*17 * 組合動畫18 * 先播放一個動畫然後再播放一個動畫19 */20 public class MainActivity extends Activity implements OnClickListener{21 private Button button = null;22 private ImageView imageview = null;23 24 protected void onCreate(Bundle savedInstanceState) {25 super.onCreate(savedInstanceState);26 setContentView(R.layout.activity_main);27 button = (Button) findViewById(R.id.button_scale);28 imageview = (ImageView) findViewById(R.id.imageview_scale);29 button.setText("組合動畫1");30 button.setOnClickListener(this);31 }32 public void onClick(View v) {33 Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.alpha);34 imageview.startAnimation(animation1);35 final Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);36 animation1.setAnimationListener(new AnimationListener() {37 //當動畫開始時調用38 public void onAnimationStart(Animation animation) {39 // TODO Auto-generated method stub40 41 }42 //當動畫重複時調用43 public void onAnimationRepeat(Animation animation) {44 // TODO Auto-generated method stub45 46 }47 //當動畫開始時調用48 public void onAnimationEnd(Animation animation) {49 // TODO Auto-generated method stub50 imageview.startAnimation(animation2);51 }52 });53 }54 }
這裡只是實現了兩種類型的動畫來完成效果,其實我們可以多種多樣,看自己的需要了。
android中的動畫之變化動畫案例1