1

当第二个 Fragment 被调用时,我有一个主 Fragment 在后台getFragmentManager().beginTransaction().add。现在用户可以看到第二个 Fragment 后面的主要 Fragment 应该是这样的。但我希望它看起来像灰色。当调用第二个片段时,主片段应该变灰。我不确定用谷歌搜索什么(尝试了很多关键字)来描述这一点。

我的想法是截取主要片段(位图)并将其设为灰色。这是正确的方向吗?

4

1 回答 1

4

只需View在两者之间添加一个,Fragments以便它覆盖Fragment您想要变灰的内容。然后将背景设置为完全黑色,并将 alpha 设置为 0,并将可见性设置为GONE

当您最终想要将另一个Fragment设置为灰色时,将可见性VISIBLE设置为并将 alpha 设置为您喜欢的某个值,可能是 0.5 或类似的值。我主要倾向于对 alpha 值进行动画处理以获得良好的效果。


所以你的布局应该是这样的:

<FrameLayout
    android:id="@+id/fragmentContainerOne"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /> 

<View
    android:id="@+id/fadeBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layerType="hardware"
    android:alpha="0"
    android:visibility="gone"
    android:background="@android:color/black"/>

<FrameLayout
    android:id="@+id/fragmentContainerTwo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Fragment鞋面中的FrameLayout那个将是灰色的,你可以这样做:

final View fadeBackground = findViewById(R.id.fadeBackground);
fadeBackground.setVisibility(VISIBLE);
fadeBackground.animate().alpha(0.5f); // The higher the alpha value the more it will be grayed out

当您想再次删除该效果时,您可以这样做:

fadeBackground.animate().alpha(0.0f).setListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        // As soon as the animation is finished we set the visiblity again back to GONE
        fadeBackground.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
});
于 2014-09-30T14:06:27.397 回答