Android 碎片過(guò)渡

2018-10-22 15:09 更新

Android 碎片過(guò)渡

Lollipop (Android 5.0) 中的活動(dòng)和碎片過(guò)渡建立在一些相對(duì)新的 Android 特性上,叫做 過(guò)渡。在 KitKat 中引入,過(guò)渡框架提供了一系列方便的 API 來(lái)在應(yīng)用程序的不同的 UI 狀態(tài)間 提供動(dòng)畫(huà)。這個(gè)框架?chē)@兩個(gè)關(guān)鍵概念構(gòu)建:場(chǎng)景和過(guò)渡。場(chǎng)景定義了應(yīng)用程序 UI 給定的狀態(tài),過(guò)渡則定義了兩個(gè)場(chǎng)景間的動(dòng)畫(huà)變化。

當(dāng)一個(gè)場(chǎng)景變化,過(guò)渡有兩個(gè)主要職責(zé):

  • 捕獲開(kāi)始和結(jié)束場(chǎng)景中每個(gè)視圖的狀態(tài)
  • 創(chuàng)建一個(gè)動(dòng)畫(huà)繪制者 (Animator),基于從一個(gè)場(chǎng)景到另一個(gè)場(chǎng)景需要?jiǎng)赢?huà)的視圖的不同。

實(shí)例

這個(gè)實(shí)例解釋如何使用碎片過(guò)渡來(lái)創(chuàng)建自定義的動(dòng)畫(huà)。讓我們按照下面的步驟開(kāi)始:

步驟 描述
1 使用 Android Studio 來(lái)創(chuàng)建 Android 應(yīng)用程序,命名為 Fragment Custom Animation,包名為 cn.uprogrammer.fragmentcustomanimation.
2 修改 res/layout/activity_main.xml 文件,添加一個(gè) TextView
3 在 res/layout/ 下創(chuàng)建 fragment_stack.xml 的布局文件,定義碎片標(biāo)簽和按鈕標(biāo)簽
4 在 res/ 下建立子目錄 anim,并添加 fragment_slide_left.xml, fragment_slide_left_exit.xml, fragment_slide_right_exit.xml 和 fragment_slide_left_enter.xml
5 在 MainActivity.java 中需要添加碎片棧,碎片管理器和 onCreateView()
6 啟動(dòng)Android模擬器來(lái)運(yùn)行應(yīng)用程序,并驗(yàn)證應(yīng)用程序所做改變的結(jié)果。

以下是 res/layout/activity_main.xml 文件的內(nèi)容,其包含幀布局和按鈕。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/new_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加新碎片" />

</LinearLayout>

以下是 res/anim/fragment_stack.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>
</LinearLayout>

以下是 res/animator/fragment_slide_left_enter.xml 文件的內(nèi)容,其包含設(shè)置方法和動(dòng)畫(huà)繪制者對(duì)象標(biāo)簽。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="100dp" android:valueTo="0dp"
        android:valueType="floatType"
        android:propertyName="translationX"
        android:duration="@android:integer/config_mediumAnimTime" />

    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0.0" android:valueTo="1.0"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

以下是 res/animator/fragment_slide_left_exit.xml 文件的內(nèi)容,其包含設(shè)置方法和動(dòng)畫(huà)繪制者對(duì)象標(biāo)簽。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0dp" android:valueTo="-100dp"
        android:valueType="floatType"
        android:propertyName="translationX"
        android:duration="@android:integer/config_mediumAnimTime" />

    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="1.0" android:valueTo="0.0"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

以下是 res/animator/fragment_slide_right_enter.xml 文件的內(nèi)容,其包含設(shè)置方法和動(dòng)畫(huà)繪制者對(duì)象標(biāo)簽。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="-100dp" android:valueTo="0dp"
        android:valueType="floatType"
        android:propertyName="translationX"
        android:duration="@android:integer/config_mediumAnimTime" />

    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0.0" android:valueTo="1.0"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

以下是 res/animator/fragment_slide_right_exit.xml 文件的內(nèi)容,其包含設(shè)置方法和動(dòng)畫(huà)繪制者對(duì)象標(biāo)簽。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0dp" android:valueTo="100dp"
        android:valueType="floatType"
        android:propertyName="translationX"
        android:duration="@android:integer/config_mediumAnimTime" />

    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="1.0" android:valueTo="0.0"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

以下是 src/cn.uprogrammer.fragmentcustomanimation/MainActivity.java 文件的內(nèi)容,包含按鈕監(jiān)聽(tīng)器,CountingFragment 和 onCreateView():

package cn.uprogrammer.fragmentcustomanimation;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.TextView;

/**
 * 演示在碎片事務(wù)中使用自定義動(dòng)畫(huà).
 */
public class MainActivity extends Activity {
    int mStackLevel = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.new_fragment);

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                addFragmentToStack();
            }
        });

        if (savedInstanceState == null) {
            // 添加初始碎片
            Fragment newFragment = CountingFragment.newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.fragment1, newFragment).commit();
        }
        else
        {
            mStackLevel = savedInstanceState.getInt("level");
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("level", mStackLevel);
    }

    void addFragmentToStack() {
        mStackLevel++;

        // 實(shí)例化新的碎片
        Fragment newFragment = CountingFragment.newInstance(mStackLevel);

        // 添加碎片到活動(dòng),并將其放入后退棧中
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_left_exit,
                R.animator.fragment_slide_right_enter,
                R.animator.fragment_slide_right_exit);
        ft.replace(R.id.fragment1, newFragment);
        ft.addToBackStack(null);
        ft.commit();
    }

    public static class CountingFragment extends Fragment {
        int mNum;
        /**
         * 創(chuàng)建CountingFragment的實(shí)例,提供"num"作為參數(shù)
         */
        static CountingFragment newInstance(int num) {
            CountingFragment f = new CountingFragment();

            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);
            return f;
        }

        /**
         * 在創(chuàng)建時(shí),獲取實(shí)例的number參數(shù).
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }
        /**
         * 碎片的界面僅包含一個(gè)TextView,用于顯示number
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_stack, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #" + mNum);
            tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
            return v;
        }
    }
}

以下是 AndroidManifest.xml 文件的內(nèi)容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.uprogrammer.fragmentcustomanimation"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>
</manifest>

讓我們運(yùn)行剛剛修改的 Fragment Custom Animation 應(yīng)用程序。我假設(shè)你已經(jīng)在安裝環(huán)境時(shí)創(chuàng)建了 AVD。打開(kāi)你的項(xiàng)目中的活動(dòng)文件,點(diǎn)擊工具欄中的 圖標(biāo)來(lái)在 Android Studio 中運(yùn)行應(yīng)用程序。Android Studio 在 AVD 上安裝應(yīng)用程序并啟動(dòng)它。如果一切順利,將在模擬器窗口上顯示如下:

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)