Viewの間に を入れて、グレーアウトしたい をFragmentsオーバーレイします。Fragment次に、背景を完全に黒く、アルファを 0 に、可視性を に設定しGONEます。
最終的にもう一方をグレーアウトしたい場合はFragment、可視性をVISIBLE設定し、アルファを好きな値、おそらく 0.5 などに設定します。私はたいていアルファ値をアニメートして、良い効果を得る傾向があります。
したがって、レイアウトは次のようになります。
<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) {
}
});