Animation與nineoldandroid項目 day02

來源:互聯網
上載者:User

標籤:建立   主題   android   項目開發   values   

一.  項目問題

1. 歡迎介面顯示時,使back鍵失效


二. 項目開發功能點:

1. 監聽按鍵事件

    在Activity中重寫onKeyDown()方法

      650) this.width=650;" style="width:604.43px;height:auto;" alt="A8C27CECA75049C2AC85D0CEFD777170" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/A8C27CECA75049C2AC85D0CEFD777170" />


2. 自訂佈景主題解決Android多版本片段化問題

    目的:在2.x中的主題是NoTitlebar,

              在4.x中的主題是NoActionbar

    Android系統的讀取資源的機制:

    apk的res目錄中有多個styles.xml,

    當apk運行在2.x版本中,Android系統會讀取values目錄中的           

    styles.xml

    當apk運行在4.x版本中,Android系統會讀取values-v14

    目錄中的styles.xml

          650) this.width=650;" style="width:158.93px;height:auto;" alt="1A5630C875C5456D85A3FFF4132128C5" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/1A5630C875C5456D85A3FFF4132128C5" />

    步驟:

    1) 在values/styles.xml檔案中建立一個主題

         650) this.width=650;" style="width:378.56px;height:auto;" alt="1D35161B509F48699C5804ADFD4E3919" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/1D35161B509F48699C5804ADFD4E3919" />

    2) 在values/styles.xml檔案中建立一個父主題

         650) this.width=650;" style="width:426.66px;height:auto;" alt="466FC885E012407F8491D346F9AE9F36" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/466FC885E012407F8491D346F9AE9F36" />

    3) 在values-v14/styles.xml檔案中建立一個父主題

         650) this.width=650;" style="width:501.48px;height:auto;" alt="2507E10A17D044EFB5E63BC582EE96B9" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/2507E10A17D044EFB5E63BC582EE96B9" />

    4)  在資訊清單檔中為歡迎頁面Activity配置新的主題

         650) this.width=650;" style="width:521.73px;height:auto;" alt="9A496FFCECCF461E81862C431D039D3D" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/9A496FFCECCF461E81862C431D039D3D" />

           

3. 動畫

    gif、flash

    原理:若干圖片按照順序和一定時間間隔(100毫秒)播放

    預設不支援gif,因為畫質較差(8位元影像,256色)

    

    Android中支援的動畫

    3.1 FrameAnimation

          功能:能夠實現任意複雜動畫

          實現步驟:

          1) 拷貝主要畫面格(Frame)

          2) 建立Android XML

                   Resource Type :Drawable

                   Root Element   :animation-list 

              650) this.width=650;" style="width:524px;height:auto;" alt="5665C9C9011B49139DE5E81C02A88ED0" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/5665C9C9011B49139DE5E81C02A88ED0" />

          3) 編輯圖片先後順序和切換時間間隔

              650) this.width=650;" style="width:537.03px;height:auto;" alt="E18606A007D14E28AD45AE5CF473257E" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/E18606A007D14E28AD45AE5CF473257E" />

             增加oneshot屬性,動畫只播放一次

              650) this.width=650;" style="width:319.34px;height:auto;" alt="6F9D0A8F79CD45719ECF8AE9AEF2A740" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/6F9D0A8F79CD45719ECF8AE9AEF2A740" />

           4) 得到動畫對象AnimationDrawable,播放

              650) this.width=650;" style="width:528px;height:auto;" alt="40C9C0AB362F45638F2DE16CADD4BC26" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/40C9C0AB362F45638F2DE16CADD4BC26" />


    注意:FrameAnimation圖片數量有限

    實際應用情境:局部的範圍內,使用少量圖片實現動畫

    

    3.2 TweenAnimation

    原理:通過改變View的屬性

              大小、位置、角度、透明度


    3.2.1 實現透明度動畫步驟:

    a) 代碼方式建立動畫對象

    650) this.width=650;" style="width:682.05px;height:auto;" alt="AA909B01573A4B93AD34258C08AF2307" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/AA909B01573A4B93AD34258C08AF2307" />


    b) XML方式建立動畫對象

        650) this.width=650;" style="width:509px;height:auto;" alt="5A2E47B781DB43168DA6BE59CA6C330A" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/5A2E47B781DB43168DA6BE59CA6C330A" />

       編輯Animation內容 

         650) this.width=650;" style="width:487.98px;height:auto;" alt="4B933B57D82942D5A90C2FF4A06737D9" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/4B933B57D82942D5A90C2FF4A06737D9" />

    代碼中將動畫xml轉換為Animation對象

         650) this.width=650;" style="width:526px;height:auto;" alt="E712FCCCC2DB41129D289B7301485C65" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/E712FCCCC2DB41129D289B7301485C65" />

    3.2.2  縮放動畫配置                     移動動畫配置 

        650) this.width=650;" style="width:288.38px;height:auto;" alt="8D521686056D4DCBA6DF714EC01F18CA" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/8D521686056D4DCBA6DF714EC01F18CA" />  650) this.width=650;" style="width:426.62px;height:auto;" alt="C2FC21A59B4B455AB21D55E8185F3C1C" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/C2FC21A59B4B455AB21D55E8185F3C1C" />

        旋轉動畫配置 650) this.width=650;" style="width:423.12px;height:auto;" alt="E928DAA57C9B46658F5747EDEB890774" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/E928DAA57C9B46658F5747EDEB890774" />


