android動(dòng)畫詳解(一)

一般常用的android動(dòng)畫有View Animation(視圖動(dòng)畫)和Property Animation(屬性動(dòng)畫)。靈活運(yùn)用android提供的這些動(dòng)畫我們能夠做出絢麗的動(dòng)畫效果。

View Animation

在安卓的官方文檔中是這么定義視圖動(dòng)畫的:View Animation is the older system and can only be used for Views. It is relatively easy to setup and offers enough capabilities to meet many application's needs.這句話告訴了我們View Animation只能應(yīng)用于Views視圖中。如果想對(duì)非View執(zhí)行動(dòng)畫, you have to implement your own code to do so.它僅僅公開(kāi)了
? 透明度變化(AlphaAnimation)
? 縮放(ScaleAnimation)
? 位移(TranslateAnimation)
? 旋轉(zhuǎn)(RotateAnimation)
這四個(gè)動(dòng)畫。
下面就將一個(gè)個(gè)的來(lái)學(xué)習(xí)這四個(gè)不同的動(dòng)畫。

Translate Animation

位移動(dòng)畫,顧名思義,它是來(lái)操縱View的平行移動(dòng)的動(dòng)畫。我們可以通過(guò)XML和代碼這兩種方式來(lái)實(shí)現(xiàn)這個(gè)動(dòng)畫效果。動(dòng)畫定義文件應(yīng)該存放在 res/anim 文件夾下,訪問(wèn)時(shí)采用 R.anim.XXX.xml 的方式。
由于使用XML這種方式來(lái)實(shí)現(xiàn)動(dòng)畫更加的方便快捷,所以我們一般選用這種方式來(lái)實(shí)現(xiàn)View Animation。
首先我們創(chuàng)建一個(gè)名為transltate.xml的文件。

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="200"
    android:toYDelta="200">
</translate>

android:fromXDelta="0" 表示X的起始值,這里的值是相對(duì)于我們將要執(zhí)行動(dòng)畫的view的。
android:toXDelta="200" 表示X的結(jié)束值
然后再代碼中點(diǎn)擊按鈕實(shí)現(xiàn)啟動(dòng)動(dòng)畫:

  final Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
  translate.setDuration(1000);
  translate.setFillAfter(true);
  sample.startAnimation(translate);

動(dòng)畫效果如下:


這里寫圖片描述

這里點(diǎn)擊sample會(huì)有一個(gè)Toast彈出,表示被點(diǎn)中了,可是從視頻的點(diǎn)擊事件看出,當(dāng)執(zhí)行完位移動(dòng)畫后,點(diǎn)擊sample將沒(méi)有彈窗了,相反點(diǎn)擊sample位移前的空白位置卻有Toast彈出,這就說(shuō)明了再View Animation中,動(dòng)畫并沒(méi)有改變View的真實(shí)位置。

Alpha Animation

透明度動(dòng)畫,它可以改變View的透明度。
仍然在anmi文件夾下創(chuàng)建alpha.xml文件

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0">
</alpha>

這里面的內(nèi)容很簡(jiǎn)單
android:fromAlpha="1" 代表View的起始透明度
android:toAlpha="0" 代表View的結(jié)束時(shí)透明度
在代碼中的調(diào)用過(guò)程與位移動(dòng)畫完全一致,這里就不在贅述。
動(dòng)畫執(zhí)行效果如下:

這里寫圖片描述

Rotate Animation

旋轉(zhuǎn)動(dòng)畫,使View產(chǎn)生旋轉(zhuǎn)的效果。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:pivotX="50%"
    android:pivotY="50%"
    >
</rotate>

android:fromDegrees="0" 旋轉(zhuǎn)的起始角度
android:toDegrees="180" 旋轉(zhuǎn)的結(jié)束角度
android:pivotX="50%" 旋轉(zhuǎn)的軸點(diǎn)的X軸坐標(biāo),可以用百分比表示
android:pivotY="50%" 旋轉(zhuǎn)的軸點(diǎn)的Y軸坐標(biāo)
動(dòng)畫的執(zhí)行效果如下:


這里寫圖片描述

Scale Animation

縮放動(dòng)畫,使View能實(shí)現(xiàn)放大和縮小的效果,

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1"
    android:toXScale="1.5"
    android:fromYScale="1"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    >
</scale>

android:fromXScale="1" 水平方向縮放的起始值
android:toXScale="1.5" 水平方向縮放的結(jié)束值
android:fromYScale="1" 豎直方向縮放的起始值
android:toYScale="1.5" 豎直方向縮放的結(jié)束值
android:pivotX="50%" 縮放軸點(diǎn)的X坐標(biāo)
android:pivotY="50%" 縮放軸點(diǎn)的Y坐標(biāo)
動(dòng)畫執(zhí)行效果如下:


這里寫圖片描述

視圖動(dòng)畫中的四種最基本用法已經(jīng)介紹完畢,使用起來(lái)還是很簡(jiǎn)單方便的。
出了上面介紹的屬性外,視圖動(dòng)畫還有一些常用的屬性,如下所示:
android:duration 動(dòng)畫的持續(xù)時(shí)間
android:fillAfter 動(dòng)畫結(jié)束以后View是否停留在結(jié)束位置,true表示停留在結(jié)束位置,false表示不停留同時(shí)也是默認(rèn)值。


LayoutAnimation

出了這四種形式外,View動(dòng)畫還可以實(shí)現(xiàn)一些特殊的場(chǎng)景,比如在ViewGroup中可以控制子元素的出場(chǎng)動(dòng)畫。一般在ListView中通過(guò)定義LayoutAnimation來(lái)實(shí)現(xiàn)。
-首先anim文件夾中定義LayoutAnimation

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="normal"
    android:animation="@anim/left_side_anim"
    >
</layoutAnimation>

android:delay="0.5" 這個(gè)屬性表示每一個(gè)條目延遲執(zhí)行的時(shí)間,假設(shè)動(dòng)畫執(zhí)行時(shí)間設(shè)置為100ms,則第一個(gè)條目將在150ms時(shí)執(zhí)行動(dòng)畫,以此類推,第二條目將在200ms時(shí)執(zhí)行動(dòng)畫。
android:animationOrder="normal" 這個(gè)是動(dòng)畫執(zhí)行的模式,有normal、reverse、random,依次是順序執(zhí)行、逆序執(zhí)行和隨機(jī)執(zhí)行
android:animation="@anim/left_side_anim" 這個(gè)屬性設(shè)置的是執(zhí)行什么樣的動(dòng)畫,事例代碼如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">
<translate android:fromXDelta="-50%" android:toXDelta="0"></translate>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"></alpha>
</set>

這段代碼相信很容易理解,就是執(zhí)行了平移和透明度動(dòng)畫,然后將listview顯示出來(lái)即可:

final ListView listView = (ListView) findViewById(R.id.lv);
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Cheeses.NAMES));

動(dòng)畫效果如下:


這里寫圖片描述

總結(jié)

視圖動(dòng)畫雖然能實(shí)現(xiàn)一些基本的動(dòng)畫效果,但是這些仍然遠(yuǎn)遠(yuǎn)不夠,所以Google在android3.0中加入了屬性動(dòng)畫。

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