将setVisibility(View.INVISIBLE)与pre Lollipop Circular Reveal库一起使用

时间:2015-09-28 11:04:11

标签: android android-fragments material-design circularreveal

我正在开发一个项目,我希望根据Material Design使用Circular Reveal效果。项目的minSDK = 11,所以为了与前Lollipop设备兼容,我正在使用这个库https://github.com/ozodrukh/CircularReveal

我有一个带有FloatingActionButton的片段,当被点击时,会在CardView中自我转换,如此处所述FAB transformations

卡片显示后,它有一个按钮可以恢复动画,将卡片重新转换为FAB。现在我的问题是:让我们说用户点击FAB并显示CardView。现在用户旋转他的设备,因此活动重置片段。我想要实现的是卡片保持可见和显示,而FAB应该被禁用和隐藏。问题是,如果我只是在我的FAB上使用setVisibility(View.INVISIBLE)它不起作用(请注意,如果我在设置隐形后使用getVisibility(),它会正确地返回值4 ==查看。不可见,但工厂仍然可见)。我要将setVisibility(...)调用包含在postDelayed()内至少50-100毫秒的延迟,以使晶圆厂不可见。

所以我的问题是:我做得对吗还是有更好的方法来完成我想要的东西(因为它对我来说似乎很难看)?

这是一些代码。这是片段的XML布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/my_appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_expanded_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="70dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ToolbarPopupTheme"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    ...
    <io.codetail.widget.RevealFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/my_fragment" />
    </io.codetail.widget.RevealFrameLayout>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:fabSize="mini"
        app:layout_anchor="@+id/my_appbar"
        app:layout_anchorGravity="bottom|left|start" />
</android.support.design.widget.CoordinatorLayout>

这是附带的CardView的XML布局:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_gravity="center"
    card_view:cardCornerRadius="2dp"
    android:visibility="invisible"
    android:focusableInTouchMode="true">

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

        ...
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="end">
            <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Ok" />
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel" />
        </LinearLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

这是我片段的代码:

public class MyFragment extends Fragment {

    private static final String CARD_OPEN_TAG = "CARD_OPEN_TAG";

    public static MyFragment newInstance(){
        return new MyFragment();
    }

    private int cardOpen;
    private FloatingActionButton fabAddPublication;
    private CardView card;
    private Button cardCancel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_magazines, container, false);
        toolbar = (Toolbar) rootView.findViewById(R.id.layout);

        ...

        // Initialize view status
        if (savedInstanceState != null){
            cardOpen = savedInstanceState.getInt(CARD_OPEN_TAG);
        } else {
            cardOpen = -1;
        }

        ...

        // Get FAB reference
        fab = (FloatingActionButton) rootView.findViewById(R.id.fab_id);
        // Get card reference
        card = (CardView) rootView.findViewById(R.id.card_id);
        editorPublication.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Using this event because I need my card to be measured to move correctly fab at his center
                if (cardOpen != -1){
                    // Move FAB to center of card
                    fab.setTranslationX(coordX); // WORKS
                    fab.setTranslationY(coordY); // WORKS
                    // fab.setVisibility(View.INVISIBLE) -> DOESN'T WORK, fab remain visible on top and at center of my card
                    // Ugly workaround
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // Hide FAB
                            fab.setVisibility(View.INVISIBLE);
                        }
                    }, 50); // Sometimes fails: if device/emulator use too much time to "rotate" screen, fab stay visible
                    // Remove listener
                    ViewTreeObserver obs = card.getViewTreeObserver();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                        obs.removeOnGlobalLayoutListener(this);
                    else obs.removeGlobalOnLayoutListener(this);
                }
            }
        });
        if (editorOpen != -1){
            fab.setEnabled(false); // WORKS
            card.setVisibility(View.VISIBLE); // WORKS
        }
        // Get editors buttons reference
        cardCancel = (Button) card.findViewById(R.id.card_cancel_id);
        // Set FAB listener
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Explode FAB
                explodeFab(fab, card); // This method trigger the reveal animation
                cardOpen = card.getId();
            }
        });
        // Set editors button listeners
        cardCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Implode FAB
                implodeFAB(fab, card); // This card reverts the reveal animation
        cardOpen = -1;
            }
        });

        ...

        return rootView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        ...
        outState.putInt(CARD_OPEN_TAG, cardOpen);
    }

}

1 个答案:

答案 0 :(得分:0)

我不认为你所做的事情是正确的,原因如下:

1

您使用的循环显示库与Android 11到17(3.0到4.2)上的硬件加速不兼容。它使用Canvas.clipPath() - 一种仅在Android 4.3中用软件实现的方法。这意味着您必须关闭硬件加速,否则您的应用程序将在不支持的OpenGL调用时崩溃。

剪辑布局的最佳方法是使用Canvas.saveLayer()/ restoreLayer()。这是支持所有设备,获得抗锯齿图像并正确支持无效/布局/绘制流程的唯一方法。

2

依靠计时器和布局侦听器来改变布局意味着某些东西不适合你。每次更改都可以直接执行,无需等待。你只需找到适合这段代码的地方。

当您的应用处于后台时,定时器也会触发。这意味着它在尝试从Timer线程访问UI时会崩溃。

也许您在代码中的某处调用setVisibility(true),这就是为什么您的FAB可见?设置可见性是可以的,并且应该没有任何延迟呼叫。只需将可见性保存为FAB的状态,并在方向更改后将其恢复。

如果您希望将FAB放在工具栏的中心,请将它们包装在FrameLayout(wrap_content)中,并使用layout_gravity =&#34; center&#34;定位FAB。这应该允许你删除布局监听器。

3

support.CardView已损坏,根本不应该使用。它在Lollipop和旧系统上的外观和工作方式略有不同。阴影是不同的,填充是不同的,内容剪辑不适用于前Lollipop设备等。这就是为什么很难在所有平台上获得一致,良好的结果。

您应该考虑为此目的使用普通布局。

4

有些动画看起来很酷,但很难实现,没有任何价值并且很快变得烦人,因为每次用户点击按钮时用户都必须等待动画完成。

我不是说你的情况没有,但你可以考虑删除转换并使用下拉菜单,底部工作表,静态工具栏或为此目的的对话框。