Android 對View執(zhí)行上移下移動(dòng)動(dòng)畫 android位移動(dòng)畫

1.補(bǔ)間動(dòng)畫 View Animation(Tween Animation)

1.1位移動(dòng)畫
<translate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:fromXDelta="0"
    android:toXDelta="300"  
    android:fromYDelta="100%p"  
    android:toYDelta="0"  
    android:duration="200"/>   
<!--   
 fromXDelta  動(dòng)畫起始位置的橫坐標(biāo)  
 toXDelta    動(dòng)畫起結(jié)束位置的橫坐標(biāo)  
 fromYDelta  動(dòng)畫起始位置的縱坐標(biāo)  
 toYDelta    動(dòng)畫結(jié)束位置的縱坐標(biāo)  
 duration    動(dòng)畫的持續(xù)時(shí)間  
 100%p 表示自身的長度的位置
 -->

代碼實(shí)現(xiàn):

Animation  animation = new TranslateAnimation(0,50,0,50);
參數(shù)1:x軸的起始位置
參數(shù)2:x軸的終止位置
參數(shù)3: y軸的起始位置
參數(shù)4:y軸的終止位置
相對于原圖位置的原點(diǎn)(圖片的右上角為0,0)详民,如果不想用這個(gè)點(diǎn)作為參照點(diǎn)牙勘,可以使用其他構(gòu)造
TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
參數(shù)1,參數(shù)3橄仍,參數(shù)5绣溜,參數(shù)7就是設(shè)置參照點(diǎn)的方式
imageView.startAnimation(animation);
1.2旋轉(zhuǎn)動(dòng)畫

xml定義

<rotate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:fromDegrees="0"  
    android:toDegrees="360"  
    android:duration="200"  />  
<!--   
 fromDegrees:表示旋轉(zhuǎn)的起始角度  
 toDegrees:表示旋轉(zhuǎn)的結(jié)束角度  
 duration    動(dòng)畫的持續(xù)時(shí)間  
 from<to:順時(shí)針旋轉(zhuǎn)
 from>to:逆時(shí)針旋轉(zhuǎn)
 -->

代碼實(shí)現(xiàn):

Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
參數(shù)1:旋轉(zhuǎn)的起始角度
參數(shù)2:旋轉(zhuǎn)的終止角度
參數(shù)3:旋轉(zhuǎn)中心的x軸取值參照方式
參數(shù)4:中心點(diǎn)x軸的取值
參數(shù)5:旋轉(zhuǎn)中心的y軸取值參照方式
參數(shù)6:中心點(diǎn)y軸的取值
imageView.startAnimation(animation);
1.3透明度動(dòng)畫

xml定義:

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="1.0"  
    android:toAlpha="0.1"  
    android:duration="200"/>  
<!--   
 fromAlpha :起始透明度  
 toAlpha:結(jié)束透明度  
 1.0表示完全不透明  
 0.0表示完全透明  
 -->

代碼實(shí)現(xiàn)

Animation animation = new AlphaAnimation(1f,0.1f);
參數(shù)1: 起始透明度;
參數(shù)2: 目標(biāo)透明度;
1.4縮放動(dòng)畫

xml定義

<scale xmlns:android="http://schemas.android.com/apk/res/android"  
    android:fromXScale="0.5"  
    android:toXScale="1.0"  
    android:fromYScale="0.5"  
    android:toYScale="1.0"  
    android:pivotX="50%"  
    android:pivotY="50%"  
    android:duration="200"/>  
<!--   
 fromXScale:表示沿著x軸縮放的起始比例  
 toXScale:表示沿著x軸縮放的結(jié)束比例  
 fromYScale:表示沿著y軸縮放的起始比例  
 toYScale:表示沿著y軸縮放的結(jié)束比例  
 1.0表示原來的大小
 50%表示圖片中心點(diǎn)
 -->

代碼實(shí)現(xiàn):

Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
參數(shù)1:x方向起始大小(1f表示原圖大小)
參數(shù)2:x方向終止大小(0.2f表示原圖的0.2倍)
參數(shù)3:y方向起始大小(1f表示原圖大小)
參數(shù)4:y方向終止大小(0.2f表示原圖的0.2倍)
參數(shù)5:縮放中心點(diǎn)x軸取值的參照方式
參數(shù)6:中心點(diǎn)x軸的取值(0.5f表示相對與原圖的0.5倍)
參數(shù)7:縮放中心點(diǎn)y軸取值參照方式
參數(shù)8:中心點(diǎn)y軸的取值(0.5f表示相對與原圖的0.5倍)
(注:參照方式:Animation.RELATIVE_TO_SELF:自身   Animation.RELATIVE_TO_PARENT:父控件   Animation.ABSOLUTE:絕對)
imageView.startAnimation(animation);
1.5 共有屬性
 
