灰色片段在背景中

时间:2014-09-30 14:02:02

标签: android android-bitmap android-background

我有一个主要片段,当第二个片段被getFragmentManager().beginTransaction().add调用时,它在后台。现在,用户可以看到第二个片段后面的主片段。但我希望它像一个灰色的外观。当第二个片段被调用时,主片段应该变灰。 我不确定谷歌(试过很多关键词)来描述这个。

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

1 个答案:

答案 0 :(得分:3)

只需在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) {

    }
});

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

{{1}}
相关问题