Last update
Last update

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法

先看幾種簡單的補間動畫效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

上面一共顯示了五個效果:平移、透明、縮放、旋轉和旋轉+透明

代碼也是比較簡單的,以平移群組合效果簡單講解:

1.資源檔案:

補間動畫的檔案都是放在res下的anmi檔案夾内,沒有的話要自己建立

(1)平移的translate_anim.xml

往同時往右邊和下邊方向移動100個像素

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

(2)組合的all_anim.xml

透明效果:從完全顯示到很透明

旋轉效果:逆時針旋轉650度

<?xml version="1.0" encoding="utf-8"?>
<set>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromAlpha="1.0"
           android:toAlpha="0.1"
           android:duration="3000"
           android:fillBefore="true"/>

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="0"
            android:toDegrees="-650"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="3000"
            android:fillAfter="true"/>
</set>
           

這裡的組個可以放置多個,這裡隻是放置兩個動畫透明和旋轉做示例。

2.java控制的主要代碼

ImageView iv;
  Animation animation;


    /**
     * 平移動畫
     */
    public void translate(View view) {
        animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
        iv.startAnimation(animation);
    }

  /**
     * 各種效果集于一身的動畫
     */
    public void all(View view) {
        setIVAnimation(R.anim.all_anim);
    }

  /**
     * 傳入動畫的ID,直接給ImageView設定動畫
     */
    private void setIVAnimation(int animID) {
        iv.startAnimation(AnimationUtils.loadAnimation(this, animID));
    }

           

上面平移動畫也是可以直接調用那個私有方法來調用動畫。

下面是補間動畫和各種屬性的詳細講解。

一.基本知識

補間動畫就是通過對場景的對象不斷進行圖像變化來産生動畫效果,在實作補間動畫時,隻要定義動畫開始和結束的”關鍵幀”其它過度幀由系統自動計算并補齊.android中提供了4種補間動畫效果:透明、旋轉、縮放、平移。

1.android補間動畫的四種類别

alpha 漸變透明度動畫效果
translate 畫面轉換位置移動動畫效果
scale 漸變尺寸伸縮動畫效果
rotate 畫面轉移旋轉動畫效果

上面這四個類别名稱是可以在xml中當作根标簽使用,來對應各種補間動畫。

當然也是可以用set做根标簽,裡面放幾個補間動畫,做組合效果。

2.關于補間動畫的類

Animation有五個子類:AlphaAnimation (透明動畫)TranslateAnimation (平移動畫)、ScaleAnimation (縮放動畫)、RotateAnimation (旋轉動畫)、AnimationSet (組合動畫)

2.動畫檔案存放位置

補間動畫定義檔案應該存放在res/anim檔案夾下,通路時采用R.anim.XXX.xml的方式,位置如圖:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

3.根據xml檔案擷取動畫類的對象,并設定動畫

//擷取動畫對象
 Animation  animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
//你也可以擷取具體的動畫對象
     AlphaAnimation  animation = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
//給控件設定動畫
  imageView.startAnimation(animation);

//這裡控件不僅僅是ImageView,也可以是TextView,或是一個布局或一個自定義View都是可以的!
           

4.各個補間動畫可以設定的通用屬性(Animation類的屬性)

下面這些屬性在各個補間動畫效果内都是通用的,并且表示的意義也是一樣的。

(1)android:interpolator 插值器

用于控制動畫的變化速度,使用動畫效果可以勻速,先加速再減速,先減速後加速或抛物線速度等各種速度變化,後面會有文章詳解

(2)android:repeatMode

用于設定動畫的重複方式,可選擇為reverse(反向)或restart(重新開始)

比如放大動畫,圖像變化剛開始從小到大,第二次會從大變小,後面一直重複。。。

(3)android:repeatCount

用于設定動畫的重複次數,屬性可以是代表次數的數值,也可以是infinite(無限循環)

