FAB

时间:2016-10-31 10:58:00

标签: android user-interface transitions floating-action-button shared-element-transition

我在FAB按钮上设置了一个共享元素转换,它与圆形显示相结合。它启动并正常工作,但是当我想回到之前的活动时,FAB按钮消失并返回此错误。

这是我的第一个活动

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@color/off_white" />

<android.support.design.widget.TabLayout
    android:id="@+id/detail_tabs"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="bottom"
    android:background="?attr/colorPrimary"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom"
    app:tabGravity="fill"
    app:tabTextColor="@color/tab_unselected_text_color"
    app:tabSelectedTextColor="@color/tab_selected_text_color"
    android:layoutDirection="rtl"
    app:tabMode="scrollable" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">



            <ImageView
                android:id="@+id/backdrop"
                android:transitionName="profile1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:contentDescription="" />


            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradiant"/>

        </FrameLayout>



        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>




<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:transitionName="@string/transition_reveal1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|left"
    android:src="@mipmap/ic_map"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"

    app:backgroundTint="@android:color/holo_blue_bright" />

这是它的类

        FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            transitionToActivity(ActivityMAp.class, v,R.string.transition_reveal1);
        }
    });


////
private void transitionToActivity(Class target, View viewHolder, int transitionName) {
    final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, false,
            new Pair<>(viewHolder, this.getString(transitionName)));
    startActivity(target, pairs);
}
private void startActivity(Class target, Pair<View, String>[] pairs) {
    Intent i = new Intent(this, target);
    Bundle b = new Bundle();
    b.putString("EXTRA_MALLid",mall.getMallId());
    b.putString("EXTRA_FLOORID","1");
    b.putString("EXTRA_SHOP_ID","1");
    i.putExtras(b);

    ActivityOptionsCompat transitionActivityOptions =   ActivityOptionsCompat.makeSceneTransitionAnimation(this, pairs);
    this.startActivity(i, transitionActivityOptions.toBundle());
}

这是第二项活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
android:id="@+id/CoordinatorLayout01"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<FrameLayout
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue">

    <android.support.v4.view.ViewPager
        android:id="@+id/Tabviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/shared_target"
            android:src="@drawable/circle_24dp"
            android:transitionName="@string/transition_reveal1"
            android:layout_height="48dp"
            android:layout_width="48dp"
            android:backgroundTint="@color/blue"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="110dp" />

    </RelativeLayout>

及其类

public class ActivityMAp extends AppCompatActivity {


    private String mMall;
    private String mFloor;
    private String mShop;
    private FrameLayout rootLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        mMall = bundle.getString("EXTRA_MALLid");
        mFloor = bundle.getString("EXTRA_FLOORID");
        mShop = bundle.getString("EXTRA_SHOP_ID");

        setContentView(R.layout.activity_map);
        ViewPager TabviewPager = (ViewPager) findViewById(R.id.Tabviewpager);
        if (TabviewPager != null) {
            setupViewPager(TabviewPager);
        }