android:duration(播放時(shí)間)
android:repeatCount: 重復(fù)的次數(shù)  默認(rèn)值是0 代表旋轉(zhuǎn)1次,值為-1或者infinite時(shí)伦仍,表示動(dòng)畫永不停止  
android:repeatMode:  設(shè)置重復(fù)的模式结窘。默認(rèn)是restart。(當(dāng)repeatCount的值大于0或者為infinite時(shí)才有效) 
                     設(shè)成reverse充蓝,表示偶數(shù)次顯示動(dòng)畫時(shí)會(huì)做與動(dòng)畫文件定義的方向相反的方向動(dòng)行隧枫。  
對應(yīng)代碼方法:
Animation.setDuration(long durationMillis)
Animation.setRepeatCount(int repeatCount)
Animation.setRepeatMode(int repeatMode)

android:interpolator(動(dòng)畫的渲染器)
  accelerate_interpolator(動(dòng)畫加速器) 使動(dòng)畫在開始的時(shí)候 最慢,然后逐漸加速 
  decelerate_interpolator(動(dòng)畫減速器) 使動(dòng)畫在開始的時(shí)候 最快,然后逐漸減速 
  accelerate_decelerate_interpolator(動(dòng)畫加速減速器)慢到快(中間位置最快)到慢
  anticipate_interpolator(動(dòng)畫前反向器) 先往動(dòng)畫移動(dòng)的反方向移動(dòng)一點(diǎn)然后在沿著設(shè)定的動(dòng)畫移動(dòng)
  overshoot_interpolator(動(dòng)畫后反向器) 最后超出目的值然后緩慢改變到目的值
  anticipate_overshoot_interpolator (動(dòng)畫雙反向器) 先往動(dòng)畫的反方向移動(dòng)一點(diǎn),然后沿著設(shè)定的方向移動(dòng)到終點(diǎn)之后繼續(xù)移動(dòng)一點(diǎn)然后在回彈到最終設(shè)定的位置
  bounce_interpolator(動(dòng)畫彈跳器) 快到目的值時(shí)值會(huì)跳躍
  cycle_interpolator(動(dòng)畫循環(huán)器) 動(dòng)畫循環(huán)一定次數(shù)
  linear_interpolator(動(dòng)畫勻速器) 線性均勻改變
  overshoot_interpolator(動(dòng)畫減速器) 快速到達(dá)終點(diǎn)并超出一小步最后回到終點(diǎn)
 使用:android:interpolator="@android:anim/accelerate_interpolator"

對應(yīng)代碼方法:

Animation.setInterpolator(Context context, int resID) //R.anim.accelerate_interpolator
Animation.setInterpolator(Interpolator i) 
  TimeInterpolator          一個(gè)接口谓苟,允許你自定義interpolator悠垛,以下幾個(gè)都是實(shí)現(xiàn)了這個(gè)接口
  AccelerateInterpolator        加速,開始時(shí)慢中間加速
  DecelerateInterpolator        減速娜谊,開始時(shí)快然后減速
  AccelerateDecelerateInterolator   先加速后減速确买,開始結(jié)束時(shí)慢,中間加速
  AnticipateInterpolator       反向 纱皆,先向相反方向改變一段再加速播放
  AnticipateOvershootInterpolator   反向加超越湾趾,先向相反方向改變,再加速播放派草,會(huì)超出目的值然后緩慢移動(dòng)至目的值
  BounceInterpolator          跳躍搀缠,快到目的值時(shí)值會(huì)跳躍,如目的值100近迁,后面的值可能依次為85艺普,77,70,80歧譬,90岸浑,100
  CycleInterpolator          循環(huán),動(dòng)畫循環(huán)一定次數(shù)瑰步,值的改變?yōu)橐徽液瘮?shù):Math.sin(2 * mCycles * Math.PI * input)
  LinearInterpolator         線性矢洲,線性均勻改變
 OvershootInterpolator       超越,最后超出目的值然后緩慢改變到目的值
2.組合動(dòng)畫

xml定義:

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
    android:shareInterpolator="true" >  
    <scale  ... />  
    <rotate ...  />  
    <translate ...  />  
    <alpha ...  />  
<!--   
 shareInterpolator:里面動(dòng)畫效果是否共享一個(gè)Interpolator
 不共享設(shè)置android:shareInterpolator="false"缩焦,每一個(gè)動(dòng)畫效果自己添加interpolator读虏。
 -->
