android學習筆記之Animations

來源:互聯網
上載者:User
Animations是一套實現動畫的API

Animations的分類
1、Tweened Animations漸層動畫、補間動畫
提供了旋轉、移動、伸展和淡出等效果
2、Frame-by-Frame Animations
可以建立一個Drawable序列,這些Drawable可以按照指定的時間間歇一個一個的顯示。

Tweened Animations的分類
1、Alpha:淡入淡出效果
2、Scale:縮放效果
3、Rotate:旋轉效果
4、Translate:移動效果

Animations的第一種使用方法:

使用Tweened Animations的步驟
1、建立一個AnimationSet對象
2、根據需要建立相應的Animation對象
3、根據軟體動畫的需求,為Animation對象設定相應的資料
4、將Animation對象添加到AnimationSet對象當中
5、使用控制項對象開始執行AnimationSet

Animation的四個子類:
AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation

private class RotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//建立一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//建立一個RotateAnimation對象
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.2f);////旋轉點的x座標為0.5倍父控制項的寬度,y座標為0.2倍父控制項的長度
//設定動畫執行的時間(單位:毫秒)
rotateAnimation.setDuration(5000);
//將RotateAnimation對象添加到AnimationSet當中
animationSet.addAnimation(rotateAnimation);
//開始動畫
imageView.setAnimation(animationSet);
}
}
    private class ScaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//建立一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//建立一個ScaleAnimation對象
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//設定動畫執行的時間(單位:毫秒)
scaleAnimation.setDuration(5000);
//將ScaleAnimation對象添加到AnimationSet當中
animationSet.addAnimation(scaleAnimation);
//開始動畫
imageView.setAnimation(animationSet);
}
    }
    
    private class AlphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//建立一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//建立一個AlphaAnimation對象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設定動畫執行的時間(單位:毫秒)
alphaAnimation.setDuration(1000);
//將AlphaAnimation對象添加到AnimationSet當中
animationSet.addAnimation(alphaAnimation);
//開始動畫
imageView.setAnimation(animationSet);
}
    }
    
    private class TranslateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//建立一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//建立一個TranslateAnimation對象
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
//設定動畫執行的時間(單位:毫秒)
translateAnimation.setDuration(5000);
//將TranslateAnimation對象添加到AnimationSet當中
animationSet.addAnimation(translateAnimation);
//開始動畫
imageView.setAnimation(animationSet);
}
    }

Tween Animation的通用屬性:
1、setDuration(long durationMills)
設定動畫期間(單位:毫秒)
2、setFillAfter(Boolean fillAfter)
如果fillAfter的值為true,則動畫執行後,控制項將停留在執行結束的狀態
3、setFillBefore(Boolean fillBefore)
如果fillBefore的值為true,則動畫執行後,控制項將回到動畫執行之前的狀態
4、setStartOffSet(long startOffSet)
設定動畫執行之前的等待時間
5、setRepeatCount(int repeatCount)
設定動畫重複執行的次數

Animations的第二種使用方法:

1、在res檔案夾下面建立一個名為anim的檔案夾
2、建立xml檔案,並首先加入set標籤,改標籤如下:
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator">
</set>
在該標籤當中加入rotate, alpha, scale或者translate標籤
3、在代碼當中使用AnimationUtils當中裝載xml檔案,並產生Animation對象

Alpha的xml檔案編寫方法 alpha.xml
<<alpha android:fromAlpha= "0.1"
android:toAlpha= "0.0"
android:startOffset= "500"
android:duration= "500" />

代碼中:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);

Rotate的xml檔案編寫方法 rotate.xml
<rotate android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
android:pivotX的值共有三種設定方法:
1、android:pivoteX= “50”這種方法使用絕對位置定位
2、android:pivoteX= “50%”這種方法相對於控制項本身定位
3、android:pivoteX= “50%p”這種方法相對於控制項的父控制項定位

translate的xml檔案編寫方法
<translate android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000" />

scale的xml檔案編寫方法
<scale android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>

AnimationSet
1、AnimationSet是Animation的子類
2、一個AnimationSet包含了一系列的Animation
3、針對AnimationSet設定一些Animation的常見屬性(如startOffset, duration等等),可以被包含在AnimationSet當中的Animation整合。

往AnimationSet中加入多個動畫
//建立一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//建立一個RotateAnimation對象
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.2f);////旋轉點的x座標為0.5倍父控制項的寬度,y座標為0.2倍父控制項的長度
//建立一個AlphaAnimation對象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//將AlphaAnimation對象添加到AnimationSet當中
animationSet.addAnimation(alphaAnimation);
//設定動畫執行的時間(單位:毫秒)
animationSet.setDuration(5000);
//將RotateAnimation對象添加到AnimationSet當中
animationSet.addAnimation(rotateAnimation);
//開始動畫
imageView.setAnimation(animationSet);

也可以在載入布局檔案的時候,布局檔案裡面定義多個動畫效果。

什麼是Interpolator
Interpolator定義動畫變化的速率,在Animations架構當中定義了以下幾種Interpolator:
1、AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候加速
2、AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始加速
3、CycleInterpolator:動畫迴圈播放特定的次數,速率改變沿著正弦曲線
4、DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始減速。
5、LinearInterpolator:在動畫的以均勻的速率改變

在xml裡面設定:android:interpolator= ….
在代碼裡設定: (AlphaAnimation、AnimationSet等)animation.setInterpolator(new DecelerateInterpolator());

Frame-By-Frame Animations的使用方法:
在res/drawable當中建立一個xml檔案(如anim_nv.xml),用於定義Animations的動畫序列:
<animation-list  >
<item/>
<item   />
</animation-list>

代碼裡:
imageView.setBackgroundResource(R.drawable.anim_nv);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();

什麼是LayoutAnimationController
1、LayoutAnimationController用於為一個layout裡面的控制項,或者是一個ViewGroup裡面的控制項設定動畫效果。
2、每一個控制項都有相同的動畫效果
3、這些控制項的動畫效果在不同的時間顯示出來
4、LayoutAnimationController可以在xml檔案當中設定,也可以在代碼當中進行設定。

在xml當中使用LayoutAnimationController
1、在res/anim檔案夾當中建立一個新檔案,名為list_anim_layout.xml檔案:
<layoutAnimation xmlns:android= http://schemas.android.com/apk/res/android
android:delay= “0.5”
android:animationOrder= “random”
android:animation= “@anim/list_anim” />
2、在布局檔案當中為ListView添加如下配置:
android:layoutAnimation= “@anim/list_anim_layout”

在代碼當中使用LayoutAnimationController
1、建立一個Animation對象:
可以通過裝載xml檔案,或者直接使用Animation的建構函式建立Animation對象
Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_item);
2、使用如下代碼建立LayoutAnimationController對象:
LayoutAnimationController lac = new LayoutAnimationController(animation);
3、設定控制項顯示的順序:
Lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4、為ListView設定LayoutAnimationController屬性:
listView.setLayoutAnimation(lac);

什麼是AnimationListener
1、AnimationListener是一個監聽器
2、該監聽器在動畫執行的各個階段會得到通知,從而調用相應的方法
3、主要包含以下三個方法:
a)onAnimationEnd(Animation animation)
b)onAnimationRepeat(Animation animation)
c)onAnimationStart(Animation animation)

animation.setAnimationListener()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.