頁面轉(zhuǎn)場基本過渡動畫

注:要Android 5.0 及以上才支持

1、設置支持動畫

在styles里里面設置

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

<!--        <item name="android:windowNoTitle">true</item>-->
        <item name="windowNoTitle">true</item>
<!--        <item name="android:windowActionBar">false</item>-->

        //設置支持動畫
        <item name="android:windowContentTransitions">true</item>
        <!--動畫重疊,默認true,false就是要等另一個動畫走完了才走該動畫-->
<!--        <item name="android:windowAllowEnterTransitionOverlap">false</item>-->
<!--        <item name="android:windowAllowReturnTransitionOverlap">false</item>-->
    </style>

</resources>
2、定義動畫
         // 側(cè)滑動畫
        Slide transition = new Slide();
        transition.setSlideEdge(Gravity.LEFT);
        // 爆炸效果的動畫
        Explode transition2 = new Explode();
        // 漸變動畫
        Fade transition3 = new Fade();
        transition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
        transition2.setDuration(getResources().getInteger(R.integer.anim_duration_long));
        transition3.setDuration(getResources().getInteger(R.integer.anim_duration_long));

3、設置動畫

頁面繼承AppCompatActivity,否則要加判斷 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

        getWindow().setExitTransition(transition); //跳轉(zhuǎn)時的退出時動畫
        getWindow().setReenterTransition(transition2); //重新進入時動畫
        getWindow().setEnterTransition(transition); //進入時的動畫
        getWindow().setReturnTransition(transition2); //返回時的退出動畫
4、頁面跳轉(zhuǎn)

Pair是共享動畫的共享元素,沒有共享動畫元素 可不傳,下面再說共享動畫

 //Pair是共享動畫的共享元素,沒有共享動畫元素 可不傳
 Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, false, new Pair<View, String>(tv1, tv1.getTransitionName()));
                ActivityOptionsCompat aoc = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pairs);
                startActivity(new Intent(this, TwoActivity.class), aoc.toBundle());

5、頁面關(guān)閉

supportFinishAfterTransition();
如果頁面沒有繼承AppCompatActivity,就這樣寫

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else
            super.finish();

上面用到的工具類,主要是處理狀態(tài)欄和導航欄是否也要跟著動畫

/**
 * Wing_Li
 * 2016/9/14.
 * 共享過渡動畫加入狀態(tài)欄和導航欄,這樣頁面轉(zhuǎn)場動畫時 狀態(tài)欄和導航欄 不會跟著動
 */
public class TransitionHelper {
    /**
     * 創(chuàng)建所需的活動過渡期間避免與系統(tǒng)UI小過渡的參與者。
     *
     * @param activity         The activity used as start for the transition.
     * @param includeStatusBar 如果是錯誤的,狀態(tài)欄將不會被添加為過渡參與者
     * @return All transition participants.
     */
    public static Pair<View, String>[] createSafeTransitionParticipants(@NonNull AppCompatActivity activity, boolean
            includeStatusBar, @Nullable Pair... otherParticipants) {
        // Avoid system UI glitches as described here:
        // https://plus.google.com/+AlexLockwood/posts/RPtwZ5nNebb
        View decor = activity.getWindow().getDecorView();
        View statusBar = null;
        if (includeStatusBar) {
            statusBar = decor.findViewById(android.R.id.statusBarBackground);
        }
        View navBar = decor.findViewById(android.R.id.navigationBarBackground);

        // 創(chuàng)建一對過渡參與者。
        List<Pair> participants = new ArrayList<>(3);
        addNonNullViewToTransitionParticipants(statusBar, participants);
        addNonNullViewToTransitionParticipants(navBar, participants);
        // only add transition participants if there's at least one none-null element
        // 只有添加過渡參與者,如果至少有一個非空元素
        if (otherParticipants != null && !(otherParticipants.length == 1 && otherParticipants[0] == null)) {
            participants.addAll(Arrays.asList(otherParticipants));
        }
        return participants.toArray(new Pair[participants.size()]);
    }

    private static void addNonNullViewToTransitionParticipants(View view, List<Pair> participants) {
        if (view == null) {
            return;
        }
        participants.add(new Pair<>(view, view.getTransitionName()));
    }

}

至此,已經(jīng)實現(xiàn)了基本的轉(zhuǎn)場過渡動畫了,
添加共享動畫元素 :

兩個控件的屬性android:transitionName 的值 一樣即可:

1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容