日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Android Developers Training】 69. 視圖切換

系統(tǒng) 3234 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛(ài)安卓而產(chǎn)生了翻譯的念頭,純屬個(gè)人興趣愛(ài)好。

原文鏈接: http://developer.android.com/training/animation/crossfade.html


淡入淡出動(dòng)畫(huà)(也稱作溶解效果):淡出一個(gè)組件并將另一個(gè)UI組件淡入的效果。淡入淡出效果一般來(lái)說(shuō)都非常的短小,但是能提供一種屏幕切換的流暢轉(zhuǎn)換。如果你不使用淡入淡出效果,屏幕切換回顯得很突兀。

這里是一個(gè)淡入淡出的例子,它從一個(gè)進(jìn)程指示器過(guò)度到文本內(nèi)容。

淡入淡出動(dòng)畫(huà)

如果你希望略過(guò)這部分內(nèi)容直接看代碼樣例,可以直接 下載 樣例代碼,然后選擇淡入淡出動(dòng)畫(huà)的例子。下面的文件是實(shí)現(xiàn)代碼:

  • src/CrossfadeActivity.java
  • layout/activity_crossfade.xml
  • menu/activity_crossfade.xml

一). 創(chuàng)建視圖

創(chuàng)建兩個(gè)你希望實(shí)現(xiàn)淡入淡出的視圖。下面的例子創(chuàng)建了一個(gè)進(jìn)度指示器和一個(gè)可滑動(dòng)的文本框:

      
        <
      
      
        FrameLayout 
      
      
        xmlns:android
      
      
        ="http://schemas.android.com/apk/res/android"
      
      
        

    android:layout_width
      
      
        ="match_parent"
      
      
        

    android:layout_height
      
      
        ="match_parent"
      
      
        >
      
      
        <
      
      
        ScrollView 
      
      
        xmlns:android
      
      
        ="http://schemas.android.com/apk/res/android"
      
      
        

        android:id
      
      
        ="@+id/content"
      
      
        

        android:layout_width
      
      
        ="match_parent"
      
      
        

        android:layout_height
      
      
        ="match_parent"
      
      
        >
      
      
        <
      
      
        TextView 
      
      
        style
      
      
        ="?android:textAppearanceMedium"
      
      
        

            android:lineSpacingMultiplier
      
      
        ="1.2"
      
      
        

            android:layout_width
      
      
        ="match_parent"
      
      
        

            android:layout_height
      
      
        ="wrap_content"
      
      
        

            android:text
      
      
        ="@string/lorem_ipsum"
      
      
        

            android:padding
      
      
        ="16dp"
      
      
        />
      
      
        </
      
      
        ScrollView
      
      
        >
      
      
        <
      
      
        ProgressBar 
      
      
        android:id
      
      
        ="@+id/loading_spinner"
      
      
        

        style
      
      
        ="?android:progressBarStyleLarge"
      
      
        

        android:layout_width
      
      
        ="wrap_content"
      
      
        

        android:layout_height
      
      
        ="wrap_content"
      
      
        

        android:layout_gravity
      
      
        ="center"
      
      
        />
      
      
        </
      
      
        FrameLayout
      
      
        >
      
    

二). 設(shè)置動(dòng)畫(huà)

要設(shè)置動(dòng)畫(huà)的話:

創(chuàng)建你希望實(shí)現(xiàn)淡入淡出的視圖的成員變量。當(dāng)在執(zhí)行動(dòng)畫(huà)期間改變視圖時(shí),你需要這些引用。

對(duì)于淡入的視圖,將它的可視性( visibility )設(shè)置為 GONE 。這回阻止這個(gè)視圖占據(jù)布局空間,然后再計(jì)算布局時(shí)將它省略,加速處理的速度。

緩存 config_shortAnimTime 這一系統(tǒng)屬性到一個(gè)成員變量中。這個(gè)屬性定義了一個(gè)標(biāo)準(zhǔn)的動(dòng)畫(huà)持續(xù)的一個(gè)較短的時(shí)間。這個(gè)持續(xù)的時(shí)間對(duì)于細(xì)微的動(dòng)畫(huà)效果或者那些頻繁出現(xiàn)的動(dòng)畫(huà)是比較合適的。你也可以使用 config_longAnimTime config_mediumAnimTime

