支持库:FragmentTransaction动画不起作用

时间:2012-01-26 10:33:01

标签: android android-fragments android-animation android-maps

我正在使用Peter Doyle的android-support-v4-googlemaps支持库来实现同时使用Fragments和Google Maps的Activity,并且似乎无法使FragmentTransaction动画生效。我尝试过使用setCustomAnimations(int enter, int exit)方法以及setTransition(int transit)方法,但无济于事。任何人都能够制作动画,或者在制作动画时遇到问题?

我尝试过的一些动画:

setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)

setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)

setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)

3 个答案:

答案 0 :(得分:13)

您应首先调用 FragmentTransaction。 setCustomAnimations 然后调用 FragmentTransaction。替换 方法如下:

        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.fade_out,R.anim.fade_in);
        ft.replace(R.id.fragmentDetails, detailsFrag);

答案 1 :(得分:1)

您是否尝试过FragmentTransaction.remove()然后FragmentTransaction.add()而不是FragmentTransaction.replace()?我在其他主题中看到抱怨replace()没有按预期工作。

我没有使用android-support-v4-googlemaps库,但我可以确认以下代码适用于android-support-v4.jar

FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
tx.replace(R.id.fragment_container, new Fragment2());
tx.addToBackStack(null);
tx.commit();

答案 2 :(得分:0)

尝试制作Google地图的快照:

private void snapShot() {
    SnapshotReadyCallback callback = new SnapshotReadyCallback() {
        Bitmap bitmap;

        @Override
        public void onSnapshotReady(Bitmap snapshot) {
            // TODO Auto-generated method stub
            bitmap = snapshot;
            try {
                FileOutputStream out = new FileOutputStream(getActivity()
                        .getFilesDir() + "/MapSnapshot.png");
                   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            } catch (Exception e) {
                   e.printStackTrace();
            }
        }
    };

    map.snapshot(callback);

}

创建一个只包含地图图片的新片段。使用replace加载此新片段,然后对要替换的片段进行转换:     final SnapShotFragment snapFrag = new SnapShotFragment();                             FragmentTransaction事务= getFragmentManager()                                     .beginTransaction();

                        transaction.replace(MapFragment.this.getId(),
                                snapFrag);
                        transaction.addToBackStack(null);
                        transaction.commit();
                        getFragmentManager().executePendingTransactions();
                        final boolean roi = isInROI;

                        WayPointDetailActivity waypointFrag = new WayPointDetailActivity();
                        waypointFrag.setWayPointId(wp.getId());
                        waypointFrag.setInRoi(roi);
                        transaction = getFragmentManager()
                                .beginTransaction();

                        transaction.setCustomAnimations(R.anim.enter,
                                R.anim.exit);

                        transaction.replace(snapFrag.getId(), waypointFrag);
                        transaction.addToBackStack(null);
                        transaction.commit();