Android動(dòng)畫合集之補(bǔ)間動(dòng)畫

補(bǔ)間動(dòng)畫分類:

TranslateAnimation(位移動(dòng)畫)
RotateAnimation(旋轉(zhuǎn)動(dòng)畫)
ScaleAnimation(縮放動(dòng)畫)
AlphaAnimation(透明度漸變)
AnimationSet(組合漸變)

1.位移動(dòng)畫

TranslateAnimation animation =new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1f,Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1f);
        animation.setDuration(2000);
        animation.setInterpolator(this, android.R.anim.linear_interpolator);
        img.startAnimation(animation);
        animation.start();

2.旋轉(zhuǎn)動(dòng)畫

RotateAnimation animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);
        animation.setDuration(5000);
        animation.setInterpolator(this, android.R.anim.accelerate_interpolator);
        img.startAnimation(animation);
        animation.start();

3.縮放動(dòng)畫

        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
        animator.setDuration(6000L);//設(shè)置縮放時(shí)間
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue();
                img.setScaleX(scale);
                img.setScaleY(scale);
            }
        });
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

4.透明動(dòng)畫

                AlphaAnimation animation = new AlphaAnimation(1, 0);
                animation.setDuration(2000);
                animation.setRepeatCount(-1);
                img.startAnimation(animation);
                animation.start();

5.組合漸變

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="5000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <!--fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例-->
    <!--toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例-->
    <!--pivotX/pivotY:縮放的中軸點(diǎn)X/Y坐標(biāo),即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點(diǎn)-->


    <rotate
        android:duration="5000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <!--fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度-->
    <!--repeatCount:旋轉(zhuǎn)的次數(shù),默認(rèn)值為0,代表一次,假如是其他值,
    比如3,則旋轉(zhuǎn)4次 另外,值為-1或者infinite時(shí),表示動(dòng)畫永不停止-->
    <!--repeatMode:設(shè)置重復(fù)模式,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時(shí)才有效。
    還可以設(shè)置成reverse,表示偶數(shù)次顯示動(dòng)畫時(shí)會(huì)做方向相反的運(yùn)動(dòng)!-->


    <translate
        android:duration="5000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="0" />

    <!--fromXDelta/fromYDelta:動(dòng)畫起始位置的X/Y坐標(biāo)-->
    <!--toXDelta/toYDelta:動(dòng)畫結(jié)束位置的X/Y坐標(biāo)-->


    <alpha
        android:duration="5000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

    <!--duration :時(shí)間-->
    <!--fromAlpha :起始透明度-->
    <!--toAlpha:結(jié)束透明度-->
</set>
       
       //在MainActivity中的代碼
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
        img.startAnimation(animation);

參考文檔:http://www.runoob.com/w3cnote/android-tutorial-alphaanimation.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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