国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

定義控件三部曲之動(dòng)畫篇(三)—— 代碼生成alpha、scale、translate、rotate、set及插值器動(dòng)畫

2019-11-06 09:37:49
字體:
供稿:網(wǎng)友

  轉(zhuǎn):定義控件三部曲之動(dòng)畫篇(三)—— 代碼生成alpha、scale、translate、rotate、set及插值器動(dòng)畫

前言:我相信信念的力量,只要每天進(jìn)步,總有一天,會(huì)與眾不同。轉(zhuǎn)到JACA以后,發(fā)現(xiàn)真的有好多知識(shí)要補(bǔ)充,不再像c++那樣,只要深入學(xué)習(xí)就好,這里沒有了深入,卻要求知識(shí)面很廣范。先把Android的知識(shí)補(bǔ)充完以后,再看android的書籍,最后深入學(xué)習(xí)java,兩年的時(shí)候應(yīng)該來得急,努力。

不登高山,不知天之高;不臨深溪,不知地之厚

相關(guān)文章:

《Android自定義控件三部曲文章索引》

一、概述

前兩篇,我為大家講述了利用xml來定義動(dòng)畫及插值器,但在代碼中,我們常常是動(dòng)態(tài)生成動(dòng)畫的,所以,這篇將為大家講述如何用代碼生成動(dòng)態(tài)生成動(dòng)畫及插值器。

先簡(jiǎn)單寫出各個(gè)標(biāo)簽對(duì)應(yīng)的類,方便大家理解:

scale —— ScaleAnimationalpha —— AlphaAnimationrotate —— RotateAnimationtranslate —— TranslateAnimationset —— AnimationSet

二、Animation公共類

官方SDK講解頁(yè)面為:《Animation》

第一篇中我們提到過,Animation類是所有動(dòng)畫(scale、alpha、translate、rotate)的基類,它所具有的標(biāo)簽及對(duì)應(yīng)函數(shù)為:

android:duration                  setDuration(long) 動(dòng)畫持續(xù)時(shí)間,以毫秒為單位 android:fillAfter                    setFillAfter(boolean)如果設(shè)置為true,控件動(dòng)畫結(jié)束時(shí),將保持動(dòng)畫最后時(shí)的狀態(tài)android:fillBefore                 setFillBefore(boolean)如果設(shè)置為true,控件動(dòng)畫結(jié)束時(shí),還原到開始動(dòng)畫前的狀態(tài)android:fillEnabled              setFillEnabled(boolean)與android:fillBefore 效果相同,都是在動(dòng)畫結(jié)束時(shí),將控件還原到初始化狀態(tài)android:repeatCount           setRepeatCount(int)重復(fù)次數(shù)android:repeatMode            setRepeatMode(int)重復(fù)類型,有reverse和restart兩個(gè)值,取值為RESTART或 REVERSE,必須與repeatCount一起使用才能看到效果。因?yàn)檫@里的意義是重復(fù)的類型,即回放時(shí)的動(dòng)作。android:interpolator            setInterpolator(Interpolator) 設(shè)定插值器,其實(shí)就是指定的動(dòng)作效果,比如彈跳效果等

在第一篇《 Animation 動(dòng)畫詳解(一)——alpha、scale、translate、rotate、set的xml屬性及用法》 我們已經(jīng)講解了每個(gè)標(biāo)簽具體所具有的功能,這里就不再細(xì)講,對(duì)于使用方法會(huì)在下面的各標(biāo)簽中使用。

三、ScaleAnimation

這是scale標(biāo)簽對(duì)應(yīng)的類,官方SDK頁(yè)面為:《ScaleAnimation》

在Scale標(biāo)簽中,我們提到過它的自有屬性有下面幾條,先列一下:

android:fromXScale    起始的X方向上相對(duì)自身的縮放比例,浮點(diǎn)值,比如1.0代表自身無變化,0.5代表起始時(shí)縮小一倍,2.0代表放大一倍;android:toXScale        結(jié)尾的X方向上相對(duì)自身的縮放比例,浮點(diǎn)值;android:fromYScale    起始的Y方向上相對(duì)自身的縮放比例,浮點(diǎn)值,android:toYScale        結(jié)尾的Y方向上相對(duì)自身的縮放比例,浮點(diǎn)值;android:pivotX            縮放起點(diǎn)X軸坐標(biāo),可以是數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p 三種樣式,比如 50、50%、50%p,當(dāng)為數(shù)值時(shí),表示在當(dāng)前View的左上角,即原點(diǎn)處加上50px,做為起始縮放點(diǎn);如果是50%,表示在當(dāng)前控件的左上角加上自己寬度的50%做為起始點(diǎn);如果是50%p,那么就是表示在當(dāng)前的左上角加上父控件寬度的50%做為起始點(diǎn)x軸坐標(biāo)。(具體意義,后面會(huì)舉例演示)android:pivotY           縮放起點(diǎn)Y軸坐標(biāo),取值及意義跟android:pivotX一樣。放到代碼中,ScaleAnimation有下面幾個(gè)構(gòu)造函數(shù):ScaleAnimation(Context context, AttributeSet attrs)  從XML文件加載動(dòng)畫,基本用不到ScaleAnimation(float fromX, float toX, float fromY, float toY)ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)第一個(gè)構(gòu)造函數(shù)是從本地XML文件加載動(dòng)畫,基本用不到的,我們主要看下面三個(gè)構(gòu)造函數(shù)。

在標(biāo)簽屬性android:pivotX中有三種取值,數(shù),百分?jǐn)?shù),百分?jǐn)?shù)p;體現(xiàn)在構(gòu)造函數(shù)中,就是最后一個(gè)構(gòu)造函數(shù)的pivotXType,它的取值有三個(gè),Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;

這三個(gè)構(gòu)造函數(shù)難度不大,就不再細(xì)講,舉個(gè)例子說明:

在第一篇中Scale的例子的XML代碼為:

[html] view plain copy 在CODE上查看代碼片<?xml version="1.0" encoding="utf-8"?>  <scale xmlns:android="http://schemas.android.com/apk/res/android"      android:fromXScale="0.0"      android:toXScale="1.4"      android:fromYScale="0.0"      android:toYScale="1.4"      android:pivotX="50"      android:pivotY="50"      android:duration="700" />  

對(duì)應(yīng)的代碼構(gòu)造代碼為:

[java]%20view%20plain%20copy%20scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  scaleAnim.setDuration(700);  在控件使用的時(shí)候,同樣是使用:[java]%20view%20plain%20copy%20tv.startAnimation(scaleAnim);  

四、AlphaAnimation

這是alpha標(biāo)簽對(duì)就的類,官方SDK文檔地址是:《AlphaAnimation》同樣alpha標(biāo)簽自有的屬性有:android:fromAlpha  %20動(dòng)畫開始的透明度,從0.0%20--1.0%20,0.0表示全透明,1.0表示完全不透明android:toAlpha%20 %20   %20動(dòng)畫結(jié)束時(shí)的透明度,也是從0.0%20--1.0%20,0.0表示全透明,1.0表示完全不透明所對(duì)應(yīng)的構(gòu)造函數(shù)為:AlphaAnimation(Context%20context,%20AttributeSet%20attrs)%20 同樣,從本地XML加載動(dòng)畫,基本不用AlphaAnimation(float%20fromAlpha,%20float%20toAlpha)這里只剩最后一個(gè)構(gòu)造函數(shù),難度不大,下面舉個(gè)例子說明下用法。

在第一篇文章中,我們構(gòu)造的XML代碼為:

[java]%20view%20plain%20copy%20<?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">  </alpha>  如果用代碼構(gòu)造同樣的效果,它所對(duì)應(yīng)的代碼為:[java]%20view%20plain%20copy%20alphaAnim = new AlphaAnimation(1.0f,0.1f);  alphaAnim.setDuration(3000);  alphaAnim.setFillBefore(true);  

五、RotateAnimation

RotateAnimation類對(duì)應(yīng)Rotate標(biāo)簽,SDK文檔地址:《RotateAnimation》

Rotate標(biāo)簽所具有的XML屬性有:

android:fromDegrees  %20 %20開始旋轉(zhuǎn)的角度位置,正值代表順時(shí)針方向度數(shù),負(fù)值代碼逆時(shí)針方向度數(shù)android:toDegrees  %20 %20 %20 %20結(jié)束時(shí)旋轉(zhuǎn)到的角度位置,正值代表順時(shí)針方向度數(shù),負(fù)值代碼逆時(shí)針方向度數(shù)android:pivotX  %20 %20 %20 %20 %20 %20 %20縮放起點(diǎn)X軸坐標(biāo),可以是數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p%20三種樣式,比如%2050、50%、50%p,具體意義已在scale標(biāo)簽中講述,這里就不再重講android:pivotY  %20 %20 %20 %20 %20 %20 %20縮放起點(diǎn)Y軸坐標(biāo),可以是數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p%20三種樣式,比如%2050、50%、50%p對(duì)應(yīng)的構(gòu)造函數(shù)有:RotateAnimation(Context%20context,%20AttributeSet%20attrs)  從本地XML文檔加載動(dòng)畫,同樣,基本不用RotateAnimation(float%20fromDegrees,%20float%20toDegrees)RotateAnimation(float%20fromDegrees,%20float%20toDegrees,%20float%20pivotX,%20float%20pivotY)RotateAnimation(float%20fromDegrees,%20float%20toDegrees,%20int%20pivotXType,%20float%20pivotXValue,%20int%20pivotYType,%20float%20pivotYValue)

RotateAnimation跟ScaleAnimation差不多,關(guān)鍵問題同樣是pivotXType和pivotYType的選擇,同樣有三個(gè)取值:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;

根據(jù)每一篇中的XML寫出對(duì)應(yīng)的JAVA構(gòu)造代碼:

XML為:

[java]%20view%20plain%20copy%20<?xml version="1.0" encoding="utf-8"?>  <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">        </rotate>  對(duì)應(yīng)JAVA構(gòu)造代碼為:[java]%20view%20plain%20copy%20rotateAnim = new RotateAnimation(0, -650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  rotateAnim.setDuration(3000);  rotateAnim.setFillAfter(true);  

六、TranslateAnimation

很顯示TranslateAnimation類對(duì)應(yīng)translate標(biāo)簽,它的SDK官方文檔地址為:《TranslateAnimation》

translate標(biāo)簽所具有的屬性為:

android:fromXDelta  %20 %20起始點(diǎn)X軸坐標(biāo),可以是數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p%20三種樣式,比如%2050、50%、50%p,具體意義已在scale標(biāo)簽中講述,這里就不再重講android:fromYDelta  %20 起始點(diǎn)Y軸從標(biāo),可以是數(shù)值、百分?jǐn)?shù)、百分?jǐn)?shù)p%20三種樣式;android:toXDelta%20   %20 %20 %20結(jié)束點(diǎn)X軸坐標(biāo)android:toYDelta  %20 %20 %20 結(jié)束點(diǎn)Y軸坐標(biāo)這些屬性所對(duì)應(yīng)的構(gòu)造函數(shù)為:TranslateAnimation(Context%20context,%20AttributeSet%20attrs)%20 同樣,基本不用TranslateAnimation(float%20fromXDelta,%20float%20toXDelta,%20float%20fromYDelta,%20float%20toYDelta)TranslateAnimation(int%20fromXType,%20float%20fromXValue,%20int%20toXType,%20float%20toXValue,%20int%20fromYType,%20float%20fromYValue,%20int%20toYType,%20float%20toYValue)

由于fromXDelta、fromYDelta、toXDelta、toYDelta這三個(gè)屬性都具有三種狀態(tài),所以在構(gòu)造函數(shù)中,最理想的狀態(tài)就是第三個(gè)構(gòu)造函數(shù),能夠指定每個(gè)值的類型,第二個(gè)構(gòu)造函數(shù):TranslateAnimation%20(float%20fromXDelta,%20float%20toXDelta,%20float%20fromYDelta,%20float%20toYDelta)使用是絕對(duì)數(shù)值。只有最后一個(gè)構(gòu)造函數(shù)可以指定百分?jǐn)?shù)和相對(duì)父控件的百分?jǐn)?shù)。