</set>

代碼實(shí)現(xiàn):

AlphaAnimation alpha=new AlphaAnimation(0, 1);  
alpha.setDuration(1000); 
alpha.setStartOffset(1000); //延時(shí)一秒后播放
AnimationSet animSet = new AnimationSet(false);
animSet.addAnimation(alpha);
animSet.cancel();//取消動(dòng)畫
animSet.reset();//釋放資源

2.2 LayoutAnimation(GridLayoutAnimationController)

xml定義:

<?xml version="1.0" encoding="utf-8"?>  
<layoutAnimation  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:delay="0.5"  
    android:animationOrder="normal"  
    android:animation="@anim/alpha"  
    />  
<!--   
 delay:延遲多少開始播放
 -->

代碼實(shí)現(xiàn):

AlphaAnimation alpha=new AlphaAnimation(0, 1);  
alpha.setDuration(1000);  
LayoutAnimationController lac=new LayoutAnimationController(alpha);  
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);  
ListView.setLayoutAnimation(lac); 
LayoutAnimationController.ORDER_NORMAL;    //順序顯示
LayoutAnimationController.ORDER_REVERSE;   //反顯示
LayoutAnimationController.ORDER_RANDOM     //隨機(jī)顯示

使用:

<ListView  
        android:id="@+id/listView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"         
    android:layoutAnimation="@anim/layoutanimation"/>

2.逐幀動(dòng)畫 Drawable Animation(Frame Animation)
xml定義:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot="true">  
    <item android:drawable="@drawable/girl1" android:duration="100" />  
    <item android:drawable="@drawable/girl2" android:duration="100" />  
    <item android:drawable="@drawable/girl3" android:duration="100" />  
<!--   
 oneshot:如果為true袁滥,表示動(dòng)畫只播放一次停止在最后一幀上盖桥,如果設(shè)置為false表示動(dòng)畫循環(huán)播放
 -->
</animation-list>

代碼實(shí)現(xiàn):

AnimationDrawable anim = new AnimationDrawable();  
for (int i = 1; i <= 3; i++) {  
    int id = getResources().getIdentifier("girl" + i, "drawable", getPackageName());  
    Drawable drawable = getResources().getDrawable(id);  
    anim.addFrame(drawable, 100);  
}

播放

View.setBackgroundResource(R.anim.frame);  // 可以在xml里直接@drawble/
AnimationDrawable anim = (AnimationDrawable) image.getBackground();  
anim.start();  
注:想activity一開始就播放請放在onWindowFocusChanged方法里
View.setBackgroundResource(R.anim.frame);  // 可以在xml里直接@drawble/
AnimationDrawable anim = (AnimationDrawable) image.getBackground();  
anim.start();  
注:想activity一開始就播放請放在onWindowFocusChanged方法里

動(dòng)畫恢復(fù)到第一幀動(dòng)畫: anim.selectDrawable(0); //選擇當(dāng)前動(dòng)畫的第一幀,然后停止 anim.stop();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末题翻,一起剝皮案震驚了整個(gè)濱河市揩徊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌藐握,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垃喊,死亡現(xiàn)場離奇詭異猾普,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)本谜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門初家,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乌助,你說我怎么就攤上這事溜在。” “怎么了他托?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵掖肋,是天一觀的道長。 經(jīng)常有香客問我赏参,道長志笼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任把篓,我火速辦了婚禮纫溃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘韧掩。我一直安慰自己紊浩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著坊谁,像睡著了一般费彼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上呜袁,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天敌买,我揣著相機(jī)與錄音,去河邊找鬼阶界。 笑死虹钮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的膘融。 我是一名探鬼主播芙粱,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼氧映!你這毒婦竟也來了春畔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤岛都,失蹤者是張志新(化名)和其女友劉穎律姨,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體臼疫,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡择份,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了烫堤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荣赶。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖鸽斟,靈堂內(nèi)的尸體忽然破棺而出拔创,到底是詐尸還是另有隱情,我是刑警寧澤富蓄,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布剩燥,位于F島的核電站,受9級特大地震影響立倍,放射性物質(zhì)發(fā)生泄漏躏吊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一帐萎、第九天 我趴在偏房一處隱蔽的房頂上張望比伏。 院中可真熱鬧,春花似錦疆导、人聲如沸赁项。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽悠菜。三九已至舰攒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間悔醋,已是汗流浹背摩窃。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留芬骄,地道東北人猾愿。 一個(gè)月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像账阻,于是被迫代替她去往敵國和親蒂秘。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

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