Example usage for android.view View getBackground

List of usage examples for android.view View getBackground

Introduction

In this page you can find the example usage for android.view View getBackground.

Prototype

public Drawable getBackground() 

Source Link

Document

Gets the background drawable

Usage

From source file:com.android.clear.reminder.AnimatorUtils.java

/**
 * Sets the alpha of the top layer's drawable (of the background) only, if the background is a
 * layer drawable, to ensure that the other layers (i.e., the selectable item background, and
 * therefore the touch feedback RippleDrawable) are not affected.
 *
 * @param view the affected view//from   w  ww  .  j a va  2 s. c  om
 * @param value the alpha value (0-255)
 */
public static void setBackgroundAlpha(View view, Integer value) {
    Drawable background = view.getBackground();
    if (background instanceof LayerDrawable && ((LayerDrawable) background).getNumberOfLayers() > 0) {
        background = ((LayerDrawable) background).getDrawable(0);
    }
    background.setAlpha(value);
}

From source file:com.desmond.ripple.view.RippleCompat.java

/**
 * Set palette mode of the ripple.//w  ww .  j a  va  2 s  .  co  m
 *
 * @param v view
 * @param paletteMode palette mode. {@link PaletteMode}
 */
public static void setPaletteMode(View v, PaletteMode paletteMode) {
    Drawable drawable = v.getBackground();
    if (drawable instanceof RippleCompatDrawable) {
        ((RippleCompatDrawable) drawable).setPaletteMode(paletteMode);
    } else if (drawable instanceof LayerDrawable) {
        int layer = ((LayerDrawable) drawable).getNumberOfLayers();
        if (((LayerDrawable) drawable).getDrawable(layer - 1) instanceof RippleCompatDrawable) {
            ((RippleCompatDrawable) ((LayerDrawable) drawable).getDrawable(layer - 1))
                    .setPaletteMode(paletteMode);
        }
    }
}

From source file:vc908.stickerfactory.ui.advancedrecyclerview.decoration.ItemShadowDecorator.java

private static boolean shouldDrawDropShadow(View child) {
    if (child.getVisibility() != View.VISIBLE) {
        return false;
    }/*  w  w  w  .ja  v a 2 s.  c om*/
    if (ViewCompat.getAlpha(child) != 1.0f) {
        return false;
    }

    Drawable background = child.getBackground();
    if (background == null) {
        return false;
    }

    if (background instanceof ColorDrawable) {
        //noinspection RedundantCast
        if (((ColorDrawable) background).getAlpha() == 0) {
            return false;
        }
    }

    return true;
}

From source file:com.desmond.ripple.view.RippleCompat.java

/**
 * Set scaleType of the ripple drawable background.
 *
 * @param v         view/*from   w ww  . java2 s.c  o  m*/
 * @param scaleType ScaleType of an image, {@link android.widget.ImageView.ScaleType}
 */
public static void setScaleType(View v, ImageView.ScaleType scaleType) {
    Drawable drawable = v.getBackground();
    if (drawable instanceof RippleCompatDrawable) {
        ((RippleCompatDrawable) drawable).setScaleType(scaleType);
    } else if (drawable instanceof LayerDrawable
            && ((LayerDrawable) drawable).getDrawable(1) instanceof RippleCompatDrawable) {
        ((RippleCompatDrawable) ((LayerDrawable) drawable).getDrawable(1)).setScaleType(scaleType);
    }
    v.invalidate();
}

From source file:Main.java

public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);// ww w .j a v a2  s  . c  o m
    else
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

From source file:ch.ethz.twimight.activities.TwimightBaseActivity.java

/**
 * Clean up the views//from   w  w w  .  ja va2s  . c o  m
 * 
 * @param view
 */
public static void unbindDrawables(View view) {
    if (view != null) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            try {
                ((ViewGroup) view).removeAllViews();
            } catch (UnsupportedOperationException e) {
                // No problem, nothing to do here
            }
        }
    }

}

From source file:android.support.design.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches Views with the specified background fill color.
 */// www  .java2  s . c  o  m
public static Matcher withBackgroundFill(final @ColorInt int fillColor) {
    return new BoundedMatcher<View, View>(View.class) {
        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final View view) {
            Drawable background = view.getBackground();
            try {
                TestUtils.assertAllPixelsOfColor("", background, view.getWidth(), view.getHeight(), true,
                        fillColor, 0, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}

From source file:com.finchuk.clock2.timepickers.Utils.java

/**
 * Sets the color on the {@code view}'s {@code selectableItemBackground} or the
 * borderless variant, whichever was set as the background.
 * @param view the view that should have its highlight color changed
 *///ww  w .j  a v a 2 s  .c om
public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
    Drawable selectableItemBackground = view.getBackground();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && selectableItemBackground instanceof RippleDrawable) {
        ((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
    } else {
        // Draws the color (src) onto the background (dest) *in the same plane*.
        // That means the color is not overlapping (i.e. on a higher z-plane, covering)
        // the background. That would be done with SRC_OVER.
        // The DrawableCompat tinting APIs *could* be a viable alternative, if you
        // call setTintMode(). Previous attempts using those APIs failed without
        // the tint mode. However, those APIs have the overhead of mutating and wrapping
        // the drawable.
        selectableItemBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}

From source file:com.philliphsu.bottomsheetpickers.Utils.java

/**
 * Sets the color on the {@code view}'s {@code selectableItemBackground} or the
 * borderless variant, whichever was set as the background.
 * @param view the view that should have its highlight color changed
 */// w w  w .  j  a  v  a2s.c  o  m
public static void setColorControlHighlight(@NonNull View view, @ColorInt int color) {
    Drawable selectableItemBackground = view.getBackground();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && selectableItemBackground instanceof RippleDrawable) {
        ((RippleDrawable) selectableItemBackground).setColor(ColorStateList.valueOf(color));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Utils.isTv(view.getContext())) {
            ((RippleDrawable) selectableItemBackground).setRadius(72);
        }
    } else {
        // Draws the color (src) onto the background (dest) *in the same plane*.
        // That means the color is not overlapping (i.e. on a higher z-plane, covering)
        // the background. That would be done with SRC_OVER.
        // The DrawableCompat tinting APIs *could* be a viable alternative, if you
        // call setTintMode(). Previous attempts using those APIs failed without
        // the tint mode. However, those APIs have the overhead of mutating and wrapping
        // the drawable.
        selectableItemBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static void clearDrawableAnimation(View view) {
    if (Build.VERSION.SDK_INT < 21 || view == null) {
        return;/*from   w  w  w  .j  a va  2s  .c  o m*/
    }
    Drawable drawable;
    if (view instanceof ListView) {
        drawable = ((ListView) view).getSelector();
        if (drawable != null) {
            drawable.setState(StateSet.NOTHING);
        }
    } else {
        drawable = view.getBackground();
        if (drawable != null) {
            drawable.setState(StateSet.NOTHING);
            drawable.jumpToCurrentState();
        }
    }
}