android 之動(dòng)畫

動(dòng)畫:

一、幀動(dòng)畫

res\drawable

Drawable動(dòng)畫

使用的類:AnimationDrawable

常用方法:

start:啟動(dòng)

stop:停止

isRunning:是否正在運(yùn)行

將指定的一系列圖片婿滓,按照順序進(jìn)行切換蚊伞,類似Gif

使用步驟:

1映跟、準(zhǔn)備資源圖片

2畅卓、在res\drawable\下創(chuàng)建<animation-list>

3尉咕、使用<item>進(jìn)行圖片顯示順序的設(shè)置和停留時(shí)間的設(shè)置

4绊袋、在對應(yīng)的ImageView中使用background引用剛剛的<animation-list>

5毕匀、在對應(yīng)的Activity中使用AnimationDrawable的start啟動(dòng)動(dòng)畫

二、補(bǔ)間動(dòng)畫

View動(dòng)畫

res\anim

使用的類:Animation和AnimationUtils

通過設(shè)置圖片的透明度癌别、平移皂岔、縮放展姐、旋轉(zhuǎn)等實(shí)現(xiàn)動(dòng)畫的效果

使用方式:

1躁垛、XML方式

使用步驟:

1、在res文件夾下創(chuàng)建anim文件夾圾笨,用于存儲(chǔ)動(dòng)畫效果

2教馆、在anim下創(chuàng)建對應(yīng)的動(dòng)畫效果

1、alpha:透明度

常用屬性:

fromAlpha:起始的透明度

toAlpha:終止的透明度

duration:動(dòng)畫的持續(xù)時(shí)間擂达,單位毫秒

注:透明度范圍:0.0-1.0土铺,其中0.0完全透明、1.0完全顯示

2板鬓、rotate旋轉(zhuǎn)

常用屬性:

fromDegrees:起始角度

toDegrees:終止角度

pivotX:中心點(diǎn)的x坐標(biāo)

pivotY:中心點(diǎn)的y坐標(biāo)

repeatCount:重復(fù)的次數(shù)

repeatMode:重復(fù)的模式

interpolator:加速效果

3悲敷、translate:平移

常用屬性:

fromXDelta:設(shè)置起始的x坐標(biāo)

toXDelta:終止的x坐標(biāo)

fromYDelta:起始的y坐標(biāo)

toYDelta:終止的y坐標(biāo)

repeatCount:重復(fù)的次數(shù)

repeatMode:重復(fù)的模式

interpolator:加速效果

4、scale:設(shè)置縮放效果

常用屬性:

fromXScale:起始的x方向的縮放

toXScale:結(jié)束的x方向的縮放

fromYScale:起始的y方向的縮放

toYScale:終止的y方向的縮放

repeatCount:重復(fù)的次數(shù)

repeatMode:重復(fù)的模式

interpolator:加速效果

5俭令、set:組合動(dòng)畫

常用屬性:

repeatCount:重復(fù)的次數(shù)

repeatMode:重復(fù)的模式

3后德、在對應(yīng)的Activity中通過AnimationUtils.loadAnimation獲取對應(yīng)的動(dòng)畫對象

4、找到對應(yīng)的ImageView設(shè)置startAnimation對應(yīng)的動(dòng)畫效果并啟動(dòng)

注意:interpolator動(dòng)畫的加速效果

常用的值:

@android:anim/accelerate_decelerate_interpolator" 先加速后減速

@android:anim/accelerate_interpolator 開始慢抄腔,結(jié)束快

@android:anim/decelerate_interpolator 開始快瓢湃,結(jié)束慢

@android:anim/anticipate_interpolator 開始時(shí)會(huì)超出起始位置,再執(zhí)行動(dòng)畫

@android:anim/overshoot_interpolator 結(jié)束時(shí)會(huì)超出終止位置妓柜,再結(jié)束動(dòng)畫

@android:anim/anticipate_overshoot_interpolator 開始和結(jié)束時(shí)都會(huì)超出一定距離

@android:anim/bounce_interpolator 結(jié)束時(shí)箱季,有反彈效果

@android:anim/cycle_interpolator 動(dòng)畫按特定的次數(shù)執(zhí)行多次,有振動(dòng)效果

