在背景中将片段变灰 [英] Gray out fragment in background

查看:108
本文介绍了在背景中将片段变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one main Fragment which is in the background when the second Fragment gets called with getFragmentManager().beginTransaction().add. Right now the user can see the main Fragment behind the second Fragment like it should be. But i want it to be like in a grayed look. When the Second Fragment gets called, the main Fragment should gray out. Im not sure what to google (have tried a lot of keywords) to describe this.

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

My idea was to take a screenshot of the main fragment (bitmap) and make it gray. Is this the right direction?

推荐答案

只需在Fragments之间放置一个View,使其覆盖您要变灰的Fragment.然后将背景设置为全黑,将alpha设置为0,将可见性设置为GONE.

Just put a View in between the Fragments so that it overlays the Fragment you want to gray out. Then set the background to be completely black and the alpha to 0 and visibility to GONE.

当您最终要使另一个Fragment变灰时,将可见性设置为VISIBLE,并将alpha设置为您喜欢的某个值,例如0.5或类似的值.我通常倾向于对alpha值进行动画处理以获得不错的效果.

When you finally want to gray out the other Fragment set the visibility to VISIBLE and set the alpha to some value you like, maybe 0.5 or something like that. I mostly tend to animate the alpha value to get a nice effect.

因此您的布局应如下所示:

So your layout should look something like this:

<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" />

上方FrameLayout中的Fragment将显示为灰色,您可以这样做:

The Fragment in the upper FrameLayout will be the one that is grayed out and you would do that like this:

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

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

When you want to remove that effect again you would do it like this:

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) {

    }
});

这篇关于在背景中将片段变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