亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開(kāi)發(fā)之Animations動(dòng)畫(huà)用法實(shí)例詳解

 更新時(shí)間:2016年02月12日 13:04:20   作者:陽(yáng)光島主  
這篇文章主要介紹了Android開(kāi)發(fā)之Animations動(dòng)畫(huà)用法,結(jié)合實(shí)例形式詳細(xì)分析了Animations動(dòng)畫(huà)的類型、組成、模式及對(duì)應(yīng)的使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)之Animations動(dòng)畫(huà)用法。分享給大家供大家參考,具體如下:

一、動(dòng)畫(huà)類型

Android的animation由四種類型組成:alpha、scale、translate、rotate

XML配置文件中

alpha
漸變透明度動(dòng)畫(huà)效果
scale
漸變尺寸伸縮動(dòng)畫(huà)效果
translate
畫(huà)面轉(zhuǎn)換位置移動(dòng)動(dòng)畫(huà)效果
rotate
畫(huà)面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫(huà)效果

Java Code代碼中

AlphaAnimation
漸變透明度動(dòng)畫(huà)效果
ScaleAnimation
漸變尺寸伸縮動(dòng)畫(huà)效果
TranslateAnimation
畫(huà)面轉(zhuǎn)換位置移動(dòng)動(dòng)畫(huà)效果
RotateAnimation
畫(huà)面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫(huà)效果

二、Android動(dòng)畫(huà)模式

Animation主要有兩種動(dòng)畫(huà)模式:tweened 和 frame

一種是tweened animation(漸變動(dòng)畫(huà))

XML
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation

一種是frame by frame(畫(huà)面轉(zhuǎn)換動(dòng)畫(huà))

XML
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation

三、XML文件中定義動(dòng)畫(huà)

① 打開(kāi)Eclipse,新建Android工程
② 在res目錄中新建anim文件夾
③ 在anim目錄中新建一個(gè)myanim.xml(注意文件名小寫(xiě))
④ 加入XML的動(dòng)畫(huà)代碼

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

四、Android XML動(dòng)畫(huà)解析

1. Alpha

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/>
<!-- 透明度控制動(dòng)畫(huà)效果 alpha
    浮點(diǎn)型值:
      fromAlpha 屬性為動(dòng)畫(huà)起始時(shí)透明度
      toAlpha  屬性為動(dòng)畫(huà)結(jié)束時(shí)透明度
      說(shuō)明:
        0.0表示完全透明
        1.0表示完全不透明
      以上值取0.0-1.0之間的float數(shù)據(jù)類型的數(shù)字
    長(zhǎng)整型值:
      duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間
      說(shuō)明:
        時(shí)間以毫秒為單位
-->
</set>

2. Scale

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale
     android:interpolator=
           "@android:anim/accelerate_decelerate_interpolator"
     android:fromXScale="0.0"
     android:toXScale="1.4"
     android:fromYScale="0.0"
     android:toYScale="1.4"
     android:pivotX="50%"
     android:pivotY="50%"
     android:fillAfter="false"
     android:duration="700" />
</set>
<!-- 尺寸伸縮動(dòng)畫(huà)效果 scale
    屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器
    在我試驗(yàn)過(guò)程中,使用android.res.anim中的資源時(shí)候發(fā)現(xiàn)
    有三種動(dòng)畫(huà)插入器:
      accelerate_decelerate_interpolator 加速-減速 動(dòng)畫(huà)插入器
      accelerate_interpolator     加速-動(dòng)畫(huà)插入器
      decelerate_interpolator     減速- 動(dòng)畫(huà)插入器
    其他的屬于特定的動(dòng)畫(huà)效果
   浮點(diǎn)型值:
      fromXScale 屬性為動(dòng)畫(huà)起始時(shí) X坐標(biāo)上的伸縮尺寸
      toXScale  屬性為動(dòng)畫(huà)結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸
      fromYScale 屬性為動(dòng)畫(huà)起始時(shí)Y坐標(biāo)上的伸縮尺寸
      toYScale  屬性為動(dòng)畫(huà)結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸
      說(shuō)明:
         以上四種屬性值
          0.0表示收縮到?jīng)]有
          1.0表示正常無(wú)伸縮
          值小于1.0表示收縮
          值大于1.0表示放大
      pivotX   屬性為動(dòng)畫(huà)相對(duì)于物件的X坐標(biāo)的開(kāi)始位置
      pivotY   屬性為動(dòng)畫(huà)相對(duì)于物件的Y坐標(biāo)的開(kāi)始位置
      說(shuō)明:
          以上兩個(gè)屬性值 從0%-100%中取值
          50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
    長(zhǎng)整型值:
      duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間
      說(shuō)明:  時(shí)間以毫秒為單位
    布爾型值:
      fillAfter 屬性 當(dāng)設(shè)置為true ,該動(dòng)畫(huà)轉(zhuǎn)化在動(dòng)畫(huà)結(jié)束后被應(yīng)用