預設次數為0,如果設定次數為1,動畫會執行2次,以此類推。。。

(4)android:duration

用于指定動畫持續的時間,機關為毫秒

如果你的動畫時間沒有設定,動畫就是在一瞬間完成的,比如從圖像從正常到很透明,就想換了一個圖檔的感覺,沒有過程。

(5)android:fillBefore

如果設定為true,控件動畫結束時,還原到開始動畫前的狀态

比如,透明動畫,從透明到完全顯示,動畫結束後,圖像會顯示動畫沒開始前的樣子。

(6)android:fillEnabled

與android:fillBefore 效果相同,都是在動畫結束時,将控件還原到初始化狀态

(7)android:fillAfter

如果設定為true,控件動畫結束時,将保持動畫最後時的狀态

這個屬性如果設定了為true,fillBefore也設定為true,動畫結束後,圖像是不會恢複的,就是以fillAfter為準。

如果fillAfter屬性設定為false,不管fillBefore設定為true還是false,圖像最終都是會恢複動畫前的樣子!

二.透明動畫:alpha

圖像大小不變,動畫實作圖像從某一個透明度的值變成另一個透明度的值。透明度的值從0到1的浮點數,0為透明,1為完全顯示。

xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
       android:fromAlpha="1.0"        //設定開始時的透明度,這裡表示完全可見
       android:toAlpha="0.1"          //設定動畫結束時的透明度,很透明
       android:duration="3000"        //動畫持續時間
       android:fillBefore="true"      //是否是保持開始的樣子(完全可見),預設為true
       android:fillAfter="true"       //是否儲存結束時的透明度,預設為false
/>   

           

解析:

透明動畫的屬性設定

1. android:fromAlpha

用于指定動畫開始時的透明度,值為0.0代表完全透明 ,值為1.0代表完全不透明

2. android:toAlpha

用于指定動畫結束時的透明度,值為0.0代表完全透明,值為1.0代表完全不透明

其他

注意上面屬性fillBefore和fillBefore如果都是true,以fillAfter屬性為主,即圖像保持動畫最後的效果。

如果上面兩個屬性都是false,最後圖像還是會顯示動畫開始時的樣子。

上面這段代碼運作的效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

重複3次的效果:

從不太顯示到完全顯示三次效果。把重複的次數設定為2,即可。

設定:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromAlpha="0.1"
       android:toAlpha="1"
       android:duration="3000"
       android:fillAfter="false"
       android:fillBefore="false"
       android:repeatCount="2"
        />
           

效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

三.平移動畫:translate

圖像大小不變,通過為圖像指定開始時的位置,結束時的位置,經及持續時間來建立動畫效果。

xml代碼

從左上角往右下角平移

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromXDelta="0"   //x軸開始的位置
           android:toXDelta="100"   //往右滑動100個像素點
           android:fromYDelta="0"   //y軸開始的位置
           android:toYDelta="100"   //往下移動100個像素點
           android:duration="2000"  //時間2000毫秒
           android:fillBefore="true"//動畫結束後,回到原來的位置
/>
           

解析:

1. android:fromXDelta

用于指定動畫開始時水準方向上的起始位置,預設機關是像素,可以使用100%代表的是自身長度,100%p代表的是父框體的長度。50%就是表示一半的距離。下面屬性的距離也可以這樣表示。

2. android:toXDelta

用于指定動畫結束時水準方向上的位置

3. android:fromYDelta

用于指定動畫開始時垂直方向上的位置

4. android:toYDelta

用于指定動畫結束時垂直方向上的位置

從左往右的平移:

剛好移出螢幕

設定:

android:fromXDelta="0"   //x軸開始的位置
           android:toXDelta="100%p" //往右滑動一個父框體的距離
           android:fromYDelta="0"   //y軸開始的位置
           android:toYDelta="0"     //不往下移動
           android:duration="2000"  //時間2000毫秒
           android:fillBefore="true"//動畫結束後,回到原來的位置
           

