標籤:android style blog http io ar os sp java
Android動畫的兩種方式,其中幀動畫上篇文章已經講了,這次主要講解的就是補間動畫,補間動畫就是動畫業務情境中常用的旋轉,平移,縮放,和漸層效果,幀動畫是通過輪播動畫實現動畫效果,補間動畫通過在兩個主要畫面格之間補充漸層的動畫效果來實現的,相對而言補間動畫的暫用的空間更小,補間動畫有兩種方式,一種是直接在代碼中是實現,另外一種是在XML檔案中定義,然後通過代碼調用,如果以後有需要直接改xml檔案就行不需要改代碼。
布局檔案
先來看下是實現的效果:
Layout中xml設定:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.googletween.MainActivity" > <LinearLayout android:id="@+id/line" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:onClick="alphaEvent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="漸層" /> <Button android:onClick="roateEvent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋轉" /> <Button android:onClick="tranEvent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" /> <Button android:onClick="scaleEvent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="縮放" /> <Button android:onClick="multiEvent" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="混合" /> </LinearLayout> <ImageView android:id="@+id/image_change" android:layout_width="wrap_content" android:layout_below="@id/line"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:src="@drawable/ic_launcher" /> </RelativeLayout>
動畫效果
漸層透明度,初始化建構函式的時候兩個數字最小透明度和最大透明度:
AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f, 1f); //設定動畫時間 alphaAnimation.setDuration(3000); //重複次數 alphaAnimation.setRepeatCount(1); alphaAnimation.setRepeatMode(Animation.REVERSE); image.startAnimation(alphaAnimation);
旋轉效果,初始化的時候是旋轉0度到360度:
RotateAnimation rotateAnimation=new RotateAnimation(0f, 360f);rotateAnimation.setDuration(2000);image.startAnimation(rotateAnimation);
位移效果,第一個參數fromXDelta ,第二個參數toXDelta:分別是動畫起始、結束時X座標,第三個參數fromYDelta ,第四個參數toYDelta:分別是動畫起始、結束時Y座標:
TranslateAnimation translateAnimation=new TranslateAnimation(0f, 100f, 0f, 100f); translateAnimation.setDuration(2000); image.startAnimation(translateAnimation);
縮放效果
ScaleAnimation scaleAnimation=new ScaleAnimation(0.1f, 1f, 0.1f, 1f); scaleAnimation.setDuration(2000); image.startAnimation(scaleAnimation);
縮放的同時移動(最後兩種效果混合):
AnimationSet animationSet=new AnimationSet(true); TranslateAnimation translateAnimation=new TranslateAnimation(0f, 100f, 0f, 100f); ScaleAnimation scaleAnimation=new ScaleAnimation(0.1f, 1f, 0.1f, 1f); animationSet.addAnimation(translateAnimation); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(2000); image.startAnimation(animationSet);
第二種是在xml檔案中定義,將代碼中的屬性值在xml中設定即可:
漸層xml
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse"></alpha>
調用:
Animation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha); image.startAnimation(alphaAnimation);
旋轉xml:
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:toDegrees="360"> </rotate>
調用:
Animation rotateAnimation=AnimationUtils.loadAnimation(this, R.anim.roate); image.startAnimation(rotateAnimation);
縮放xml:
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1"> </scale>
調用:
Animation scaleAnimation=AnimationUtils.loadAnimation(this, R.anim.scale); image.startAnimation(scaleAnimation);
位移xml:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="0" android:toXDelta="100" android:fromYDelta="0" android:toYDelta="100"> </translate>
調用:
Animation translateAnimation=AnimationUtils.loadAnimation(this, R.anim.tran); image.startAnimation(translateAnimation);
組合xml:
<?xml version="1.0" encoding="utf-8"?><set> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromAlpha="0.1" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="1.0" > </alpha> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:toDegrees="360" > </rotate> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" > </scale> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100" android:toYDelta="100" > </translate></set>
調用:
Animation animationSet=AnimationUtils.loadAnimation(this, R.anim.set); image.startAnimation(animationSet);
Android動畫-補間(Tween)動畫