這里的一個(gè)例子使用了上述的布局文件作為activity的布局:

      
        public
      
      
        class
      
       CrossfadeActivity 
      
        extends
      
      
         Activity {



    
      
      
        private
      
      
         View mContentView;

    
      
      
        private
      
      
         View mLoadingView;

    
      
      
        private
      
      
        int
      
      
         mShortAnimationDuration;



    ...



    @Override

    
      
      
        protected
      
      
        void
      
      
         onCreate(Bundle savedInstanceState) {

        
      
      
        super
      
      
        .onCreate(savedInstanceState);

        setContentView(R.layout.activity_crossfade);



        mContentView 
      
      =
      
         findViewById(R.id.content);

        mLoadingView 
      
      =
      
         findViewById(R.id.loading_spinner);



        
      
      
        //
      
      
         Initially hide the content view.
      
      
                mContentView.setVisibility(View.GONE);



        
      
      
        //
      
      
         Retrieve and cache the system's default "short" animation time.
      
      

        mShortAnimationDuration =
      
         getResources().getInteger(

                android.R.integer.config_shortAnimTime);

    }
      
    

三). 淡入淡出視圖

現(xiàn)在視圖已經(jīng)配置好了,根據(jù)下面的步驟實(shí)現(xiàn)淡入淡出:

  • 對(duì)于淡入的視圖,將alpha值設(shè)置為0,將可視性設(shè)置為 VISIBLE (記住在之前它被預(yù)設(shè)為 GONE ),這樣就使得這個(gè)視圖邊的可見(jiàn)但是是完全透明的。
  • 對(duì)于淡入的視圖,將它的alpha動(dòng)態(tài)地從0變化到1。同時(shí),對(duì)于淡出的視圖, 將它的alpha動(dòng)態(tài)地從1變化到0。
  • 在一個(gè) Animator.AnimatorListener 中使用 onAnimationEnd() ,將淡出的視圖的可視性設(shè)置為 GONE 。雖然它的alpha的值已經(jīng)變成0了,但是將視圖的可視性設(shè)置為 GONE 可以阻止它占據(jù)布局空間,并將它略出布局的計(jì)算過(guò)程,以此來(lái)提高程序的執(zhí)行性能。

下面的代碼是一個(gè)例子:

      
        private
      
      
         View mContentView;


      
      
        private
      
      
         View mLoadingView;


      
      
        private
      
      
        int
      
      
         mShortAnimationDuration;



...




      
      
        private
      
      
        void
      
      
         crossfade() {



    
      
      
        //
      
      
         Set the content view to 0% opacity but visible, so that it is visible

    
      
      
        //
      
      
         (but fully transparent) during the animation.
      
      
            mContentView.setAlpha(0f);

    mContentView.setVisibility(View.VISIBLE);



    
      
      
        //
      
      
         Animate the content view to 100% opacity, and clear any animation

    
      
      
        //
      
      
         listener set on the view.
      
      
            mContentView.animate()

            .alpha(1f)

            .setDuration(mShortAnimationDuration)

            .setListener(
      
      
        null
      
      
        );



    
      
      
        //
      
      
         Animate the loading view to 0% opacity. After the animation ends,

    
      
      
        //
      
      
         set its visibility to GONE as an optimization step (it won't

    
      
      
        //
      
      
         participate in layout passes, etc.)
      
      
            mLoadingView.animate()

            .alpha(0f)

            .setDuration(mShortAnimationDuration)

            .setListener(
      
      
        new
      
      
         AnimatorListenerAdapter() {

                @Override

                
      
      
        public
      
      
        void
      
      
         onAnimationEnd(Animator animation) {

                    mLoadingView.setVisibility(View.GONE);

                }

            });

}
      
    

【Android Developers Training】 69. 視圖切換的淡入淡出效果


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 泰州市| 霍邱县| 织金县| 莆田市| 仪陇县| 安吉县| 酒泉市| 宜川县| 桐乡市| 廊坊市| 台江县| 洪湖市| 体育| 金昌市| 崇明县| 南川市| 乐东| 邹城市| 疏勒县| 始兴县| 务川| 页游| 项城市| 全州县| 嘉义市| 元江| 澎湖县| 通辽市| 开鲁县| 汉中市| 白银市| 吴忠市| 收藏| 南郑县| 安陆市| 庆云县| 洞头县| 宝鸡市| 宜州市| 铁岭市| 高台县|