Example usage for android.graphics.drawable LayerDrawable LayerDrawable

List of usage examples for android.graphics.drawable LayerDrawable LayerDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable LayerDrawable LayerDrawable.

Prototype

public LayerDrawable(@NonNull Drawable[] layers) 

Source Link

Document

Creates a new layer drawable with the list of specified layers.

Usage

From source file:com.google.samples.apps.topeka.adapter.CategoryAdapter.java

@NonNull
private LayerDrawable loadSolvedIconLollipop(Category category, int categoryImageResource) {
    final Drawable done = loadTintedDoneDrawable();
    final Drawable categoryIcon = loadTintedCategoryDrawable(category, categoryImageResource);
    Drawable[] layers = new Drawable[] { categoryIcon, done }; // ordering is back to front
    return new LayerDrawable(layers);
}

From source file:com.lumisky.bluetooth_led.Interface.Bulbs.BulbsActivity.java

private void changeColor(int newColor) {

    tabs.setIndicatorColor(newColor);//from   ww  w .jav  a  2  s .  c om

    // change ActionBar color just if an ActionBar is available
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        Drawable colorDrawable = new ColorDrawable(newColor);
        Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
        LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });

        if (oldBackground == null) {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                ld.setCallback(drawableCallback);
            } else {
                getActionBar().setBackgroundDrawable(ld);
            }

        } else {

            TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });

            // workaround for broken ActionBarContainer drawable handling on
            // pre-API 17 builds
            // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                td.setCallback(drawableCallback);
            } else {
                getActionBar().setBackgroundDrawable(td);
            }

            td.startTransition(200);

        }

        oldBackground = ld;

        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(true);

    }

    currentColor = newColor;

}

From source file:net.kjmaster.cookiemom.MainActivity.java

private void changeColor(int newColor) {
    tabs.setIndicatorColor(newColor);//from   w w w .j av  a2s.  c om
    // change ActionBar color just if an ActionBar is available
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        Drawable colorDrawable = new ColorDrawable(newColor);
        Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
        LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });

        if (oldBackground == null) {

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                ld.setCallback(drawableCallback);
            } else {
                getActionBar().setBackgroundDrawable(ld);
            }

        } else {

            TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });

            // workaround for broken ActionBarContainer drawable handling on
            // pre-API 17 builds
            // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                td.setCallback(drawableCallback);
            } else {
                getActionBar().setBackgroundDrawable(td);
            }

            td.startTransition(200);
        }

        oldBackground = ld;

        // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(true);

    }

    currentColor = newColor;
}

From source file:xyz.berial.textinputlayout.TintManager.java

public Drawable getDrawable(int resId, boolean failIfNotKnown) {
    final Context context = mContextRef.get();
    if (context == null)
        return null;

    Drawable drawable = ContextCompat.getDrawable(context, resId);

    if (drawable != null) {
        if (Build.VERSION.SDK_INT >= 8) {
            // Mutate can cause NPEs on 2.1
            drawable = drawable.mutate();
        }// w  ww . ja  v  a  2s.c  o m

        final ColorStateList tintList = getTintList(resId);
        if (tintList != null) {
            // First wrap the Drawable and set the tint list
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTintList(drawable, tintList);

            // If there is a blending mode specified for the drawable, use it
            final PorterDuff.Mode tintMode = getTintMode(resId);
            if (tintMode != null) {
                DrawableCompat.setTintMode(drawable, tintMode);
            }
        } else if (resId == R.drawable.abc_cab_background_top_material) {
            return new LayerDrawable(new Drawable[] { getDrawable(R.drawable.abc_cab_background_internal_bg),
                    getDrawable(R.drawable.abc_cab_background_top_mtrl_alpha) });
        } else {
            final boolean usedColorFilter = tintDrawableUsingColorFilter(resId, drawable);
            if (!usedColorFilter && failIfNotKnown) {
                // If we didn't tint using a ColorFilter, and we're set to fail if we don't
                // know the id, return null
                drawable = null;
            }
        }
    }
    return drawable;
}

From source file:com.max.library.view.v7.AppCompatDrawableManager.java

