2018-04-23

補間動畫(Tween Animation)

 包括  (1)透明度漸變動畫  AlphaAnimation
       (2)旋轉動畫  RotateAnimation
       (3)縮放動畫  ScaleAnimation
       (4)平移動畫  TranslateAnimation

1.做出如圖所示的界面兽埃,然后給各個按鈕加上onclick檩禾,對按鈕進行監(jiān)聽:


圖1-13.png

代碼如下:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="145dp"
    android:src="@drawable/a09" />

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:orientation="horizontal" >

     <Button
         android:id="@+id/button1"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="透明"
         android:onClick="Alpha" />

     <Button
         android:id="@+id/button2"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="旋轉" 
         android:onClick="Rotate"/>

     <Button
         android:id="@+id/button3"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="縮放" 
         android:onClick="Scale"/>

     <Button
         android:id="@+id/button4"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="平移"
         android:onClick="Translate" />

     <Button
         android:id="@+id/button5"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="全部" 
         android:onClick="All"/>
 </LinearLayout>

 </RelativeLayout>

2.在MainActivity中編寫各個按鈕應實現(xiàn)的功能
代碼如下:
先找到圖片斜做,以便把動畫效果運用到圖片上:

 public class MainActivity extends Activity { 
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView) findViewById(R.id.imageView1);
}

(1)透明度漸變動畫 AlphaAnimation

  public void Alpha(View view) {
    AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);// 1.0不透明的,0.0透明的
    aa.setDuration(2000);// 設置動畫的持續(xù)時間,毫秒
    aa.setRepeatCount(1);// 設置動畫重復的次數(shù) 重復兩次
    aa.setRepeatMode(Animation.REVERSE);// 設置動畫重
       復模式 REVERSE相反的回來一次 從有到無谈为,從無到有
    img.startAnimation(aa); // 把動畫應用到圖片上
}

(2)旋轉動畫 RotateAnimation

 public void Rotate(View view) {
    // 以左上角為中心點進行旋轉360度
    // RotateAnimation ra=new RotateAnimation(0.0f, 360.0f);
    // 以自身中心點進行旋轉
    RotateAnimation ra = new RotateAnimation(0.0f, 360.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    ra.setDuration(2000);
    ra.setRepeatCount(3); // 轉四圈
    ra.setRepeatMode(Animation.REVERSE);
    img.startAnimation(ra);
}

(3)縮放動畫 ScaleAnimation

 public void Scale(View view) {
    // 以左上角為縮放點
    // ScaleAnimation sc=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f);
    // 以自身中心點為縮放點
    ScaleAnimation sc = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    sc.setDuration(2000);
    sc.setRepeatCount(3);
    sc.setRepeatMode(Animation.REVERSE);
    img.startAnimation(sc);
}

(4)平移動畫 TranslateAnimation

 public void Translate(View view) {
    TranslateAnimation tr = new TranslateAnimation(
         //x軸從-2.5到2.5旅挤,y軸從-2.0到2.0
            Animation.RELATIVE_TO_SELF, -2.5f, 
            Animation.RELATIVE_TO_SELF, 2.5f,
            Animation.RELATIVE_TO_SELF, -2.0f,
            Animation.RELATIVE_TO_SELF, 2.0f);

    tr.setDuration(5000);
    tr.setRepeatCount(1);
    tr.setRepeatMode(Animation.REVERSE);
 // tr.setFillAfter(true);//走到哪之后,停下來
    img.startAnimation(tr);

 }

5.把幾種動畫效果合起來

 public void All(View view) {
    ScaleAnimation sc = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    sc.setDuration(2000);
    sc.setRepeatCount(3);
    sc.setRepeatMode(Animation.REVERSE);
         
    RotateAnimation ra = new RotateAnimation(0.0f, 360.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    ra.setDuration(2000);
    ra.setRepeatCount(3); // 轉四圈
    ra.setRepeatMode(Animation.REVERSE);

    AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);// 1.0不透明的,0.0透明的
    aa.setDuration(2000);// 設置動畫的持續(xù)時間伞鲫,毫秒
    aa.setRepeatCount(3);// 設置動畫重復的次數(shù) 重復兩次
    aa.setRepeatMode(Animation.REVERSE);// 設置動畫重
     復模式 REVERSE相反的回來一次 從有到無粘茄,從無到有

    AnimationSet as = new AnimationSet(true); // 創(chuàng)建一個動畫集合,把兩種動畫合在
                                                 一起
    as.addAnimation(sc);
    as.addAnimation(ra);
    as.addAnimation(aa);
    img.setAnimation(as);

}

運行結果如圖所示:

(1)
圖1-14.png

(2)
圖1-15.png

(3)
圖1-19.jpg

(4)
圖1-16.png

圖1-17.png

(5)
圖1-18.jpg

本節(jié)知識點:

1. setDuration(2000);// 設置動畫的持續(xù)時間秕脓,毫秒
2. setRepeatCount(3);// 設置動畫重復的次數(shù) 
3.setRepeatMode(Animation.REVERSE);// 設置動畫重復模式  REVERSE相反的回來一次 
從有到無柒瓣,從無到有
4.img.startAnimation(aa); // 把動畫應用到圖片上
5.AnimationSet as = new AnimationSet(true); // 創(chuàng)建一個動畫集合,把兩種動畫合在一
                                               起
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吠架,一起剝皮案震驚了整個濱河市芙贫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌傍药,老刑警劉巖磺平,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拐辽,居然都是意外死亡拣挪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門俱诸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來菠劝,“玉大人,你說我怎么就攤上這事乙埃≌⒂ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵介袜,是天一觀的道長甫何。 經(jīng)常有香客問我,道長遇伞,這世上最難降的妖魔是什么辙喂? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上巍耗,老公的妹妹穿的比我還像新娘秋麸。我一直安慰自己,他們只是感情好炬太,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布灸蟆。 她就那樣靜靜地躺著,像睡著了一般亲族。 火紅的嫁衣襯著肌膚如雪炒考。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天霎迫,我揣著相機與錄音斋枢,去河邊找鬼。 笑死知给,一個胖子當著我的面吹牛瓤帚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涩赢,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼戈次,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谒主?” 一聲冷哼從身側響起朝扼,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎霎肯,沒想到半個月后擎颖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡观游,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年搂捧,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片懂缕。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡允跑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出搪柑,到底是詐尸還是另有隱情聋丝,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布工碾,位于F島的核電站弱睦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏渊额。R本人自食惡果不足惜况木,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一垒拢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧火惊,春花似錦求类、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至张症,卻和暖如春仓技,著一層夾襖步出監(jiān)牢的瞬間鸵贬,已是汗流浹背俗他。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留阔逼,地道東北人兆衅。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像嗜浮,于是被迫代替她去往敵國和親羡亩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

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