Android动画之帧动画和补间动画
Android系統提供三種動畫:幀動畫、補間動畫和屬性動畫。這里先分析總結幀動畫和補間動畫。
FrameAnimation
幀動畫,通俗來說就是按照圖片動作順序依次播放來形成動畫,創建幀動畫可以用 xml 定義,也可直接使用代碼創建。
應用場景:較為復雜的個性化動畫效果。使用時一定要避免使用尺寸較大的圖片,否則會引起OOM。
1.在 res/drawable 文件夾下創建一個 drawable 文件,使用 animation-list 標簽:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><itemandroid:drawable="@drawable/pic_1"android:duration="100" /><itemandroid:drawable="@drawable/pic_2"android:duration="100" /><itemandroid:drawable="@drawable/pic_3"android:duration="100" />
</animation-list>
然后在 Activity 中使用獲取該 drawable 文件對應的 AnimationDrawable
//獲取Frame動畫文件對應的AnimationDrawable
mAnimationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_animator);
//設置AnimationDrawable為圖片的背景
imageView.setBackground(mAnimationDrawable);//開啟動畫
mAnimationDrawable.start();
//停止動畫
mAnimationDrawable.stop();
2.代碼創建幀動畫
//代碼創建Frame動畫
AnimationDrawable mAnimationDrawable = new AnimationDrawable();
//設置動畫循環播放,true為動畫只播放一次
mAnimationDrawable.setOneShot(false);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.pic_1),100);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.pic_2),100);
mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.pic_3),100);imageView.setBackground(mAnimationDrawable);//開啟
mAnimationDrawable.start();
//停止
mAnimationDrawable.stop();
TweenAnimation
補間動畫,主要有以下幾種:
位移動畫(Translation)
縮放動畫(Scale)
旋轉動畫(Rotate)
透明度動畫(Alpha)
組合動畫
不同的補間動畫有各自特有的屬性,以位移動畫為例:
1.在 res/anim 下創建一個xml文件 translation_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1200"android:startOffset ="0"android:fillBefore = "true"android:fillAfter = "false"android:fillEnabled= "false"android:repeatMode = "reverse"android:repeatCount = "5"android:interpolator = "@android:anim/accelerate_interpolator"<!--水平方向動畫開始的位置-->android:fromXDelta="0"<!--垂直方向動畫開始的位置-->android:fromYDelta="0"<!--水平方向動畫結束的位置-->android:toXDelta="100"<!--垂直方向動畫結束的位置-->android:toYDelta="100">
然后在 Activity 中獲取該 xml 文件對應的 TranslateAnimation,將其設置到想要設置位移動畫的 View 上
private void translation(){//獲取在anim下定義的動畫文件TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.translation_anim);//設置并開啟動畫ivImage.startAnimation(translateAnimation);
}
2.代碼中創建位移動畫,使用 Animation 的子類 TranslateAnimation
//代碼創建位移動畫
private void translation(){//表示相對View自身原點(View左上角)像素偏移量TranslateAnimation translateAnimation = new TranslateAnimation(0,100,0,100);//設置動畫持續時間translateAnimation.setDuration(1200);//設置動畫重復模式translateAnimation.setRepeatMode(Animation.REVERSE);//設置動畫重復次數translateAnimation.setRepeatCount(3);translateAnimation.setFillAfter(true);//設置動畫插值器translateAnimation.setInterpolator(this,android.R.anim.accelerate_interpolator);//translateAnimation.setInterpolator(new AccelerateInterpolator());ivImage.startAnimation(translateAnimation);
}
組合動畫使用 AnimationSet 來實現,可使用 xml 定義組合動畫,也可以使用代碼創建組合動畫。
1.在 res/anim 下創建一個 xml 文件 combine_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1200"><!--透明度動畫--><alphaandroid:repeatMode="reverse"android:repeatCount="10"android:fromAlpha="1"android:toAlpha="0.5" /><!--旋轉動畫--><rotateandroid:repeatMode="reverse"android:repeatCount="10"android:fromDegrees="0"android:pivotX="50%"android:pivotY="50%"android:toDegrees="360" /><!--縮放動畫--><scaleandroid:repeatMode="reverse"android:repeatCount="10"android:fromXScale="1"android:fromYScale="1"android:pivotX="50%"android:pivotY="50%"android:toXScale="2"android:toYScale="2" />
</set>
然后在 Activity 中獲取該 xml 文件對應的 AnimationSet,將其設置到想要設置動畫的 View 上
private void combine(){AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.combine_anim);ivImage.startAnimation(animationSet);
}
2.代碼創建組合動畫,使用 Animation 的子類 AnimationSet 創建 AnimationSet 對象,將要組合的動畫按序添加到 AnimationSet 中
//代碼創建組合動畫
private void combine(){AnimationSet animationSet = new AnimationSet(true);AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.5f);alphaAnimation.setRepeatMode(Animation.REVERSE);alphaAnimation.setRepeatCount(3);RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setRepeatMode(Animation.REVERSE);rotateAnimation.setRepeatCount(3);ScaleAnimation scaleAnimation = new ScaleAnimation(1,2,1,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);scaleAnimation.setRepeatMode(Animation.REVERSE);scaleAnimation.setRepeatCount(3);animationSet.addAnimation(alphaAnimation);animationSet.addAnimation(rotateAnimation);animationSet.addAnimation(scaleAnimation);animationSet.setDuration(1200);ivImage.startAnimation(animationSet);
}
總結
以上是生活随笔為你收集整理的Android动画之帧动画和补间动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android App的启动过程
- 下一篇: ANR异常发生条件