【Android】執行個體 忐忑的精靈

來源:互聯網
上載者:User

標籤:art   tar   app   anim   修改   linear   www   odi   you   

在Android Studio中建立項目,名稱為“Animation And Multimedia”,然後在該項目中建立一個Module,名稱為“Frame-By-Frame Animation”。在該Module中實現本執行個體,具體步驟如下。

(1)首先在res\drawable目錄中添加一個名稱為fairy.xml的XML資源檔,然後在該檔案中定義組成動畫的圖片資源,具體代碼如下:

01   <?xml version="1.0" encoding="utf-8"?>02   <animation-list xmlns:android="http://schemas.android.com/apk/res/android">03       <item android:drawable="@drawable/img001" android:duration="60"/>04       <item android:drawable="@drawable/img002" android:duration="60"/>05       <item android:drawable="@drawable/img003" android:duration="60"/>06       <item android:drawable="@drawable/img004" android:duration="60"/>07       <item android:drawable="@drawable/img005" android:duration="60"/>08       <item android:drawable="@drawable/img006" android:duration="60"/>09   </animation-list>

  (2)修改建立項目的res\layout目錄下的布局檔案activity_main.xml,將預設添加的布局管理器修改為垂直線性布局管理器,並且在該布局管理器中將預設添加的TextView組件刪除,然後為修改後的線性布局管理器設定android:id和android:background屬性。將android:background屬性設定為步驟(1)中建立的動畫資源,修改後的代碼如下:

01   <LinearLayout02       android:id="@+id/linearLayout"03       xmlns:android="http://schemas.android.com/apk/res/android"04       xmlns:tools="http://schemas.android.com/tools"05       android:layout_width="match_parent"06       android:layout_height="match_parent"07       android:orientation="vertical"08       android:background="@drawable/fairy"09       tools:context="com.mingrisoft.MainActivity">10   </LinearLayout>

  (3)開啟預設建立的MainActivity,在該檔案中,首先定義一個私人的布爾型變數flag=true,用於判斷開始和停止動畫,然後在onCreate()方法中擷取垂直線性布局管理器與AnimationDrawable對象,再為布局管理器添加單擊事件,最後在onClick()方法中根據flag變數的值來實現動畫的開始與停止,具體代碼如下:

01   public class MainActivity extends AppCompatActivity {02       private boolean flag = true;03       @Override04       protected void onCreate(Bundle savedInstanceState) {05           super.onCreate(savedInstanceState);06           setContentView(R.layout.activity_main);07           //擷取布局管理器08           LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);09           //擷取AnimationDrawable對象10          final AnimationDrawable anim= (AnimationDrawable) linearLayout.getBackground();11           //為布局管理器添加單擊事件12           linearLayout.setOnClickListener(new View.OnClickListener() { 13               @Override14               public void onClick(View v) {15                   if(flag){16                       anim.start();            //開始播放動畫17                       flag=false;18                   }else {19                       anim.stop();            //停止播放動畫20                       flag=true;21                   }22               }23           });24       }25   }

(4)在AndroidManifest.xml檔案的<activity>標記中添加screenOrientation屬性,設定其橫屏顯示,關鍵代碼如下:

android:screenOrientation="landscape"

(5)在工具列中找到下拉式清單方塊,選擇要啟動並執行應用(這裡為Frame-By-Frame Animation),再單擊右側的按鈕,運行效果10.1所示。

 

圖10.1  忐忑的精靈

 

感謝讀者朋友對明日科技的支援,如果您對圖書中的講解及光碟片源碼有任何問題,可以直接登入http://www.mingrisoft.com上的社區進行發貼,把您的問題詳細的說明一下,我們的技術人員會在那裡給您回複!

本文摘自明日科技出版的《零基礎學Android》,李磊 轉載請註明出處!!!

【Android】執行個體 忐忑的精靈

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.