android中三種動畫的簡單介紹

前言 ?

在學(xué)習(xí)了Android的動畫之后,簡單總結(jié)了一下,看完這篇文章就可以自己動手寫一個簡單的動畫了

正文

Android中的動畫可分為三種:

1、逐幀動畫 FrameAnimation

2、補間動畫 TweenAnimation

3、屬性動畫 PropertyAnimation

本文就來簡單的介紹一下這三種動畫

一、逐幀動畫:FrameAnimation

? ? ? ? ? 每一張靜止的動畫依次顯示出來。利用人眼暫時停留的錯覺,得出的動畫。

首先在drawable文件中添加文件。資源文件一般存放在res/drawable/目錄當(dāng)中。


幀動畫的使用步驟

1、在drawable文件夾下創(chuàng)建幀動畫的資源文件frame_animlist.xml 代碼如下:

<animation-listxmlns:android="http://schemas.android.com/apk/res/android">

<item

? ? ? ? ? android:drawable="@drawable/icon_open"

? ? ? ? ? android:duration="200"/>

<item

? ? ? ? ? android:drawable="@drawable/icon_hell"

? ? ? ? ? android:duration="200"/>

</animation-list>

其中:

? ? ? ? ? duration:表示持續(xù)的時間

? ? ? ? ? 設(shè)置android:oneshot="false":設(shè)置為true:就表示指定的圖片只切換一次

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?設(shè)置為false:就表示指定的item圖片無限切換。默認(rèn)為無限切換

2、在布局文件中用ImageView控件作為動畫載體來顯示動畫,可設(shè)置兩個button按鈕來開啟和停止動畫

<ImageView

android:id="@+id/iv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"/>

<Button

android:id="@+id/bt_start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="start"/>

<Button

android:id="@+id/bt_stop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="stop"

android:layout_alignParentBottom="true"

android:layout_toRightOf="@+id/bt_start"

android:layout_toEndOf="@+id/bt_start"

android:layout_marginLeft="26dp"

android:layout_marginStart="26dp"/>


3、在java代碼中設(shè)置控件的背景為動畫資源文件。

iv. setBackgroundResource();

4、聲明動畫管理器,AnimationDrawable,通過獲得控件的背景,對其進(jìn)行初始化。

AnimationDrawable ?anima;//聲明一個動畫管理器對象

mImageView.setBackgroundResource(R.drawable.frame_animlist);

anima= (AnimationDrawable)mImageView.getBackground();

5、調(diào)用動畫管理器的start,stop。開啟和停止動畫。


完整代碼如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView mImageView;

private Button mBtStart,mBtStop;

private AnimationDrawable anima;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

mBtStart= (Button) findViewById(R.id.bt_start);

mBtStop= (Button) findViewById(R.id.bt_stop);

mImageView.setBackgroundResource(R.drawable.frame_animlist);

anima= (AnimationDrawable)mImageView.getBackground();

mBtStart.setOnClickListener(this);

mBtStop.setOnClickListener(this);

}

@Override

public voidonClick(View view) {

switch(view.getId()){

caseR.id.bt_start:

anima.start();

break;

caseR.id.bt_stop:

anima.stop();

break;

default:

break;

}

}

}

二、補間動畫:TweenAnimation

補間動畫是指開發(fā)者只需要提供動畫開始和結(jié)束的關(guān)鍵幀,二動畫中間變化的幀,由系統(tǒng)計算,自己補充完整

資源文件一般存放在res/anim/目錄當(dāng)中。

提供了4中變化的方式。

位移的改變,旋轉(zhuǎn),透明度,大小縮放動畫。

<translate . . ./>

<rotate . . ./>

<alpha . . ./>

<scale . . ./>

可以指定動畫變化的時間,和動畫變化的速度。

補間動畫的使用步驟:

1、在drawable文件夾下創(chuàng)建anim文件夾,在anim文件夾下創(chuàng)建對應(yīng)的變化方式的xml文件

a、translate_anim.xml:(位移的改變)

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromXDelta="0"

android:toXDelta="300"

android:fromYDelta="0"

android:toYDelta="0"

android:duration="2000"/>

其中:

android:interpolator 動畫的渲染器

fromXDelta? 動畫起始位置的橫坐標(biāo)

toXDelta? ? 動畫起結(jié)束位置的橫坐標(biāo)

fromYDelta? 動畫起始位置的縱坐標(biāo)

toYDelta? 動畫結(jié)束位置的縱坐標(biāo)

duration 動畫的持續(xù)時間-->

b、rotate_anim.xml:(旋轉(zhuǎn))

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromDegrees="0"

android:toDegrees="360"

android:duration="1000"

android:repeatCount="1"

android:repeatMode="restart"/>

其中:

fromDegrees:表示旋轉(zhuǎn)的起始角度

toDegrees:表示旋轉(zhuǎn)的結(jié)束角度

repeatCount:旋轉(zhuǎn)的次數(shù)? 默認(rèn)值是0 代表旋轉(zhuǎn)1次? 如果值是repeatCount=4 旋轉(zhuǎn)5次,值為-1或者infinite時,表示補間動畫永不停止

repeatMode 設(shè)置重復(fù)的模式。默認(rèn)是restart。當(dāng)repeatCount的值大于0或者為infinite時才有效。