        rootLayout = (FrameLayout) findViewById(R.id.root_layout);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            setupWindowAnimations();
        }

    }

    private Interpolator interpolator;


    private void setupWindowAnimations() {
        interpolator = AnimationUtils.loadInterpolator(this, android.R.interpolator.linear_out_slow_in);
        setupEnterAnimations();
        setupExitAnimations();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setupEnterAnimations() {
        Transition transition = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            transition = TransitionInflater.from(this).inflateTransition(R.transition.changebounds_with_arcmotion);

            getWindow().setSharedElementEnterTransition(transition);
            transition.addListener(new Transition.TransitionListener() {
                @Override
                public void onTransitionStart(Transition transition) {
                }

                @Override
                public void onTransitionEnd(Transition transition) {
                    // Removing listener here is very important because shared element transition is executed again backwards on exit. If we don't remove the listener this code will be triggered again.
                    transition.removeListener(this);
                    hideTarget();
                    animateRevealShow(findViewById(R.id.shared_target));
                    animateButtonsIn();
                }

                @Override
                public void onTransitionCancel(Transition transition) {
                }

                @Override
                public void onTransitionPause(Transition transition) {
                }

                @Override
                public void onTransitionResume(Transition transition) {
                }
            });

        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void animateRevealShow(View viewRoot) {
//        int cx = (viewRoot.getLeft()) ;
        int cx = (viewRoot.getLeft() + viewRoot.getRight()) / 2;
//        int cy = ( viewRoot.getBottom()) ;
        int cy = (viewRoot.getTop() + viewRoot.getBottom()) / 2;
        int finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight());

        Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius);
//        viewRoot.setVisibility(View.GONE);
        anim.setDuration(getResources().getInteger(R.integer.anim_duration_long));
        anim.setInterpolator(new AccelerateInterpolator());
        anim.start();
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setupExitAnimations() {
        Fade returnTransition = new Fade();
        getWindow().setReturnTransition(returnTransition);
        returnTransition.setDuration(getResources().getInteger(R.integer.anim_duration_medium));
        returnTransition.setStartDelay(getResources().getInteger(R.integer.anim_duration_medium));
        returnTransition.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
                transition.removeListener(this);
                animateButtonsOut();
                animateRevealHide(rootLayout);
            }

            @Override
            public void onTransitionEnd(Transition transition) {
            }

            @Override
            public void onTransitionCancel(Transition transition) {
            }

            @Override
            public void onTransitionPause(Transition transition) {
            }

            @Override
            public void onTransitionResume(Transition transition) {
            }
        });
    }

    private void hideTarget() {
        findViewById(R.id.shared_target).setVisibility(View.GONE);
    }


    private void setupViewPager(ViewPager viewPager) {
        bootomsheetcontent.Adapter adapter = new bootomsheetcontent.Adapter(getSupportFragmentManager());

//        adapter.addFragment(new FragMalls(),getResources().getString( R.string.malls));

//        adapter.addFragment(new FragBeacons(), getResources().getString(R.string.goods));

        adapter.addFragment(new FragMap(), getResources().getString(R.string.map));
        viewPager.setAdapter(adapter);
    }

    private void animateButtonsOut() {
        for (int i = 0; i < rootLayout.getChildCount(); i++) {
            View child = rootLayout.getChildAt(i);
            child.animate()
                    .setStartDelay(i)
                    .setInterpolator(interpolator)
                    .alpha(0)
                    .scaleX(0f)
                    .scaleY(0f);
        }
    }
    private static final int DELAY = 100;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void animateRevealHide(final View viewRoot) {
        int cx = (viewRoot.getLeft() + viewRoot.getRight()) / 2;
        int cy = (viewRoot.getTop() + viewRoot.getBottom()) / 2;
        int initialRadius = viewRoot.getWidth();

        Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, initialRadius, 0);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                viewRoot.setVisibility(View.INVISIBLE);
            }
        });

    anim.setDuration(getResources().getInteger(R.integer.anim_duration_medium));
        anim.start();
    }
    private void animateButtonsIn() {
        for (int i = 0; i < rootLayout.getChildCount(); i++) {
            View child = rootLayout.getChildAt(i);
            child.animate()
                    .setStartDelay(100 + i * DELAY)
                    .setInterpolator(interpolator)
                    .alpha(1)
                    .scaleX(1)
                    .scaleY(1);
        }
    }

}

事实上,当我点击fab时,它会打开一个新的活动并在循环显示运行后运行共享事务。这工作正常。但是当我再次按回按钮时,它会运行循环显示而不是交易。交易后FAb消失并发出错误。

 Process: ir.gfpishro.signal, PID: 20087
                                                                java.lang.IllegalArgumentException: Rect should intersect with child's bounds.
                                                                    at android.support.design.widget.CoordinatorLayout.offsetChildByInset(CoordinatorLayout.java:1319)
                                                                    at android.support.design.widget.CoordinatorLayout.onChildViewsChanged(CoordinatorLayout.java:1257)
                                                                    at android.support.design.widget.CoordinatorLayout$OnPreDrawListener.onPreDraw(CoordinatorLayout.java:1805)
                                                                    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)

0 个答案:

没有答案