一個螢幕的寬度或高度:100%p


4. 快捷的註冊事件的方法

    1) 在布局中為控制項添加onClick屬性

          650) this.width=650;" style="width:455.43px;height:auto;" alt="FC3D989AF91640498B7BF0490A9363FA" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/FC3D989AF91640498B7BF0490A9363FA" />

    2)  在Activity中添加以onClick屬性值為名稱的方法

          650) this.width=650;" style="width:466.76px;height:auto;" alt="79D7725118484FCBACE77BE1D839859E" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/79D7725118484FCBACE77BE1D839859E" />

         注意:必須是public,參數必須是View v


5. 開源項目原理

   項目設定為庫,可被其他項目複用


6. nineoldandroid項目用法

  

Properties動畫    屬性動畫
Android3.x新增

問題:官方不支援2.x
使用開源項目

7. 歡迎頁面
    使用的控制項ViewPager
    libs/android-support-v4.jar
實現步驟:
1) 在布局中新增ViewPager控制項
    android.support.v4.view.ViewPager
    技巧:在代碼中輸入ViewPager,聯想後在import中
2) 初始化所有ViewPager頁面,並用ArrayList<View>儲存
3) 代碼中設定適配器
    mPager.setAdapter()
4) 重寫isViewFromObject()方法
    return arg0 == arg1;
5) 重寫instantiateItem()方法
    View view = mList.get(position);
    mPager.addView(view);
    return view;    
6) 重寫destroyItem()方法
    View view = mList.get(position);
    mPager.removeView(view);

// 改變ViewPager緩衝個數,

// 參數的含義是左右各緩衝幾個
   mPager.setOffscreenPageLimit(3);




java.lang.UnsupportedOperationException: Required method

destroyItem was not overridden

附:常見問題

目標:能夠解決所有講解過的FC問題


1. 代碼中引用R編譯錯誤,

   原因是:匯入了錯誤的R

       650) this.width=650;" style="width:369px;height:auto;" alt="49CC45D546344D3B870C893ABFF99121" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/49CC45D546344D3B870C893ABFF99121" />


2. java.lang.RuntimeException: Unable to instantiate(執行個體化) activity ComponentInfo{com.example.cw_0811/com.example.w_0811.MainActivity}: java.lang.ClassNotFoundException: com.example.w_0811.MainActivity


    FC的原因:運行時異常和Error

    查看日誌的方法:

    1) 拉到logcat最底部,向上找

        650) this.width=650;" style="width:255px;height:auto;" alt="984F162E25194824B1410054EA412AEB" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/984F162E25194824B1410054EA412AEB" />

        main是線程名稱,可以是其他

    2) 找異常類型名稱:

        650) this.width=650;" style="width:405px;height:auto;" alt="02A2630ABC3D4748AA466CD0A8F42E67" src="http://note.youdao.com/yws/public/resource/088e077f9af472c19e8f99579265d3a1/BCBA43691A214EB9A6C41A9956C74262/02A2630ABC3D4748AA466CD0A8F42E67" />

    3) ClassNotFoundException的原因是:

       代碼中沒有這個類,注意仔細檢查包名 + 類名


3. java.lang.RuntimeException: Unable to start activity ComponentInfo{org.yuchen.test/org.yuchen.test.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageView


原因:eclipse的ADT的bug

出現情境:修改布局而較少修改代碼

解決:project->clean



聯繫我們

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