-->

3. Translate

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="30"
android:toXDelta="-80"
android:fromYDelta="30"
android:toYDelta="300"
android:duration="2000"
/>
<!-- translate 位置轉(zhuǎn)移動(dòng)畫(huà)效果
    整型值:
      fromXDelta 屬性為動(dòng)畫(huà)起始時(shí) X坐標(biāo)上的位置
      toXDelta  屬性為動(dòng)畫(huà)結(jié)束時(shí) X坐標(biāo)上的位置
      fromYDelta 屬性為動(dòng)畫(huà)起始時(shí) Y坐標(biāo)上的位置
      toYDelta  屬性為動(dòng)畫(huà)結(jié)束時(shí) Y坐標(biāo)上的位置
      注意:
           沒(méi)有指定fromXType toXType fromYType toYType 時(shí)候,
           默認(rèn)是以自己為相對(duì)參照物
    長(zhǎng)整型值:
      duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間
      說(shuō)明:  時(shí)間以毫秒為單位
-->
</set>

4. Rotate

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="+350"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="3000" />
<!-- rotate 旋轉(zhuǎn)動(dòng)畫(huà)效果
    屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器
       在我試驗(yàn)過(guò)程中,使用android.res.anim中的資源時(shí)候發(fā)現(xiàn)
       有三種動(dòng)畫(huà)插入器:
        accelerate_decelerate_interpolator  加速-減速 動(dòng)畫(huà)插入器
        accelerate_interpolator        加速-動(dòng)畫(huà)插入器
        decelerate_interpolator        減速- 動(dòng)畫(huà)插入器
       其他的屬于特定的動(dòng)畫(huà)效果
    浮點(diǎn)數(shù)型值:
      fromDegrees 屬性為動(dòng)畫(huà)起始時(shí)物件的角度
      toDegrees  屬性為動(dòng)畫(huà)結(jié)束時(shí)物件旋轉(zhuǎn)的角度 可以大于360度
      說(shuō)明:
           當(dāng)角度為負(fù)數(shù)——表示逆時(shí)針旋轉(zhuǎn)
           當(dāng)角度為正數(shù)——表示順時(shí)針旋轉(zhuǎn)
           (負(fù)數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))
           (負(fù)數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))
           (正數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))
           (正數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))
      pivotX   屬性為動(dòng)畫(huà)相對(duì)于物件的X坐標(biāo)的開(kāi)始位置
      pivotY   屬性為動(dòng)畫(huà)相對(duì)于物件的Y坐標(biāo)的開(kāi)始位置
      說(shuō)明:    以上兩個(gè)屬性值 從0%-100%中取值
             50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
    長(zhǎng)整型值:
      duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間
      說(shuō)明:    時(shí)間以毫秒為單位
-->
</set>

XML中使用動(dòng)畫(huà)效果

public static Animation loadAnimation (Context context, int id)
//第一個(gè)參數(shù)Context為程序的上下文
//第二個(gè)參數(shù)id為動(dòng)畫(huà)XML文件的引用
//例子:
myAnimation= AnimationUtils.loadAnimation(this, R.anim.my_action);
//使用AnimationUtils類的靜態(tài)方法loadAnimation()來(lái)加載XML中的動(dòng)畫(huà)XML文件

五、Java代碼中定義動(dòng)畫(huà)

//在代碼中定義 動(dòng)畫(huà)實(shí)例對(duì)象
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;
//根據(jù)各自的構(gòu)造方法來(lái)初始化一個(gè)實(shí)例對(duì)象
myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
       Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Translate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

六、Android 代碼動(dòng)畫(huà)解析

1. AlphaAnimation

AlphaAnimation類對(duì)象定義

1. private AlphaAnimation myAnimation_Alpha;

AlphaAnimation類對(duì)象構(gòu)造

AlphaAnimation(float fromAlpha, float toAlpha)
//第一個(gè)參數(shù)fromAlpha為 動(dòng)畫(huà)開(kāi)始時(shí)候透明度
//第二個(gè)參數(shù)toAlpha為 動(dòng)畫(huà)結(jié)束時(shí)候透明度
myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
//說(shuō)明:
//        0.0表示完全透明
//        1.0表示完全不透明

設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間

myAnimation_Alpha.setDuration(5000);
//設(shè)置時(shí)間持續(xù)時(shí)間為 5000毫秒