效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

圖解

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

從上下的平移:

設定:

android:fromXDelta="100"
           android:toXDelta="100"
           android:fromYDelta="0"
           android:toYDelta="200"
           android:duration="2000"
           

效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

可以看到在距離y軸100像素的地方,從上往下移動200像素的效果。

從右下角往右上角平移:

設定:

““

android:fromXDelta=”80%p”

android:toXDelta=”0”

android:fromYDelta=”80%p”

android:toYDelta=”0”

####效果:
![7](http://i.imgur.com/noGUwnj.gif)

##四.縮放動畫:scale
改變圖檔大小,動畫指定開始時縮放系數,結束時的縮放系數,以及持續時間來他建動畫,在縮放時還可以通過指定軸心點坐标來改變綻放的中心。
###1.xml代碼
           
###2.解析
####(1)android:fromXScale 用于指定動畫開始時水準方向上的縮放系數,浮點數,值0表示無,值為1.0表示不變化 ,值為2.0表示放大一倍 。。。以此類推
####(2)android:toXScale 用于指定動畫結束時水準方向上的縮放系數。
####(3)android:fromYScale 用于指定動畫開始時垂直方向上的縮放系數。
####(4)android:toYScale 用于指定動畫結束時垂直方向上的縮放系數。
####(5)android:pivotX 用于指定軸心點X軸坐标,200表示距離y軸200個像素點的距離,50%表示自身的x軸中心點的,50%p表示父框體的x軸中心點 
####(6)android:pivotY 用于指定軸心點Y軸坐标,200表示距離x軸200個像素點的距離,50%表示自身的y軸中心點的,50%p表示父框體的y軸中心點 

###上面代碼運作的效果:
![9](http://i.imgur.com/OftxOrM.gif)
可以看到圖像是由無變到有,并且是(200,200)這個點開始做放大,最後到1.4倍後停下,并保持最後的樣子。

###從0變大到3倍效果:
####設定代碼:
設定在自身位置中心開始放大
           
android:fromXScale="0.0"
   android:toXScale="3"
   android:fromYScale="0.0"
   android:toYScale="3"
   android:pivotX="50%"
   android:pivotY="50%"
           
####效果:
![9](http://i.imgur.com/nX5jY85.gif)

###在父窗體中心位置放大到0.5倍

####設定代碼:
           
android:fromXScale="0.0"
   android:toXScale="0.5"
   android:fromYScale="0.0"
   android:toYScale="0.5"
   android:pivotX="50%p"
   android:pivotY="50%p"
           
這個比較坑的就是它會向你圖像本來的位置移動!這裡會從中心點向原點方向移動。
####效果:
![10](http://i.imgur.com/NJoQssJ.gif)
這裡可以看到圖像向原來的方向移動一半,如果設定放大的倍率大于1,就會發現圖像移動到螢幕外面去了!如果設定x和y的倍率剛好是1,圖像放大後,剛好到原點的位置。
要想圖像在中心點放大或縮小,就要把圖像位置定在中心點,然後在自身的中心點,進行放大或縮小。



##五.旋轉動畫:rotate
旋轉動畫就是通過為動畫指定開始時的旋轉角度,結束時的旋轉角度,經及持續時間來建立動畫,在旋轉時還可以通過指定軸心點坐标來改為旋轉的中心。 

###1.xml代碼
           
###2.解析
####(1)android:fromDegrees 用于指定動畫開始時旋轉的角度數,正負任意大小的浮點數,正的表示順時針,負的表示逆時針 
####(2) android:toDeggrees 用于指定動畫結束時旋轉的角度 數,正負任意大小的浮點數,正的表示順時針,負的表示逆時針 
####(3) android:pivotX 用于指定軸心點X軸坐标,20表示距離y軸20個像素點的距離,50%表示自身的x軸中心點的,50%p表示父框體的x軸中心點 
####(4) android:pivotY 用于指定軸心點Y軸坐标 ,20表示距離x軸20個像素點的距離,50%表示自身的y軸中心點的,50%p表示父框體的y軸中心點 

###上面代碼顯示的效果:

![12](http://i.imgur.com/uwzQevC.gif)
可以看到圖像,以自身為中心旋轉90度後停下來。

###圖檔以父窗體的40%的一個中心點旋轉180度
####代碼:
           
android:fromDegrees="0"
    android:toDegrees="180"
    android:pivotX="40%p"
    android:pivotY="40%p"
    android:duration="3000"
           
####效果:
![12](http://i.imgur.com/mwXdSMv.gif)
可以看到圖檔旋轉了180,但是旋轉到了螢幕右下角
圖解:
![13](http://i.imgur.com/b8C07KM.png)
###如果把最終角度換成360度,效果:
 語句:android:toDegrees="360"
![14](http://i.imgur.com/csJTwZA.gif)
角度可以任意指定,旋轉720度,就會顯示旋轉兩圈效果!

這裡的pivotX和pivotY,我剛開始是以為圖像在這個點旋轉,但是實際上是以這個點為中心旋轉,半徑就是圖像原點到這個中心點之間的距離!

##六.組合動畫:set
組合動畫就是可以把多種動畫放在一起顯示。可以是單獨一個,也可以是多個東西一起顯示。
###1.xml代碼
           

2.解析

組合動畫,各個動畫的屬性還是不變

set标簽也是可以設定通用的那些屬性。

但是如果通用屬性在set标簽中設定,在其他動畫也設定,會有什麼效果呢?

根據之前Android的其他知識,我們知道裡面的标簽屬性會覆寫掉外面的标簽屬性,

但是這個補間動畫的卻不是,通用屬性都是以外邊set标簽中設定為準,不然可以看看上面代碼運作的效果,

上面代碼從左到右移動要5秒,裡面标簽設定旋轉時間2秒,但是實際效果是旋轉5秒時間

同樣,fillAfter等等這些通用屬性都是以set标簽為準的。

上面代碼的效果:

走穿Android動畫---補間動畫alpha、scale、translate、rotate、set的xml屬性及用法走穿Android動畫—補間動畫alpha、scale、translate、rotate、set的xml屬性及用法下面是補間動畫和各種屬性的詳細講解。共勉:成功是失敗走剩的路。

關于xml使用補間動畫,這裡已經詳細介紹了各方面的知識,但是插值器還沒有說,這個會在後面的文章介紹。

插值器,其實就是可以讓動畫顯示效果有點不同,比如一個平移過程,前期勻速,到中間加速到最後。

動畫還是原來動畫,隻是顯示的速率會發生改變!

共勉:成功是失敗走剩的路。

失敗是什麼?沒有什麼,隻是走向成功更近一步;怎麼走向成功呢?當你走穿所有失敗的路,剩下的那條路必定是成功的路,但是也不一定是每條路都走到底,有時候看穿也可以的。

Shaodong City held the 6th Mid-Autumn Festival Music and Poetry Festival Nearly 1,000 people enjoyed the audiovisual feast

Red Net Moment, October 1 (Correspondent Wei Xiaoqing Zhang Yibin) On the evening of September 29, Shaodong City held the sixth Mid-Autumn Festival Music and Poetry Festival, where more than 800 poets and poetry lovers from Hunan and Beijing, Inner Mongolia, Anhui, Jiangsu and other places enjoyed an audiovisual feast. Present at the meeting were Wang Bing, deputy editor-in-chief of the Poetry Journal of the China Writers Association; You Heping, deputy secretary of the party leading group and full-time vice chairman of the Hunan Writers Association; Liang Eryuan, president of the Hunan Poetry Society; Huang Bin, editor-in-chief of Hunan Literature; Wang Zhuhai, director of the Theoretical Research Center of the Hunan Provincial Federation of Literary and Art Circles and executive editor of Xiangjiang Literature and Art; Shu Lechuan, vice chairman of the Inner Mongolia Writers Association; and other famous figures in the literary and art circles; and Leaders of Shaodong City, Shen Guirong, Liu Yongge, Guo Jingcheng, and Huang Deyuan.

Shaodong City held the 6th Mid-Autumn Festival Music and Poetry Festival Nearly 1,000 people enjoyed the audiovisual feast

The scene of the event.

Under the specific guidance of the Hunan Poetry Society, the Shaoyang Municipal Literary Association, and the Propaganda Department of the Shaodong Municipal CPC Committee, the Shaodong Mid-Autumn Festival Music and Poetry Festival was created by the Shaodong Municipal Literary Association, the Municipal Writers Association, and the Municipal Poetry Society, and has been held for six consecutive sessions so far, becoming a well-known cultural business card inside and outside the province.

Shaodong City held the 6th Mid-Autumn Festival Music and Poetry Festival Nearly 1,000 people enjoyed the audiovisual feast

Revolutionary modern Peking Opera "Cuckoo Mountain" excerpt performance.

The theme of this year's Mid-Autumn Festival Music and Poetry Festival is "Ode to the Motherland, Hometown Feelings". The whole poem will be divided into six chapters: "personable", "red flag fluttering", "majestic hunting", "nostalgia", "moonlight dissolving", and "ancient rhyme and leisurely". The poetry meeting kicked off in the group recitation of Mao Zedong's poems "Qinyuan Spring Changsha", "Seven Laws , Long March", "Seven Laws • People's Liberation Army Occupation of Nanjing", "Water Tune Song Head , Heavy On Jinggangshan", Luo Luming, Chen Xinwen, David, Fu Li and other famous artists successively recited their own poems, and then read more than 30 poems created by local poets in Shaodong, during which they were also interspersed with dance, song singing, Peking Opera excerpts, live paintings, live calligraphy and other literary and artistic programs, and colorful programs made the music and poetry club wonderful.

Liang Eryuan, president of the Hunan Poetry Society, fully affirmed the practical significance and far-reaching impact of Shao Dong's holding of six consecutive Mid-Autumn Festival music and poetry festivals on practicing truth, benevolence and beauty, praising the new era, carrying forward positive energy, building a strong poetry city, and promoting poetry prosperity. He hoped that Shaodong Mid-Autumn Festival Music and Poetry would continue to make this cultural brand bigger and stronger.

Wang Bing, deputy editor-in-chief of Poetry Journal, announced the start of the 2020 Shaodong City "Ode to the Motherland , Hometown Feelings" Mid-Autumn Festival Music and Poetry Festival.

Shaodong City held the 6th Mid-Autumn Festival Music and Poetry Festival Nearly 1,000 people enjoyed the audiovisual feast

Dance "My Motherland and Me".

Shaodong Municipal Federation of Literature and Literature to "build a strong poetry city" as the goal, rooted in the soil of life, in line with the call of the times, the poetry creation team is increasingly large, poetry creation achievements are remarkable, and gradually emerged a "Shaodong poetry group" that has an initial influence inside and outside the province, the city's poetry creators more than 1,000 people, the existing provincial poetry society members 82 people. "Shaoyang Daily", "Shaoyang Evening News" and "Liaohe" magazine have successively launched special editions and albums of "Shaodong Poets". In particular, since the beginning of this year, the provincial poetry society journal "Poetry World" has launched albums of works by 39 Shaodong poets at one time; "Anhui Poets" and "Hunan Poets" have launched special editions and albums of Shaodong poets respectively; a large number of member works have been launched in national and provincial key publications such as "Chinese Writers", "Beijing Literature", "Poetry Journal", "Stars", "Poetry Tide", "Selected Small Novels", "Hunan Literature", "Xiangjiang Literature and Art", etc.