補間動畫Tween Animation
AlphaAnimtaion(透明)
/**
* 構(gòu)造方法:
* AlphaAnimation(fromAlpha, toAlpha)
* fromAlpha:開始時的透明度
* toAlpha:結(jié)束時的透明度
* */
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(1000);//設(shè)置動畫執(zhí)行時間
v.startAnimation(animation);
RotateAnimation(旋轉(zhuǎn))
/**
* 構(gòu)造方法:
* RotateAnimation(fromDegrees, toDegrees)
* RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY)
* RotateAnimation(fromDegrees, toDegrees, pivotXType,pivotXValue, pivotYType, pivotYValue)
* fromDegrees:從哪個旋轉(zhuǎn)角度開始
* toDegrees:轉(zhuǎn)到什么角度 后4個參數(shù)用于設(shè)置圍繞著旋轉(zhuǎn)的圓的圓心在哪里
* pivotXType:確定x軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* pivotXValue:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸 pivotYType:確定y軸坐標的類型
* pivotYType:確定y軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* pivotYValue:y軸的值,0.5f表明是以自身這個控件的一半長度為x軸
* */
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
v.startAnimation(rotateAnimation);
ScaleAnimation(縮放)
/**
* 構(gòu)造方法:
* ScaleAnimation(fromX, toX, fromY, toY)
* ScaleAnimation(fromX, toX, fromY, toY, pivotX, pivotY)
* ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue)
* fromX:x軸的初始值
* toX:x軸收縮后的值
* fromY:y軸的初始值
* toY:y軸收縮后的值
* pivotXType:確定x軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* pivotXValue:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸 pivotYType:確定y軸坐標的類型
* pivotYType:確定y軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* pivotYValue:y軸的值,0.5f表明是以自身這個控件的一半長度為x軸
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
v.startAnimation(scaleAnimation);
TranslateAnimation(位移)
/**
* 構(gòu)造方法:
* TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
* TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
* fromXType:確定x軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、 RELATIVE_TO_PARENT相對于父控件的坐標
* fromXValue:x軸的初始值
* toXType:確定x軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* toXValue:x軸的結(jié)束值
* fromYType:確定y軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* fromYValue:y軸的初始值
* toYType:確定y軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標
* toYValue:y軸的結(jié)束值
* */
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
v.startAnimation(translateAnimation);
AnimationSet(動畫集合)
/**
* 是否使用統(tǒng)一interpolator,即動畫的變化速率
* */
AnimationSet animationSet = new AnimationSet(true);
//可以設(shè)置動畫集合的執(zhí)行時間,那么單獨的動畫執(zhí)行時間就會變?yōu)闊o效
//animationSet.setDuration(5000);
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
animationSet.addAnimation(rotateAnimation);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
animationSet.addAnimation(scaleAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
translateAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
animationSet.addAnimation(translateAnimation);
v.startAnimation(animationSet);
Interpolator(動畫變化速率)
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);// 設(shè)置動畫執(zhí)行時間
rotateAnimation.setRepeatCount(1000);// 設(shè)置動畫執(zhí)行次數(shù)
/**
* Interpolator 定義了動畫的變化速度,可以實現(xiàn)勻速、正加速、負加速、無規(guī)則變加速等
* AccelerateDecelerateInterpolator 在動畫開始與結(jié)束的地方速率改變比較慢,在中間的時候加速
* AnticipateInterpolator 開始的時候向后然后向前甩
* AnticipateOvershootInterpolator 開始的時候向后然后向前甩一定值后返回最后的值
* BounceInterpolator 動畫結(jié)束的時候彈起
* CycleInterpolator 動畫循環(huán)播放特定的次數(shù),速率改變沿著正弦曲線
* DecelerateInterpolator 在動畫開始的地方快然后慢
* LinearInterpolator 以常量速率改變
* OvershootInterpolator 向前甩一定值后再回到原來位置
* */
rotateAnimation.setInterpolator(new LinearInterpolator());//勻速不變的
v.startAnimation(rotateAnimation);
其他常用方法:
**setRepeatMode **設(shè)置動作結(jié)束后,將怎樣處理
默認是Animation.RESTART,回到動作開始時的坐標 。參數(shù)應(yīng)是Animation.RESTART或Animation.REVERSE或Animation.INFINITE。REVERSE是反轉(zhuǎn)的意思,如果repeatCount大于1,repeatMode為Animation.REVERSE那么動作將會回來不斷執(zhí)行,即由a點去到b點后,會從b點和原來的動作相反的相同時間...
**setRepeatCount(repeatCount) **動畫執(zhí)行次數(shù)
參數(shù)是Animation.INFINITE則表示不斷重復該動作,只有在repeatCount大于0或為INFINITE的時候setRepeatMode才會生效。
</br>
幀動畫Frame Animation
- 動畫文件
<?xml version="1.0" encoding="utf-8"?>
<!--
根標簽為animation-list,其中oneshot代表著是否只展示一遍,設(shè)置為false會不停的循環(huán)播放動畫
根標簽下,通過item標簽對動畫中的每一個圖片進行聲明
android:duration 表示展示所用的該圖片的時間長度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item android:drawable="@drawable/ico0" android:duration="150"></item>
<item android:drawable="@drawable/ico1" android:duration="150"></item>
<item android:drawable="@drawable/ico2" android:duration="150"></item>
<item android:drawable="@drawable/ico3" android:duration="150"></item>
<item android:drawable="@drawable/ico4" android:duration="150"></item>
<item android:drawable="@drawable/ico5" android:duration="150"></item>
</animation-list>
- 布局文件
<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">
<ImageView
android:id="@+id/frame_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/anima"
android:layout_centerInParent="true"/>
</RelativeLayout>
- 代碼
ImageView iv = (ImageView) findViewById(R.id.frame_animation);
AnimationDrawable animationDrawable = (AnimationDrawable) iv.getDrawable();
animationDrawable.start();