@android:anim/linear_interpolator 均勻的速度執(zhí)行動(dòng)畫

2棍掐、JAVA方式

常用的動(dòng)畫類:

1藏雏、AlphaAnimation:透明度動(dòng)畫類

構(gòu)造方法:

AlphaAnimation(float fromAlpha, float toAlpha)

參數(shù)說明:

fromAlpha:起始的透明度

toAlpha:終止的透明度

2、RotateAnimation:旋轉(zhuǎn)動(dòng)畫類

構(gòu)造方法:

RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

參數(shù)說明:

fromDegrees:起始的角度

toDegrees:終止的角度

pivotX:中心點(diǎn)的x坐標(biāo)

pivotY:中心點(diǎn)的y坐標(biāo)

3、TranslateAnimation:平移動(dòng)畫類

構(gòu)造方法:

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

參數(shù)說明:

fromXDelta:x方向起始位置

toXDelta:x方向終止位置

fromYDelta:y方向起始位置

toYDelta:y方向終止位置

4掘殴、ScaleAnimation:縮放動(dòng)畫類

構(gòu)造方法:

ScaleAnimation(float fromX, float toX, float fromY, float toY)

參數(shù)說明:

fromX:x方向的起始縮放

toX:x方向終止縮放

fromY:y方向起始縮放

toY:y方向終止縮放

5赚瘦、AnimationSet:組合動(dòng)畫類

常用方法:

addAnimation:添加動(dòng)畫效果

三、屬性動(dòng)畫

res\animator

2種方式:

1奏寨、xml文件

使用步驟:

1起意、在res\animator文件夾

2、創(chuàng)建對應(yīng)的<objectAnimator>

使用android:propretyName:用來標(biāo)記哪種動(dòng)畫

常用的值:

alpha:透明度

backgroundColor:背景色

rotateX/Y:旋轉(zhuǎn)

scaleX/Y:縮放

translationX/Y:移動(dòng)

注意:組合動(dòng)畫使用的<set>

3病瞳、在對應(yīng)的Activity中獲取對應(yīng)的動(dòng)畫并綁定控件且啟動(dòng)

AnimatorInflater.loadAnimator:將指定的xml文件轉(zhuǎn)換為動(dòng)畫對象

setTarget:為指定的控件設(shè)置動(dòng)畫效果

start:啟動(dòng)動(dòng)畫

end:結(jié)束動(dòng)畫

cancel:取消動(dòng)畫

2揽咕、java方式

1、ObjectAnimator:屬性動(dòng)畫

包括常用的:透明度套菜、旋轉(zhuǎn)亲善、移動(dòng)、縮放

2逗柴、AnimatorSet:屬性動(dòng)畫的組合動(dòng)畫

playTogether:同時(shí)播放

playSequentially:順序播放

play(動(dòng)畫).with/before/after:自定義播放

addUpdateListener:監(jiān)聽屬性動(dòng)畫的變化過程

顏色資源:

路徑:res\values\colors

<color >

補(bǔ)間動(dòng)畫

public class MainActivity extends Activity {

private ImageView iv;

//聲明對應(yīng)的補(bǔ)間動(dòng)畫對象

private Animation animation;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

}

public void click(View view) {

switch (view.getId()) {

case R.id.tv_1://透明度動(dòng)畫

//加載對應(yīng)的透明度動(dòng)畫效果

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

break;

case R.id.tv_2://旋轉(zhuǎn)

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

break;

case R.id.tv_3://移動(dòng)

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

break;

case R.id.tv_4://縮放

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

break;

case R.id.iv://組合動(dòng)畫

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

AnimationSet as=new AnimationSet(true);

as.addAnimation(animation);

as.setDuration(3000);

as.setRepeatCount(10);

as.setRepeatMode(Animation.RESTART);

as.setFillAfter(true);//設(shè)置動(dòng)畫結(jié)束時(shí)蛹头,是否

//設(shè)置是否有動(dòng)畫效果

as.setInterpolator(this, android.R.anim.fade_in);

break;

}

//為動(dòng)畫設(shè)置監(jiān)聽事件

animation.setAnimationListener(new AnimationListener() {

//啟動(dòng)

@Override

public void onAnimationStart(Animation animation) {

// TODO Auto-generated method stub

Log.e("Animation", "onAnimationStart……");

}

//重復(fù)

@Override

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

Log.e("Animation", "onAnimationRepeat……");

}

//結(jié)束

@Override

public void onAnimationEnd(Animation animation) {

// TODO Auto-generated method stub

Log.e("Animation", "onAnimationEnd……");

}

});