public Drawable getDrawable(@NonNull Context context, @DrawableRes int resId, boolean failIfNotKnown) {
    // Let the InflateDelegates have a go first
    if (mDelegates != null) {
        for (int i = 0, count = mDelegates.size(); i < count; i++) {
            final InflateDelegate delegate = mDelegates.get(i);
            final Drawable result = delegate.onInflateDrawable(context, resId);
            if (result != null) {
                return result;
            }/*from   w w w .j a v a2 s . c om*/
        }
    }

    // The delegates failed so we'll carry on
    Drawable drawable = ContextCompat.getDrawable(context, resId);

    if (drawable != null) {
        if (Build.VERSION.SDK_INT >= 8) {
            // Mutate can cause NPEs on 2.1
            drawable = drawable.mutate();
        }

        final ColorStateList tintList = getTintList(context, resId);
        if (tintList != null) {
            // First wrap the Drawable and set the tint list
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTintList(drawable, tintList);

            // If there is a blending mode specified for the drawable, use it
            final PorterDuff.Mode tintMode = getTintMode(resId);
            if (tintMode != null) {
                DrawableCompat.setTintMode(drawable, tintMode);
            }
        } else if (resId == R.drawable.abc_cab_background_top_material) {
            return new LayerDrawable(
                    new Drawable[] { getDrawable(context, R.drawable.abc_cab_background_internal_bg),
                            getDrawable(context, R.drawable.abc_cab_background_top_mtrl_alpha) });
        } else if (resId == com.max.library.R.drawable.abc_seekbar_track_material) {
            LayerDrawable ld = (LayerDrawable) drawable;
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                    getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                    getThemeAttrColor(context, R.attr.colorControlNormal), DEFAULT_MODE);
            setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                    getThemeAttrColor(context, R.attr.colorControlActivated), DEFAULT_MODE);
        } else {
            final boolean tinted = tintDrawableUsingColorFilter(context, resId, drawable);
            if (!tinted && failIfNotKnown) {
                // If we didn't tint using a ColorFilter, and we're set to fail if we don't
                // know the id, return null
                drawable = null;
            }
        }
    }
    return drawable;
}

From source file:com.google.maps.android.clustering.view.DefaultClusterRenderer.java

private LayerDrawable makeClusterBackground() {
    mColoredCircleBackground = new ShapeDrawable(new OvalShape());
    ShapeDrawable outline = new ShapeDrawable(new OvalShape());
    outline.getPaint().setColor(0x80ffffff); // Transparent white.
    LayerDrawable background = new LayerDrawable(new Drawable[] { outline, mColoredCircleBackground });
    int strokeWidth = (int) (mDensity * 3);
    background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
    return background;
}

From source file:org.smssecure.smssecure.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {/* w ww .  j  a  v  a  2 s. c  o m*/
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res.getDrawable(R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}

From source file:android.support.v7.widget.AppCompatDrawableManager.java

private Drawable createDrawableIfNeeded(@NonNull Context context, @DrawableRes final int resId) {
    if (mTypedValue == null) {
        mTypedValue = new TypedValue();
    }//w w w . j a  v  a 2 s.  com
    final TypedValue tv = mTypedValue;
    context.getResources().getValue(resId, tv, true);
    final long key = createCacheKey(tv);

    Drawable dr = getCachedDrawable(context, key);
    if (dr != null) {
        // If we got a cached drawable, return it
        return dr;
    }

    // Else we need to try and create one...
    if (resId == R.drawable.abc_cab_background_top_material) {
        dr = new LayerDrawable(new Drawable[] { getDrawable(context, R.drawable.abc_cab_background_internal_bg),
                getDrawable(context, R.drawable.abc_cab_background_top_mtrl_alpha) });
    }

    if (dr != null) {
        dr.setChangingConfigurations(tv.changingConfigurations);
        // If we reached here then we created a new drawable, add it to the cache
        addDrawableToCache(context, key, dr);
    }

    return dr;
}

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private Drawable tintDrawable(@NonNull Context context, @DrawableRes int resId, boolean failIfNotKnown,
        @NonNull Drawable drawable) {// www .ja v  a  2 s .  c  o m
    final ColorStateList tintList = getTintList(context, resId);
    if (tintList != null) {
        // First mutate the Drawable, then wrap it and set the tint list
        if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
            drawable = drawable.mutate();
        }
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, tintList);

        // If there is a blending mode specified for the drawable, use it
        final PorterDuff.Mode tintMode = getTintMode(resId);
        if (tintMode != null) {
            DrawableCompat.setTintMode(drawable, tintMode);
        }
    } else if (resId == R.drawable.abc_cab_background_top_material_ox) {
        return new LayerDrawable(
                new Drawable[] { getDrawable(context, R.drawable.abc_cab_background_internal_bg_ox),
                        getDrawable(context, R.drawable.abc_cab_background_internal_bg_ox) });
    } else if (resId == R.drawable.abc_seekbar_track_material_ox) {
        LayerDrawable ld = (LayerDrawable) drawable;
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal_ox), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal_ox), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox), DEFAULT_MODE);
    } else if (resId == R.drawable.abc_ratingbar_indicator_material_ox
            || resId == R.drawable.abc_ratingbar_small_material_ox) {
        LayerDrawable ld = (LayerDrawable) drawable;
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.background),
                ThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorControlNormal_ox), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.secondaryProgress),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox), DEFAULT_MODE);
        setPorterDuffColorFilter(ld.findDrawableByLayerId(android.R.id.progress),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated_ox), DEFAULT_MODE);
    } else {
        final boolean tinted = tintDrawableUsingColorFilter(context, resId, drawable);
        if (!tinted && failIfNotKnown) {
            // If we didn't tint using a ColorFilter, and we're set to fail if we don't
            // know the id, return null
            drawable = null;
        }
    }
    return drawable;
}

From source file:com.tingtingapps.securesms.preferences.ColorPreference.java

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();

        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {/*from  w w w  . j a va  2s.c o m*/
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }

        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);

        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);

        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res
                    .getDrawable(com.tingtingapps.securesms.R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }

        imageView.setImageDrawable(drawable);

    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}