Android Animation 動畫使用詳解
閱讀五分鐘,每日十點,和您一起終身學(xué)習(xí),這里是程序員Android
本篇文章主要介紹?Android?開發(fā)中的部分知識點,通過閱讀本篇文章,您將收獲以下內(nèi)容:
一、幀動畫 使用詳解
二、補(bǔ)間動畫 使用詳解
三、屬性動畫 使用詳解
動畫在Android 開發(fā)中經(jīng)常會被用到,使用好的動畫效果有時候可以達(dá)到事半功倍的效果。
Android動畫常見的有 幀動畫 、補(bǔ)間動畫、屬性動畫 三種。
一、幀動畫 使用詳解
1. 在xml 聲明幀動畫
我們可以在 xml中設(shè)置要播放幀動畫的圖片資源,持續(xù)時間,播放屬性等。
在xml 聲明幀動畫舉例如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/bird0001_risk"
android:duration="80"/>
<item
android:drawable="@drawable/bird0002_risk"
android:duration="80"/>
... ...
android:duration="80"/>
<item
android:drawable="@drawable/bird0016_risk"
android:duration="80"/>
<item
android:drawable="@drawable/bird0017_risk"
android:duration="80"/>
<item
android:drawable="@drawable/bird0018_risk"
android:duration="80"/>
<item
android:drawable="@drawable/bird0019_risk"
android:duration="80"/>
<item
android:drawable="@drawable/bird0020_risk"
android:duration="80"/>
animation-list>
2.控件中幀動畫 xml文件
我們使用幀動畫的時候可以通過@anim/自定義文件名引用我們定義的xml動畫資源。
ImageView 中將幀動畫設(shè)置為背景的方法舉例如下:
<ImageView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:background="@anim/frame_animation" />
二、補(bǔ)間動畫 使用詳解
補(bǔ)間動畫也是Android中常用的動畫之一,相對屬性動畫來說,補(bǔ)間動畫的點擊事件不會跟著動畫的位置變化而變化。因為屬性動畫的點擊事件會隨著動畫的位置變化而變化,后續(xù)將逐漸被屬性動畫替代。
1. 補(bǔ)間動畫分類
補(bǔ)間動畫 常用的分類如下:
透明動畫 AlphaAnimation
旋轉(zhuǎn)動畫 ScaleAnimation
縮放動畫 RotateAnimation
平移動畫 TranslateAnimation
動畫集合 AnimationSet
XML 實現(xiàn)動畫效果
2. 透明動畫
AlphaAnimation 透明動畫 可以設(shè)置動畫的透明效果,執(zhí)行時間,重復(fù)方式、重復(fù)次數(shù)等等。
AlphaAnimation 使用舉例如下:
/**
* 透明度動畫AlphaAnimation 從不透明到完全透明
* **/
// 起始透明度 到結(jié)束透明度 不透明到透明(1f-0f)
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
// 動畫執(zhí)行時間
alphaAnimation.setDuration(4000);
// 設(shè)置重復(fù)次數(shù)
alphaAnimation.setRepeatCount(2);
// 重復(fù)模式
alphaAnimation.setRepeatMode(Animation.RESTART);
// alphaAnimation.setRepeatMode(Animation.REVERSE);
// 保持結(jié)束時候的狀態(tài)
alphaAnimation.setFillAfter(true);
mImageView.startAnimation(alphaAnimation);
3. 旋轉(zhuǎn)動畫
RotateAnimation 可以設(shè)置旋轉(zhuǎn)動畫的旋轉(zhuǎn)角度、持續(xù)時間、重復(fù)次數(shù)等等。
RotateAnimation 使用舉例如下:
/**
* 旋轉(zhuǎn)動畫RotateAnimation,旋轉(zhuǎn)360度
**/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
rotateAnimation.setDuration(2000);
rotateAnimation.setRepeatCount(2);
mImageView.startAnimation(rotateAnimation);
4. 縮放動畫
ScaleAnimation可以設(shè)置縮放動畫的倍數(shù),持續(xù)時間,重復(fù)模式等等。
ScaleAnimation使用舉例如下:
/**
* 縮放動畫 ScaleAnimation使用方法 縮放2倍
* */
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(2);
scaleAnimation.setRepeatMode(Animation.RESTART);
mImageView.startAnimation(scaleAnimation);
5. 平移動畫
TranslateAnimation可以設(shè)置平移動畫的舉例,持續(xù)時間,重復(fù)模式等等。
TranslateAnimation 使用舉例如下:
/***
* 平移動畫TranslateAnimation 從x,y 軸 從(0,0)平移到(300,200) *
**/
TranslateAnimation translateAnimation = new TranslateAnimation(0,
300.f, 0, 200.f);
translateAnimation.setDuration(2000);
translateAnimation.setRepeatCount(2);
translateAnimation.setRepeatMode(Animation.RESTART);
mImageView.startAnimation(translateAnimation);
6. 動畫集合
AnimationSet,可以實現(xiàn)設(shè)置AlphaAnimation、TranslateAnimation 、RotateAnimation、RotateAnimation 4種動畫累加在一起,進(jìn)而實現(xiàn)非一般的動畫效果。
動畫集合效果使用舉例如下:
/***
* 動畫集合使用效果如下:
* ***/
AnimationSet sets = new AnimationSet(true);
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.0f, 1.0f);
TranslateAnimation translateAnimation1 = new TranslateAnimation(0,
100.f, 0, 100.f);
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360);
ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 2, 1, 2);
// 將動畫添加到set集合中
sets.addAnimation(alphaAnimation1);
sets.addAnimation(translateAnimation1);
sets.addAnimation(rotateAnimation1);
sets.addAnimation(scaleAnimation1);
sets.setDuration(4000);
sets.setRepeatCount(2);
sets.setRepeatMode(Animation.RESTART);
mImageView.startAnimation(sets);
7. XML 實現(xiàn)動畫效果
實現(xiàn)動畫有兩種方式 ,一種是通過xml 設(shè)置,另一種是通過Java代碼動態(tài)設(shè)置,上面講的是Java代碼中的動態(tài)設(shè)置,下面我們來看xml代碼中的動畫設(shè)置實現(xiàn)方法。
1.XML文件聲明動畫內(nèi)容
比如我們在res/anim/hyperspace_jump.xml中通過set 集合,聲明旋轉(zhuǎn)以及縮放動畫方法舉例如下:
<set android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400"
android:fillBefore="false" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400" />
set>
set>
2.Java 代碼中加載xml動畫文件
完成在xml定義完動畫后,我們可以在Java代碼中動態(tài)引用xml文件,使用方法舉例如下:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
三、 屬性動畫 使用詳解
屬性動畫是補(bǔ)間動畫的延伸,主要解決動畫點擊事件可以隨位置到改變而改變,將來會替換補(bǔ)間動畫。
1. 屬性動畫分類
ValueAnimator
ObjectAnimator
TypeEvaluator
2. ValueAnimator 動畫
ValueAnimator 使用方法如下:
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.start();
3. ObjectAnimator動畫
1. 屬性動畫分類
屬性動畫跟補(bǔ)間動畫類似主要分為以下幾類,
透明動畫 alpha
旋轉(zhuǎn)動畫 rotation
縮放動畫 scaleX
平移動畫 translationX
動畫集合 AnimatorSet
2. 旋轉(zhuǎn)動畫
rotation 可以設(shè)置旋轉(zhuǎn)動畫的旋轉(zhuǎn)角度、持續(xù)時間、重復(fù)次數(shù)等等。
rotation 使用舉例如下:
/**
* 旋轉(zhuǎn)動畫 rotation
* */
ObjectAnimator rotationanimator = ObjectAnimator.ofFloat(mImageView,
"rotation", 0, 360);
rotationanimator.setDuration(2000);
rotationanimator.setRepeatCount(2);
rotationanimator.setRepeatMode(Animation.RESTART);
rotationanimator.start();
3. 縮放動畫
scale可以設(shè)置縮放動畫的倍數(shù),持續(xù)時間,重復(fù)模式等等。
scaleX 使用舉例如下:
/**
* scaleX 縮放動畫
*
* */
ObjectAnimator scaleXanimator = ObjectAnimator.ofFloat(mImageView,
"scaleX", 0, 2);
scaleXanimator.setDuration(2000);
scaleXanimator.setRepeatCount(2);
scaleXanimator.setRepeatMode(Animation.RESTART);
scaleXanimator.start();
4. 平移動畫
translation可以設(shè)置平移動畫的舉例,持續(xù)時間,重復(fù)模式等等。
translationX 使用舉例如下:
/**
* translationX平移動畫
*
* */
ObjectAnimator translationanimator = ObjectAnimator.ofFloat(mImageView,
"translationX", 0, 200f);
translationanimator.setDuration(2000);
translationanimator.setRepeatCount(2);
translationanimator.setRepeatMode(Animation.RESTART);
translationanimator.start();
5. 透明動畫
alpha透明動畫 可以設(shè)置動畫的透明效果,執(zhí)行時間,重復(fù)方式、重復(fù)次數(shù)等等。
alpha 使用舉例如下:
/**
* alpha 透明動畫 1.屬性動畫作用在誰身上 2.屬性名稱 3.屬性的變化范圍值 透明值
* **/
ObjectAnimator alphaanimator = ObjectAnimator.ofFloat(mImageView,
"alpha", 0, 1.0f);
alphaanimator.setDuration(4000);
alphaanimator.setRepeatCount(2);
alphaanimator.start();
6. 動畫集合
AnimationSet,可以實現(xiàn)設(shè)置alpha、scale、translation、rotation 4種動畫累加在一起,進(jìn)而實現(xiàn)非一般的動畫效果。
動畫集合效果使用舉例如下:
/**
* 動畫集合效果 rotation
* */
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageView, "alpha",
0, 1.0f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageView,
"translationX", 0, 100f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageView, "scaleX",
0, 3);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageView,
"rotation", 0, 90);
List<Animator> list = new ArrayList<Animator>();
// 將動畫集合添加到list集合中
list.add(animator1);
list.add(animator2);
list.add(animator3);
list.add(animator4);
// 播放集合中的動畫
animatorSet.playSequentially(list);
animatorSet.setDuration(2000);
animatorSet.start();
7. 動畫監(jiān)聽事件
動畫監(jiān)聽事件主要用來監(jiān)聽動畫播放時的Listener時間,比如監(jiān)聽動畫播放取消onAnimationCancel,動畫播放完成onAnimationEnd的事件。
動畫監(jiān)聽事件舉例如下:
scaleXanimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
@Override
public void onAnimationPause(Animator animation) {
super.onAnimationPause(animation);
}
@Override
public void onAnimationResume(Animator animation) {
super.onAnimationResume(animation);
}
});友情推薦
至此,本篇已結(jié)束,如有不對的地方,歡迎您的建議與指正。同時期待您的關(guān)注,感謝您的閱讀,謝謝!
點個在看,方便您使用時快速查找!