//開始執(zhí)行動(dòng)畫

iv.startAnimation(animation);

}

}

xml

<?xml version="1.0" encoding="UTF-8"?>

<alpha android:duration="4000" android:toAlpha="1.0" android:fromAlpha="0.1" xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>

2

<?xml version="1.0" encoding="UTF-8"?>

<rotate android:repeatMode="reverse" android:repeatCount="10" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotY="50%" android:pivotX="50%" android:toDegrees="360" android:fromDegrees="0" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>

3

<?xml version="1.0" encoding="UTF-8"?>

<scale android:interpolator="@android:anim/cycle_interpolator" android:repeatMode="restart" android:repeatCount="10" android:pivotY="50%" android:pivotX="50%" android:toYScale="6" android:fromYScale="0.1" android:toXScale="3" android:fromXScale="0.1" android:duration="3000" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>

4

<?xml version="1.0" encoding="UTF-8"?>

-<set android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android"> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:toAlpha="1.0" android:fromAlpha="0.1" android:duration="2000"> </alpha> <rotate android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:toDegrees="360" android:pivotY="50%" android:pivotX="50%" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0"> </rotate> <scale android:repeatMode="restart" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:pivotY="50%" android:pivotX="50%" android:interpolator="@android:anim/cycle_interpolator" android:toYScale="6" android:toXScale="3" android:fromYScale="0.1" android:fromXScale="0.1"> </scale> <translate android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:interpolator="@android:anim/bounce_interpolator" android:toYDelta="0" android:toXDelta="200" android:fromYDelta="0" android:fromXDelta="0"> </translate> </set>

6

<?xml version="1.0" encoding="UTF-8"?>

<translate android:interpolator="@android:anim/bounce_interpolator" android:repeatMode="reverse" android:repeatCount="10" android:toYDelta="400" android:fromYDelta="0" android:toXDelta="200" android:fromXDelta="0" android:duration="4000" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>

補(bǔ)間動(dòng)畫有一定的缺陷,按鈕移動(dòng)后點(diǎn)擊事件依然在原來的位置戏溺,動(dòng)畫效果是通過一些列算法來實(shí)現(xiàn)的渣蜗,所以后來出現(xiàn)屬性動(dòng)畫,效果是真實(shí)控件的移動(dòng)變幻旷祸。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末耕拷,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子肋僧,更是在濱河造成了極大的恐慌斑胜,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嫌吠,死亡現(xiàn)場離奇詭異止潘,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)辫诅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門凭戴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炕矮,你說我怎么就攤上這事么夫。” “怎么了肤视?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵档痪,是天一觀的道長。 經(jīng)常有香客問我邢滑,道長腐螟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮乐纸,結(jié)果婚禮上衬廷,老公的妹妹穿的比我還像新娘。我一直安慰自己汽绢,他們只是感情好吗跋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宁昭,像睡著了一般跌宛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上久窟,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天秩冈,我揣著相機(jī)與錄音,去河邊找鬼斥扛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛丹锹,可吹牛的內(nèi)容都是我干的稀颁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼楣黍,長吁一口氣:“原來是場噩夢啊……” “哼匾灶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起租漂,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤阶女,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后哩治,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秃踩,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年业筏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了憔杨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蒜胖,死狀恐怖消别,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情台谢,我是刑警寧澤寻狂,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站朋沮,受9級(jí)特大地震影響蛇券,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一怀读、第九天 我趴在偏房一處隱蔽的房頂上張望诉位。 院中可真熱鬧,春花似錦菜枷、人聲如沸苍糠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽岳瞭。三九已至,卻和暖如春蚊锹,著一層夾襖步出監(jiān)牢的瞬間瞳筏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來泰國打工牡昆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留姚炕,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓丢烘,卻偏偏與公主長得像柱宦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子播瞳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容