Filter out color (greyscale) everything behind a view

194 Views Asked by At

I am showing a semi-transparent overlay over other apps. I would like to remove any color coming through this view from what is behind it. Is there any way to apply a color filter that would remove the color to make whatever comes through greyscale or monochromatic?

Here's what I've tried, with the help of this stackoverflow question:

My view:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorBlackTransparent"
android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:id="@+id/cardView_locked_out"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="?android:colorBackground"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">
    <!-- ?android:colorBackground gives us the default activity background color -->

    <include layout="@layout/layout_locked_out_card"/>

</androidx.cardview.widget.CardView>

</LinearLayout>

And the code to change the color:

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);

    Paint greyscalePaint = new Paint();
    greyscalePaint.setColorFilter(colorMatrixColorFilter);

    // Create a hardware layer with the greyscale paint
    viewOverlay.setLayerType(View.LAYER_TYPE_HARDWARE, greyscalePaint);

This works to make everything that is drawn within the overlay black and white (the cardView in this case), but anything coming through the semi-transparent "layout_root" (what can be seen behind the overlay) still has color. In fact, I only want to make what is seen behind transparent, not what I draw in front.

0

There are 0 best solutions below