repeatCount=-1 或者infinite 循環(huán)了

還可以設(shè)成reverse,表示偶數(shù)次顯示動畫時會做與動畫文件定義的方向相反的方向動行。-->

c、alpha_anim.xml(透明度):

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromAlpha="1.0"

android:toAlpha="0.1"

android:duration="2000"/>

其中:

fromAlpha :表示起始透明度

toAlpha:表示結(jié)束透明度

1.0表示完全不透明

0.0表示完全透明

d、scale_anim.xml:(大小縮放動畫)

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

android:interpolator="@android:anim/accelerate_interpolator"

android:fromXScale="0.2"

android:toXScale="1.5"

android:fromYScale="0.2"

android:toYScale="1.5"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"/>

其中

fromXScale:表示沿著x軸縮放的起始比例

toXScale:表示沿著x軸縮放的結(jié)束比例

fromYScale:表示沿著y軸縮放的起始比例

toYScale:表示沿著y軸縮放的結(jié)束比例

pivotX、pivotY 表示圖片大小縮放軸點

2、在布局文件中用ImageView控件作為動畫載體來顯示動畫,同樣可設(shè)置button按鈕來顯示不同的動畫

3、聲明Animation

Animation animation= AnimationUtils.loadAnimation(this,R.anim.translate_anim);;

animation.setRepeatCount(Animation.INFINITE);//循環(huán)顯示

mImageView.startAnimation(animation);

補充:當(dāng)然可以將這些動畫設(shè)置在一起在anim文件夾下創(chuàng)建all_anim.xml使這些動畫同時實現(xiàn)

完整代碼如下:

public class MainActivity extends AppCompatActivity implementsView.OnClickListener {

private ImageView mImageView;

private Button mBtTranslate,mBtRotate,mBtScale,mBtAlpha,mBtAll;

private Animationanimation;

@Override

protected void ?onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

mBtTranslate= (Button) findViewById(R.id.bt_translate);

mBtRotate= (Button) findViewById(R.id.bt_rotate);

mBtScale= (Button) findViewById(R.id.bt_scale);

mBtAlpha= (Button) findViewById(R.id.bt_alpha);

mBtAll= (Button) findViewById(R.id.bt_all);

mBtTranslate.setOnClickListener(this);

mBtRotate.setOnClickListener(this);

mBtScale.setOnClickListener(this);

mBtAlpha.setOnClickListener(this);

mBtAll.setOnClickListener(this);

}

@Override

public void ?onClick(View view) {

switch(view.getId()){

caseR.id.bt_translate:

animation= AnimationUtils.loadAnimation(this,R.anim.translate_anim);

break;

caseR.id.bt_rotate:

animation= AnimationUtils.loadAnimation(this,R.anim.rotate_anim);

break;

caseR.id.bt_scale:

animation= AnimationUtils.loadAnimation(this,R.anim.scale_anim);

break;

caseR.id.bt_alpha:

animation= AnimationUtils.loadAnimation(this,R.anim.alpha_anim);

break;

caseR.id.bt_all:

animation= AnimationUtils.loadAnimation(this,R.anim.all_anim);

}

animation.setRepeatCount(Animation.INFINITE);//循環(huán)顯示

mImageView.startAnimation(animation);

}

}

三、屬性動畫 :PropertyAnimation

補間動畫的增強(qiáng)版

屬性動畫的兩大特點:

1、可以定義任何屬性的變化

2、可以對任何對象執(zhí)行動畫

屬性動畫能夠定義的屬性:

1、動畫的持續(xù)時間

2、動畫的插入方式

3、動畫的重復(fù)次數(shù)

4、動畫的行為

5、動畫集

6、每一幀刷新的頻率

. . . . . . . .

屬性動畫的使用步驟:

1、在drawable文件夾下創(chuàng)建animator文件夾,在animator文件夾下創(chuàng)建對應(yīng)的object_anim_x.xml文件 內(nèi)容如下:

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

android:propertyName="scaleX" ?//想要變化的屬性名稱

android:duration="5000" ?//改變的時間

android:valueType="floatType" ?//改變的值的類型

android:valueFrom="1.0" ?//改變前的值

android:valueTo="3.0"> ? ?//改變后的值

</objectAnimator>

2、同樣的需要一個ImageView控件作為動畫載體來顯示動畫

3、聲明ObjectAnimator

ObjectAnimator ?objectAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(this,

R.animator.object_anim_x);

objectAnimation . setTarget(mImageView);

objectAnimation . start();

完整代碼如下:

public class MainActivity extends AppCompatActivity {

privateImageViewmImageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

ObjectAnimator objectAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(this,

R.animator.object_anim_x);

objectAnimation . setTarget(mImageView);

objectAnimation . start();

}

}

屬性動畫很強(qiáng)大,這里只列舉了其中一種,補間動畫能夠?qū)iew進(jìn)行位移的改變,旋轉(zhuǎn),透明度和大小縮放,但是一旦超過這四種動畫補間動畫就無能為力了,此時我們可以使用屬性動畫來實現(xiàn)我們想要達(dá)到的效果。屬性動畫相當(dāng)于補間動畫的增強(qiáng)版,是3.0后推出的動畫,使用簡單、容易實現(xiàn),當(dāng)然也有一定的局限性,就是需要3.0以上的API支持,限制較大。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容