Android學習筆記-tween動畫

來源:互聯網
上載者:User

標籤:android   style   http   color   io   使用   ar   strong   檔案   

Android動畫分為Tween動畫和Frame動畫,近期學習了,體tween動畫,現在講學習的心得以及相關知識介紹如下。

Tween又稱為補間動畫,可以把對象進行縮小、放大、旋轉和漸層等操作。

   第一: Tween動畫四個主要實作類別:1、AlphaAnimation:漸層(顏色)動畫,主要控制透明度變化動畫類,常使用AlphaAnimation(float fromAlpha, float toAlpha)來構造;    fromAlpha:動畫開始時的透明度(取值範圍為0.0到1.0);    toAlpha:動畫結束時的透明度;2、ScaleAnimation:主要控制大小變化,常使用ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)來構造;    fromX:動畫開始X座標上的伸縮尺度(是相對於原來圖片的大小而言);    toX:動畫結束X座標上的伸縮尺度;    fromY:動畫開始Y座標上的伸縮尺度;    toY:動畫結束Y座標上的伸縮尺度;    pivotXType:X座標上的伸縮模式(類型),取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF:相對於自身, Animation.RELATIVE_TO_PARENT;    pivotXValue:X座標上縮放的中心位置;    pivotYType:Y座標上的伸縮模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT;    pivotYValue:Y座標上縮放的中心位置;3、TranslateAnimation:主要控制位置變換的動畫實作類別,常使用TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)來構造;    fromXDelta:動畫開始的X座標;    toXDelta:動畫結束的X座標;    fromYDelta:動畫開始的Y座標;    toYDelta:動畫結束的Y座標;4、RotateAnimation:主要控制旋轉的動畫實作類別,常使用RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)來構造;    fromDegrees:旋轉開始角度;    toDegrees:旋轉結束角度;    pivotXType, pivotXValue, pivotYType, pivotYValue與尺度變化動畫ScaleAnimation類似;  第二:所包含的共用的方法 //設定播放時間

animation.setDuration(2000);

//設定重複的次數,記著是重複的次數

animation.setRepeatCount(2);

//設定重複的模式,有兩種RESTART:重新開始與REVERSE:反向開始

animation.setRepeatMode(AlphaAnimation.REVERSE);

//啟動播放

iv.startAnimation(animation);

 

第三:執行個體,

布局檔案我們這樣寫,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.ftf.tween.MainActivity" >

 

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:onClick="click"

            android:text="透明度"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" />

        <Button

            android:onClick="click2"

            android:text="縮放"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" />

        <Button

            android:onClick="click3"

            android:text="旋轉"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" />

        <Button

            android:onClick="click4"

            android:text="平移"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" />

        <Button

            android:onClick="click5"

            android:text="組合"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:gravity="center" >

 

        <ImageView

            android:id="@+id/iv"

            android:src="@drawable/ic_launcher"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

             />

    </LinearLayout>

 

</LinearLayout>

 

activity中,這樣寫: 

// 透明度變化

public void click(View view) {

AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);

animation.setDuration(2000);

animation.setRepeatCount(2);

animation.setRepeatMode(AlphaAnimation.REVERSE);

iv.startAnimation(animation);

}

 

public void click2(View view) {

ScaleAnimation animation = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

0.5f);

 

animation.setDuration(2000);

animation.setRepeatCount(2);

animation.setRepeatMode(AlphaAnimation.REVERSE);

iv.startAnimation(animation);

 

}

 

public void click3(View view) {

RotateAnimation animation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

0.5f);

 

animation.setDuration(2000);

animation.setRepeatCount(2);

animation.setRepeatMode(AlphaAnimation.REVERSE);

iv.startAnimation(animation);

}

 

public void click4(View view) {

TranslateAnimation animation = new TranslateAnimation(

Animation.RELATIVE_TO_PARENT, 0.2f,

Animation.RELATIVE_TO_PARENT, 1.0f,

Animation.RELATIVE_TO_PARENT, 0.2f,

Animation.RELATIVE_TO_PARENT, 1.0f);

 

animation.setDuration(2000);

animation.setRepeatCount(2);

animation.setRepeatMode(AlphaAnimation.REVERSE);

iv.startAnimation(animation);

}

 

public void click5(View view) {

//設定混合模式,也即多重播放效果放在一起。

AnimationSet set = new AnimationSet(false);

 

TranslateAnimation pa = new TranslateAnimation(

Animation.RELATIVE_TO_PARENT, 0.2f,

Animation.RELATIVE_TO_PARENT, 1.0f,

Animation.RELATIVE_TO_PARENT, 0.2f,

Animation.RELATIVE_TO_PARENT, 1.0f);

 

pa.setDuration(2000);

pa.setRepeatCount(2);

pa.setRepeatMode(AlphaAnimation.REVERSE);

 

RotateAnimation ra = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

0.5f);

 

ra.setDuration(2000);

ra.setRepeatCount(2);

ra.setRepeatMode(AlphaAnimation.REVERSE);

 

ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

0.5f);

 

sa.setDuration(2000);

sa.setRepeatCount(2);

sa.setRepeatMode(AlphaAnimation.REVERSE);

 

set.addAnimation(sa);

set.addAnimation(ra);

set.addAnimation(pa);

 

iv.startAnimation(set);

}

 

 

Android學習筆記-tween動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.