下面以第一篇中的XML代碼為例,用JAVA代碼構(gòu)造同樣的效果:

XML代碼:

[html]%20view%20plain%20copy%20<?xml version="1.0" encoding="utf-8"?>  <translate xmlns:android="http://schemas.android.com/apk/res/android"      android:fromXDelta="0"       android:toXDelta="-80"      android:fromYDelta="0"      android:toYDelta="-80"      android:duration="2000"      android:fillBefore="true">  </translate>  對(duì)應(yīng)的JAVA代碼為:[html]%20view%20plain%20copy%20translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,           Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);  translateAnim.setDuration(2000);  translateAnim.setFillBefore(true);  

七:AnimationSet

AnimationSet類對(duì)應(yīng)set標(biāo)簽,定義動(dòng)作類的集合,對(duì)應(yīng)的SDK文檔地址為:《AnimationSet》它自己是沒有XML屬性的,所以我們直接說它的構(gòu)造函數(shù):AnimationSet(Context%20context,%20AttributeSet%20attrs)%20 同樣,基本不用AnimationSet(boolean%20shareInterpolator)%20 shareInterpolator取值true或false,取true時(shí),指在AnimationSet中定義一個(gè)插值器(interpolater),它下面的所有動(dòng)畫共同。如果設(shè)為false,則表示它下面的動(dòng)畫自己定義各自的插值器。

增加動(dòng)畫的函數(shù)為:(更多函數(shù),請(qǐng)參看SDK文檔)

public%20void%20addAnimation%20(Animation%20a)

下面在第一篇中的XML代碼為例寫出能構(gòu)造同樣效果的JAVA代碼:

XML代碼為:

[html]%20view%20plain%20copy%20<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android"      android:duration="3000"      android:fillAfter="true">          <alpha       android:fromAlpha="0.0"      android:toAlpha="1.0"/>        <scale      android:fromXScale="0.0"      android:toXScale="1.4"      android:fromYScale="0.0"      android:toYScale="1.4"      android:pivotX="50%"      android:pivotY="50%"/>        <rotate      android:fromDegrees="0"      android:toDegrees="720"      android:pivotX="50%"      android:pivotY="50%"/>           </set>  對(duì)應(yīng)的JAVA代碼為:[java]%20view%20plain%20copy%20alphaAnim = new AlphaAnimation(1.0f,0.1f);  scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);    setAnim=new AnimationSet(true);  setAnim.addAnimation(alphaAnim);  setAnim.addAnimation(scaleAnim);  setAnim.addAnimation(rotateAnim);    setAnim.setDuration(3000);  setAnim.setFillAfter(true);  

八、Interpolater插值器

關(guān)于插值器的效果及應(yīng)用,我們專門開了一篇來講,看這里:《Animation動(dòng)畫詳解(二)——Interpolator插值器》

關(guān)于插值器的SDK講解見《Animation%20Resources》中的Interpolators部分;

插值器XML屬性及對(duì)應(yīng)的類如下表所示:

Interpolator%20classResource%20IDAccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolatorAccelerateInterpolator@android:anim/accelerate_interpolatorAnticScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  interpolateScaleAnim.setInterpolator(new BounceInterpolator());  interpolateScaleAnim.setDuration(3000);  

九、示例,   

源碼下載地址:http://download.csdn.net/detail/harvic880925/8047669

請(qǐng)大家尊重原創(chuàng)者版權(quán),轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/harvic880925/article/details/40117115 謝謝!

如果我的文章有幫到您,記得加關(guān)注哦!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐清市| 图片| 武夷山市| 贵港市| 广昌县| 万宁市| 新安县| 黑河市| 定远县| 虹口区| 大新县| 北京市| 龙川县| 牙克石市| 澄江县| 太仆寺旗| 太白县| 高平市| 吉林省| 曲阜市| 仁怀市| 浙江省| 九江县| 永春县| 屏山县| 天等县| 资溪县| 临清市| 老河口市| 鹿邑县| 叶城县| 图木舒克市| 永定县| 阿勒泰市| 大城县| 罗源县| 泗阳县| 濉溪县| 方城县| 梓潼县| 冕宁县|