public final class

ColorFilterDimmer

extends java.lang.Object

 java.lang.Object

↳androidx.leanback.graphics.ColorFilterDimmer

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha02'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha02

Artifact androidx.leanback:leanback:1.2.0-alpha02 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.graphics.ColorFilterDimmer android.support.v17.leanback.graphics.ColorFilterDimmer

Overview

Helper class for applying a dim level to a View. The ColorFilterDimmer uses a ColorFilter in a Paint object to dim the view according to the currently active level.

Summary

Methods
public voidapplyFilterToView(View view)

Apply current the ColorFilter to a View.

public static ColorFilterDimmercreate(ColorFilterCache dimmer, float activeLevel, float dimmedLevel)

Creates a ColorFilterDimmer for the given color and levels..

public static ColorFilterDimmercreateDefault(Context context)

Creates a default ColorFilterDimmer.

public ColorFiltergetColorFilter()

Gets the ColorFilter set to the current dim level.

public PaintgetPaint()

Gets the Paint object set to the current dim level.

public voidsetActiveLevel(float level)

Sets the active level of the dimmer.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static ColorFilterDimmer createDefault(Context context)

Creates a default ColorFilterDimmer. Uses the default color and level for the dimmer.

Parameters:

context: A Context used to retrieve Resources.

Returns:

A ColorFilterDimmer with the default dim color and levels.

public static ColorFilterDimmer create(ColorFilterCache dimmer, float activeLevel, float dimmedLevel)

Creates a ColorFilterDimmer for the given color and levels..

Parameters:

dimmer: The ColorFilterCache for dim color.
activeLevel: The level of dimming when the View is in its active state. Must be a float value between 0.0 and 1.0.
dimmedLevel: The level of dimming when the View is in its dimmed state. Must be a float value between 0.0 and 1.0.

public void applyFilterToView(View view)

Apply current the ColorFilter to a View. This method will set the hardware layer of the view when applying a filter, and remove it when not applying a filter.

Parameters:

view: The View to apply the ColorFilter to.

public void setActiveLevel(float level)

Sets the active level of the dimmer. Updates the ColorFilter based on the level.

Parameters:

level: A float between 0 (fully dim) and 1 (fully active).

public ColorFilter getColorFilter()

Gets the ColorFilter set to the current dim level.

Returns:

The current ColorFilter.

public Paint getPaint()

Gets the Paint object set to the current dim level.

Returns:

The current Paint object.

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package androidx.leanback.graphics;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.view.View;

import androidx.leanback.R;

/**
 * Helper class for applying a dim level to a View.  The ColorFilterDimmer
 * uses a ColorFilter in a Paint object to dim the view according to the
 * currently active level.
 */
public final class ColorFilterDimmer {

    private final ColorFilterCache mColorDimmer;

    private final float mActiveLevel;
    private final float mDimmedLevel;

    private final Paint mPaint;
    private ColorFilter mFilter;

    /**
     * Creates a default ColorFilterDimmer. Uses the default color and level for
     * the dimmer.
     *
     * @param context A Context used to retrieve Resources.
     * @return A ColorFilterDimmer with the default dim color and levels.
     */
    public static ColorFilterDimmer createDefault(Context context) {
        TypedArray a = context.obtainStyledAttributes(R.styleable.LeanbackTheme);

        int dimColor = a.getColor(R.styleable.LeanbackTheme_overlayDimMaskColor,
                context.getResources().getColor(R.color.lb_view_dim_mask_color));
        float activeLevel = a.getFraction(R.styleable.LeanbackTheme_overlayDimActiveLevel, 1, 1,
                context.getResources().getFraction(R.fraction.lb_view_active_level, 1, 0));
        float dimmedLevel = a.getFraction(R.styleable.LeanbackTheme_overlayDimDimmedLevel, 1, 1,
                context.getResources().getFraction(R.fraction.lb_view_dimmed_level, 1, 1));
        a.recycle();
        return new ColorFilterDimmer(ColorFilterCache.getColorFilterCache(
                    dimColor), activeLevel, dimmedLevel);
    }

    /**
     * Creates a ColorFilterDimmer for the given color and levels..
     *
     * @param dimmer      The ColorFilterCache for dim color.
     * @param activeLevel The level of dimming when the View is in its active
     *                    state. Must be a float value between 0.0 and 1.0.
     * @param dimmedLevel The level of dimming when the View is in its dimmed
     *                    state. Must be a float value between 0.0 and 1.0.
     */
    public static ColorFilterDimmer create(ColorFilterCache dimmer,
            float activeLevel, float dimmedLevel) {
        return new ColorFilterDimmer(dimmer, activeLevel, dimmedLevel);
    }

    private ColorFilterDimmer(ColorFilterCache dimmer, float activeLevel, float dimmedLevel) {
        mColorDimmer = dimmer;
        if (activeLevel > 1.0f) activeLevel = 1.0f;
        if (activeLevel < 0.0f) activeLevel = 0.0f;
        if (dimmedLevel > 1.0f) dimmedLevel = 1.0f;
        if (dimmedLevel < 0.0f) dimmedLevel = 0.0f;
        mActiveLevel = activeLevel;
        mDimmedLevel = dimmedLevel;
        mPaint = new Paint();
    }

    /**
     * Apply current the ColorFilter to a View. This method will set the
     * hardware layer of the view when applying a filter, and remove it when not
     * applying a filter.
     *
     * @param view The View to apply the ColorFilter to.
     */
    public void applyFilterToView(View view) {
        if (mFilter != null) {
            view.setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
        } else {
            view.setLayerType(View.LAYER_TYPE_NONE, null);
        }
        // FIXME: Current framework has bug that not triggering invalidate when change layer
        // paint.  Will add conditional sdk version check once bug is fixed in released
        // framework.
        view.invalidate();
    }

    /**
     * Sets the active level of the dimmer. Updates the ColorFilter based on the
     * level.
     *
     * @param level A float between 0 (fully dim) and 1 (fully active).
     */
    public void setActiveLevel(float level) {
        if (level < 0.0f) level = 0.0f;
        if (level > 1.0f) level = 1.0f;
        mFilter = mColorDimmer.getFilterForLevel(
                mDimmedLevel + level * (mActiveLevel - mDimmedLevel));
        mPaint.setColorFilter(mFilter);
    }

    /**
     * Gets the ColorFilter set to the current dim level.
     *
     * @return The current ColorFilter.
     */
    public ColorFilter getColorFilter() {
        return mFilter;
    }

    /**
     * Gets the Paint object set to the current dim level.
     *
     * @return The current Paint object.
     */
    public Paint getPaint() {
        return mPaint;
    }
}