安卓動畫

補間動畫 幀動畫 屬性動畫

TweeAnimation 補間動畫

只可以改變兩個關(guān)鍵幀之間的透明度,旋轉(zhuǎn),縮放,位移四個變化

AlphaAnimation
RotateAnimation
ScaleAnimation
TranslateAnimation

  1. 設(shè)置動畫的資源文件

補間動畫的資源文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    >
    <scale android:fromXScale="1.0"
           android:toXScale="2.0"
           android:fromYScale="1.0"
           android:toYScale="2.0"
           android:fillAfter="true"
           android:duration="3000"
           />
</set>
animation=AnimationUtils.loadAnimation(this, R.anim.anim_1);//加載動畫資源
btn.setOnClickListener(this);//啟動動畫
  1. 直接在代碼中設(shè)置動畫
 RotateAnimation animation = new RotateAnimation(0, 2700);//創(chuàng)建動畫
 animation.setDuration(10000);//設(shè)置時長
 animation.setRepeatCount(5);//設(shè)置重復(fù)次數(shù)
 animation.setInterpolator(new AnticipateInterpolator());//設(shè)置插值器
 animation.setAnimationListener(this);//設(shè)置監(jiān)聽
 mButton1.startAnimation(animation);//啟動動畫

TimeInterpolator和TypeEvaluator

TimeInterpolator中文翻譯為時間插值器,它的作用是根據(jù)時間流逝的百分比來計算出當(dāng)前屬性值改變的百分比

系統(tǒng)預(yù)置的有LinearInterpolator(線性插值器:勻速動畫)、AccelerateDecelerateInterpolator(加速減速插值器:動畫兩頭慢中間快)和DecelerateInterpolator(減速插值器:動畫越來越慢)等

TypeEvaluator的中文翻譯為類型估值算法,它的作用是根據(jù)當(dāng)前屬性改變的百分比來計算改變后的屬性值

系統(tǒng)預(yù)置的有IntEvaluator(針對整型屬性)、FloatEvaluator(針對浮點型屬性)和ArgbEvaluator(針對Color屬性)

為補間動畫添加插值器Interpolator

  • 幾種插值器
  1. 越來越快 AccelerateInterpolator()
  2. 越來越慢 DecelerateInterpolator()
  3. 先快后慢 AccelerateDecelerateInterpolator()
  4. 先后退一小步然后向前加速 AnticipateInterpolator()
  5. 快速到達終點超出一小步然后回到終點 OvershootInterpolator()
  6. 到達終點超出一小步然后回到終點 AnticipateOvershootInterpolator()
  7. 彈球效果,彈幾下回到終點 BounceInterpolator()
  8. 均勻速度 LinearInterpolator()

幀動畫

Frame Animation是順序播放事先做好的圖像,跟電影類似。不同于animation package,Android SDK提供了另外一個類AnimationDrawable來定義使用Frame Animation。
利用xml文件實現(xiàn):res/drawable-hdpi/frame.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="true">
       <item android:drawable="@drawable/p1" android:duration="1000"/>
       <item android:drawable="@drawable/p2" android:duration="1000"/>
       <item android:drawable="@drawable/p3" android:duration="1000"/>
</animation-list>
  • 使用幀動畫
AnimationDrawable anim = (AnimationDrawable)getResources().  
getDrawable(R.drawable.frame);  
textWidget = (TextView)findViewById(R.id.text_widget);  
textWidget.setText("背景漸變動畫效果");  
textWidget.setBackgroundDrawable(anim);  
anim.start();

屬性動畫

ValueAnimation

ObjectAnimation

AnimatorSet

動畫的常見使用場景

在ViewGroup中控制子元素的出場效果

  1. 在anim文件夾中給ViewGroup設(shè)置動畫
 <?xml version="1.0" encoding="utf-8"?>
 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
                 android:animation="@anim/animation_item"
                 android:animationOrder="normal"
                 android:delay="0.2">
    <!--animation 給子控件設(shè)置動畫資源-->
    <!--animationOrder 是定義子控件的出場順序-->
    <!--delay 是定義兩個子控件的動畫延遲時間,0.2是延遲的倍數(shù)-->
</layoutAnimation>
  1. 在anim文件夾中給子控件設(shè)置動畫效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="500"
     android:interpolator="@android:interpolator/anticipate"
     android:shareInterpolator="true"
    >
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"/>
    <translate
        android:fromXDelta="500"
        android:toXDelta="0"/>
</set>
  • 這種方式也可在代碼中通過LayoutAnimationController設(shè)置

Activity,F(xiàn)ragment的切換效果

  1. 設(shè)置activity的場景轉(zhuǎn)換動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="5000"
     android:interpolator="@android:interpolator/anticipate"
     android:shareInterpolator="true">
    <rotate
        android:fromDegrees="90"
        android:toDegrees="0"/>
    <translate
        android:fromXDelta="500"
        android:toXDelta="0"
        />
</set>
  1. 啟動activity的時候設(shè)置activity的轉(zhuǎn)場動畫,調(diào)用方法 overridePendingTransition,該方法必須在startActivity方法和finish方法之后調(diào)用才有效果
Intent intent = new Intent(this, SeconActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_enter, R.anim.activity_out);
                //分別設(shè)置進場和出場的動畫效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容