2. ScaleAnimation

ScaleAnimation類對(duì)象定義

private ScaleAnimation myAnimation_Scale;

ScaleAnimation類對(duì)象構(gòu)造

ScaleAnimation(float fromX, float toX, float fromY, float toY,
      int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一個(gè)參數(shù)fromX為動(dòng)畫(huà)起始時(shí) X坐標(biāo)上的伸縮尺寸
//第二個(gè)參數(shù)toX為動(dòng)畫(huà)結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸
//第三個(gè)參數(shù)fromY為動(dòng)畫(huà)起始時(shí)Y坐標(biāo)上的伸縮尺寸
//第四個(gè)參數(shù)toY為動(dòng)畫(huà)結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸
/*說(shuō)明:
  以上四種屬性值
  0.0表示收縮到?jīng)]有
  1.0表示正常無(wú)伸縮
  值小于1.0表示收縮
  值大于1.0表示放大
*/
//第五個(gè)參數(shù)pivotXType為動(dòng)畫(huà)在X軸相對(duì)于物件位置類型
//第六個(gè)參數(shù)pivotXValue為動(dòng)畫(huà)相對(duì)于物件的X坐標(biāo)的開(kāi)始位置
//第七個(gè)參數(shù)pivotXType為動(dòng)畫(huà)在Y軸相對(duì)于物件位置類型
//第八個(gè)參數(shù)pivotYValue為動(dòng)畫(huà)相對(duì)于物件的Y坐標(biāo)的開(kāi)始位置
myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
       Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間

myAnimation_Scale.setDuration(700);
//設(shè)置時(shí)間持續(xù)時(shí)間為 700毫秒

3. TranslateAnimation

ranslateAnimation類對(duì)象定義

private TranslateAnimation myAnimation_Translate;

TranslateAnimation類對(duì)象構(gòu)造

TranslateAnimation(float fromXDelta, float toXDelta,
            float fromYDelta, float toYDelta)
//第一個(gè)參數(shù)fromXDelta為動(dòng)畫(huà)起始時(shí) X坐標(biāo)上的移動(dòng)位置
//第二個(gè)參數(shù)toXDelta為動(dòng)畫(huà)結(jié)束時(shí) X坐標(biāo)上的移動(dòng)位置
//第三個(gè)參數(shù)fromYDelta為動(dòng)畫(huà)起始時(shí)Y坐標(biāo)上的移動(dòng)位置
//第四個(gè)參數(shù)toYDelta為動(dòng)畫(huà)結(jié)束時(shí)Y坐標(biāo)上的移動(dòng)位置

設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間

myAnimation_Translate = new TranslateAnimation(10f, 100f, 10f, 100f);
myAnimation_Translate.setDuration(2000);
//設(shè)置時(shí)間持續(xù)時(shí)間為 2000毫秒

4. RotateAnimation

RotateAnimation類對(duì)象定義
private RotateAnimation myAnimation_Rotate;
RotateAnimation類對(duì)象構(gòu)造
RotateAnimation(float fromDegrees, float toDegrees,
      int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一個(gè)參數(shù)fromDegrees為動(dòng)畫(huà)起始時(shí)的旋轉(zhuǎn)角度
//第二個(gè)參數(shù)toDegrees為動(dòng)畫(huà)旋轉(zhuǎn)到的角度
//第三個(gè)參數(shù)pivotXType為動(dòng)畫(huà)在X軸相對(duì)于物件位置類型
//第四個(gè)參數(shù)pivotXValue為動(dòng)畫(huà)相對(duì)于物件的X坐標(biāo)的開(kāi)始位置
//第五個(gè)參數(shù)pivotXType為動(dòng)畫(huà)在Y軸相對(duì)于物件位置類型
//第六個(gè)參數(shù)pivotYValue為動(dòng)畫(huà)相對(duì)于物件的Y坐標(biāo)的開(kāi)始位置
myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間

myAnimation_Rotate.setDuration(3000);
//設(shè)置時(shí)間持續(xù)時(shí)間為 3000毫秒

如何Java代碼中使用動(dòng)畫(huà)效果

使用從View父類繼承過(guò)來(lái)的方法startAnimation()來(lái)為View或是子類View等等添加一個(gè)動(dòng)畫(huà)效果

public void startAnimation (Animation animation)
view.startAnimation(myAnimation_Alpha);
view.startAnimation(myAnimation_Scale);
view.startAnimation(myAnimation_Translate);
view.startAnimation(myAnimation_Rotate);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)動(dòng)畫(huà)技巧匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》及《Android控件用法總結(jié)》。

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論