補間動畫(Tween)View Animation


一、 alpha:漸變透明度動畫效果


1. 代碼方式實現(xiàn)

AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(1000);
imageView.startAnimation(alphaAnimation);
  • 第一個參數(shù):起始透明度。
  • 第二個參數(shù):結(jié)束透明度
  • 0.0f 全透明
    1.0f 不透明

2. xml 方式實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <alpha 
        android:duration="1000" 
        android:fromAlpha="0.0" 
        android:toAlpha="1.0" />
 </set>
Animation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha);  //加載Xml文件中的動畫
imageView.startAnimation(alphaAnimation);

二、 scale:漸變尺寸伸縮動畫效果


1. 代碼方式實現(xiàn)

ScaleAnimation scaleAnimation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f, 
                ScaleAnimation.RELATIVE_TO_SELF,1.0f,ScaleAnimation.RELATIVE_TO_SELF,0.0f);
scaleAnimation.setDuration(1000);
imageView.startAnimation(scaleAnimation);
  • 第一個參數(shù):起始時 X坐標(biāo)上的伸縮尺寸
  • 第二個參數(shù):結(jié)束時 X坐標(biāo)上的伸縮尺寸。
  • 第三個參數(shù):起始時 Y坐標(biāo)上的伸縮尺寸。
  • 第四個參數(shù):結(jié)束時 Y坐標(biāo)上的伸縮尺寸
  • 0.0f表示收縮到?jīng)]有
    1.0f表示正常無伸縮
    值小于1.0f表示收縮
    值大于1.0f表示放大
  • 第五個參數(shù):動畫在X軸相對于物件位置類型。
  • 第六個參數(shù):動畫相對于物件的X坐標(biāo)的開始位置
  • 第七個參數(shù):動畫在Y軸相對于物件位置類型。
  • 第八個參數(shù):動畫相對于物件的Y坐標(biāo)的開始位置。
  • 0.0f,0.0f 在左上角
    0.0f,1.0f 在左下角
    1.0f,1.0f 在右下角
    0.5f,0.5f 在中間

2. xml 方式實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <scale 
        android:duration="1000" 
        android:fillAfter="false" 
        android:fromXScale="0.0" 
        android:fromYScale="0.0" 
        android:toXScale="1.4" 
        android:toYScale="1.4"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotX="50%" 
        android:pivotY="50%"  />
</set>
Animation scaleAnimation=AnimationUtils.loadAnimation(this, R.anim.scale);  //加載Xml文件中的動畫
imageView.startAnimation(scaleAnimation);

三、translate:畫面轉(zhuǎn)換位置移動動畫效果


1. 代碼方式實現(xiàn)

TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);
translateAnimation.setDuration(1000);
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);
translateAnimation.setFillAfter(true);
imageView.startAnimation(translateAnimation);
  • 第一個參數(shù):動畫起始時 X坐標(biāo)上的移動位置
  • 第二個參數(shù):**動畫結(jié)束時 X坐標(biāo)上的移動位置 **。
  • 第三個參數(shù):**動畫起始時 Y坐標(biāo)上的移動位置 **。
  • 第四個參數(shù):動畫結(jié)束時 Y坐標(biāo)上的移動位置。

2. xml 方式實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="2000" 
        android:fromXDelta="30" 
        android:fromYDelta="30" 
        android:toXDelta="-80" 
        android:toYDelta="300" /> 
</set>
Animation translateAnimation=AnimationUtils.loadAnimation(this, R.anim.translate);  //加載Xml文件中的動畫
imageView.startAnimation(translateAnimation);

四、rotate:畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果


1. 代碼方式實現(xiàn)

RotateAnimation rotateAnimation=new RotateAnimation(0.0f,360.0f, 
            Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
imageView.startAnimation(rotateAnimation);
  • 第一個參數(shù):動畫起始時的旋轉(zhuǎn)角度
  • 第二個參數(shù):動畫旋轉(zhuǎn)到的角度
  • 第三個參數(shù):動畫在X軸相對于物件位置類型
  • 第四個參數(shù):動畫相對于物件的X坐標(biāo)的開始位置
  • 第五個參數(shù):動畫在Y軸相對于物件位置類型
  • 第六個參數(shù):動畫相對于物件的Y坐標(biāo)的開始位置

2. xml 方式實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <rotate 
        android:duration="3000" 
        android:fromDegrees="0" 
        android:toDegrees="350" 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotX="50%" 
        android:pivotY="50%" /> 
</set>
Animation rotateAnimation=AnimationUtils.loadAnimation(this, R.anim.rotate);  //加載Xml文件中的動畫
imageView.startAnimation(rotateAnimation);

五、通用方法


  1. setDuration(long durationMills)
    設(shè)置動畫持續(xù)時間(單位:毫秒)
  2. setFillAfter(Boolean fillAfter)
    如果fillAfter的值為true,則動畫執(zhí)行后,控件將停留在執(zhí)行結(jié)束的狀態(tài)
  3. setFillBefore(Boolean fillBefore)
    如果fillBefore的值為true,則動畫執(zhí)行后,控件將回到動畫執(zhí)行之前的狀態(tài)
  4. setStartOffSet(long startOffSet)
    設(shè)置動畫執(zhí)行之前的等待時間
  5. setRepeatCount(int repeatCount)
    設(shè)置動畫重復(fù)執(zhí)行的次數(shù)

六、Interpolator:動畫插入器


animation.setInterpolator(this, android.R.anim.cycle_interpolator);
animation.setInterpolator(new AccelerateInterpolator());

android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  1. AccelerateInterpolator
    加速,開始時慢中間加速
  2. DecelerateInterpolator
    減速,開始時快然后減速
  3. AccelerateDecelerateInterolator
    先加速后減速,開始結(jié)束時慢,中間加速
  4. AnticipateInterpolator
    反向,先向相反方向改變一段再加速播放
  5. AnticipateOvershootInterpolator
    反向加超越,先向相反方向改變,再加速播放,會超出目的值然后緩慢移動至目的值
  6. BounceInterpolator
    彈球效果,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
  7. CycleIinterpolator
    循環(huán),動畫循環(huán)一定次數(shù),值的改變?yōu)橐徽液瘮?shù):Math.sin(2 x mCycles x Math.PI x input)
  8. LinearInterpolator
    線性,線性均勻改變
  9. OvershottInterpolator
    超越,最后超出目的值然后緩慢改變到目的值

七、AnimationSet 實現(xiàn)多種動畫混合效果


  • 同時播放,也可以給不同動畫分別設(shè)置 setStartOffset() 開始偏移來順序播放。

1. 代碼方式實現(xiàn)

AnimationSet animationSet=new AnimationSet(true);

AlphaAnimation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha);
ScaleAnimation scaleAnimation=AnimationUtils.loadAnimation(this, R.anim.scale);
TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);
RotateAnimation rotateAnimation=new RotateAnimation(0.0f,360.0f, 
          Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF, 0.5f);

alphaAnimation.setStartOffset(0);
scaleAnimation.setStartOffset(2000);
translateAnimation.setStartOffset(4000);
rotateAnimation.setStartOffset(6000);

animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);

animationSet.setInterpolator(this, android.R.anim.anticipate_interpolator);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);

2. xml 方式實現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">
  
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="500"/>
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>
        ......
</set>    
AnimationSet animationSet=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.animset);

雖然可以通過代碼的方式定義動畫,但是Android官方還是建議在xml中定義動畫效果,這樣可做到最大程度上的解耦,方便項目的后期維護。

最后編輯于
?著作權(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)容