<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 編寫:[XizhiXu](https://github.com/XizhiXu) - 原文:[http://developer.android.com/training/animation/cardflip.html](http://developer.android.com/training/animation/cardflip.html) 這節課展示如何使用自定義 fragment 動畫實現卡片翻轉動畫。通過展示一個模擬卡片翻轉的動畫實現 view 內容的卡片翻轉效果。 下面是卡片翻轉動畫的樣子: 如果你想跳過看整個例子,[下載](http://developer.android.com/shareables/training/Animations.zip) App 樣例然后運行卡片翻轉例子。查看下列文件中的代碼實現: - src/CardFlipActivity.java - animator/card_flip_right_in.xml - animator/card_flip_right_out.xml - animator/card_flip_left_in.xml - animator/card_flip_left_out.xml - layout/fragment_card_back.xml - layout/fragment_card_front.xml ### 創建Animator 創建卡片翻轉動畫,你需要兩個 animator 讓前面的卡片向右翻轉消失,向左翻轉出現。你還需要兩個 animator 讓背面的卡片向左翻轉出現,向右翻轉消失。 **card_flip_left_in.xml** ~~~ <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--旋轉之前,立刻設置透明度alpha為0--> <objectAnimator android:valueFrom="1.0" android:valueTo="0.0" android:propertyName="alpha" android:duration="0" /> <!--旋轉--> <objectAnimator android:valueFrom="-180" android:valueTo="0" android:propertyName="rotationY" android:interpolator="@android:interpolator/accelerate_decelerate" android:duration="@integer/card_flip_time_full" /> <!--旋轉中途(時間偏移量取決于startOffset屬性)設置透明度為1--> <objectAnimator android:valueFrom="0.0" android:valueTo="1.0" android:propertyName="alpha" android:startOffset="@integer/card_flip_time_half" android:duration="1" /> </set> ~~~ **card_flip_left_out.xml** ~~~ <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 旋轉. --> <objectAnimator android:valueFrom="0" android:valueTo="180" android:propertyName="rotationY" android:interpolator="@android:interpolator/accelerate_decelerate" android:duration="@integer/card_flip_time_full" /> <!--旋轉中途(時間偏移量取決于startOffset屬性)設置透明度為0--> <objectAnimator android:valueFrom="1.0" android:valueTo="0.0" android:propertyName="alpha" android:startOffset="@integer/card_flip_time_half" android:duration="1" /> </set> ~~~ **card_flip_right_in.xml** ~~~ <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--旋轉之前,立刻設置透明度alpha為0--> <objectAnimator android:valueFrom="1.0" android:valueTo="0.0" android:propertyName="alpha" android:duration="0" /> <!-- 旋轉. --> <objectAnimator android:valueFrom="180" android:valueTo="0" android:propertyName="rotationY" android:interpolator="@android:interpolator/accelerate_decelerate" android:duration="@integer/card_flip_time_full" /> <!--旋轉中途(時間偏移量取決于startOffset屬性)設置透明度為1--> <objectAnimator android:valueFrom="0.0" android:valueTo="1.0" android:propertyName="alpha" android:startOffset="@integer/card_flip_time_half" android:duration="1" /> ~~~ **card_flip_right_out.xml** ~~~ <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 旋轉. --> <objectAnimator android:valueFrom="0" android:valueTo="-180" android:propertyName="rotationY" android:interpolator="@android:interpolator/accelerate_decelerate" android:duration="@integer/card_flip_time_full" /> <!--旋轉中途(時間偏移量取決于startOffset屬性)設置透明度為0--> <objectAnimator android:valueFrom="1.0" android:valueTo="0.0" android:propertyName="alpha" android:startOffset="@integer/card_flip_time_half" android:duration="1" /> </set> ~~~ ### 創建View 卡片的每一面是一個獨立包含你想要內容的布局,比如兩屏文字,兩張圖片,或者任何view的組合。然后你將在應用動畫的fragment里面用到這倆布局。下面的布局創建了卡片展示文本一面的布局: ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#a6c" android:padding="16dp" android:gravity="bottom"> <TextView android:id="@android:id/text1" style="?android:textAppearanceLarge" android:textStyle="bold" android:textColor="#fff" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/card_back_title" /> <TextView style="?android:textAppearanceSmall" android:textAllCaps="true" android:textColor="#80ffffff" android:textStyle="bold" android:lineSpacingMultiplier="1.2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/card_back_description" /> </LinearLayout> ~~~ 卡片另一面顯示一個 [ImageView](http://developer.android.com/reference/android/widget/ImageView.html): ~~~ <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image1" android:scaleType="centerCrop" android:contentDescription="@string/description_image_1" /> ~~~ ### 創建Fragment 為卡片正反面創建fragment,這些類從[ onCreateView() ](http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle))方法中分別為每個framgent返回你之前創建的布局。在父[activity](# "An activity represents a single screen with a user interface.")中,你可以在你想要顯示卡片時創建對應的 fragment 實例。下面的例子展示父[activity](# "An activity represents a single screen with a user interface.")內嵌套的fragment: ~~~ public class CardFlipActivity extends Activity { ... /** * 一個呈現在卡片前方的fragment */ public class CardFrontFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_card_front, container, false); } } /** * 一個呈現在卡片后方的fragment */ public class CardBackFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_card_back, container, false); } } } ~~~ ### 應用卡片翻轉動畫 現在,你需要在父[activity](# "An activity represents a single screen with a user interface.")中展示fragment。為做這件事,首先創建你[activity](# "An activity represents a single screen with a user interface.")的布局。下面例子創建了一個你可以在運行時添加fragment的 [FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html)。 ~~~ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> ~~~ 在[activity](# "An activity represents a single screen with a user interface.")代碼中,把剛創建的布局設置成content view。當[activity](# "An activity represents a single screen with a user interface.")創建時展示一個默認的fragment是個不錯的注意。所以下面的[activity](# "An activity represents a single screen with a user interface.")樣例告訴你如何默認顯示卡片正面: ~~~ public class CardFlipActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_card_flip); if (savedInstanceState == null) { getFragmentManager() .beginTransaction() .add(R.id.container, new CardFrontFragment()) .commit(); } } ... } ~~~ 既然現在顯示了卡片的正面,你可以在合適時機用翻轉動畫顯示卡片背面了。創建一個方法來顯示背面需要做下面這些事情: - 為fragment轉換設置你剛做的自定義動畫 - 用新fragment替換當前顯示的fragment,并且應用你剛創建的動畫到這個事件中。 - 添加之前顯示的fragment到fragment的back stack中,所以當用戶摁 _Back_ 鍵時,卡片會翻轉回來。 ~~~ private void flipCard() { if (mShowingBack) { getFragmentManager().popBackStack(); return; } // 向后翻轉. mShowingBack = true; // 創建并提交一個新的Fragment事務用于在卡片后面添加Fragment,使用自定義動畫,并且加入 // Fragment管理器回退棧 getFragmentManager() .beginTransaction() // 用動畫器資源呈現卡片自前向后的旋轉效果替換默認的Fragment動畫, // 當翻轉到前面的時候動畫器資源也可以呈現自后向前的旋轉效果(例如按下系統返回鍵時) .setCustomAnimations( R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out) // 用一個Fragment替換任何當前在容器布局內的Fragment來呈現下一頁 //(通過僅自增的變量currentPage來表示) .replace(R.id.container, new CardBackFragment()) // 添加這個事務到回退棧,允許用戶來按下返回按鈕來回退到卡片正面. .addToBackStack(null) // 提交完成事務. .commit(); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看