Example usage for android.graphics.drawable Drawable getBounds

List of usage examples for android.graphics.drawable Drawable getBounds

Introduction

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

Prototype

@NonNull
public final Rect getBounds() 

Source Link

Document

Return the drawable's bounds Rect.

Usage

From source file:Main.java

private static Drawable createPlaceholder(Drawable original) {
    Drawable placeholder = new ColorDrawable();
    placeholder.setBounds(original.getBounds());
    return placeholder;
}

From source file:Main.java

/**
 * Copies various properties from one drawable to the other.
 * @param to drawable to copy properties to
 * @param from drawable to copy properties from
 *//*  www. jav  a 2s  . c  o  m*/
public static void copyProperties(Drawable to, Drawable from) {
    if (from == null || to == null || to == from) {
        return;
    }

    to.setBounds(from.getBounds());
    to.setChangingConfigurations(from.getChangingConfigurations());
    to.setLevel(from.getLevel());
    to.setVisible(from.isVisible(), /* restart */false);
    to.setState(from.getState());
}

From source file:Main.java

public static Bitmap toBitmap(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return null;
    }//  w w  w  .j  a  v a 2s.com
    Rect bounds = drawable.getBounds();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    drawable.setBounds(bounds);
    return bitmap;
}

From source file:Main.java

public static Drawable toGreyDrawable(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from  w w w.j a  v  a  2 s .c om*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

public static Drawable toGrey(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from  ww w.j a  va  2  s  . c  o m*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:com.stepstone.stepper.internal.util.TintUtil.java

/**
 * Tints a drawable with the provided color state list
 * @param drawable drawable to tint/*  w  ww.j  a  va 2 s  .c o  m*/
 * @param color tint color state list
 * @return tinted drawable
 */
public static Drawable tintDrawable(@Nullable Drawable drawable, ColorStateList color) {
    if (drawable != null) {
        drawable = DrawableCompat.unwrap(drawable);
        Rect bounds = drawable.getBounds();
        drawable = DrawableCompat.wrap(drawable);
        // bounds can be all set to zeros when inflating vector drawables in Android Support Library 23.3.0...
        if (bounds.right == 0 || bounds.bottom == 0) {
            if (drawable.getIntrinsicHeight() != -1 && drawable.getIntrinsicWidth() != -1) {
                bounds.right = drawable.getIntrinsicWidth();
                bounds.bottom = drawable.getIntrinsicHeight();
            } else {
                Log.w(TAG, "Cannot tint drawable because its bounds cannot be determined!");
                return DrawableCompat.unwrap(drawable);
            }
        }
        DrawableCompat.setTintList(drawable, color);
        drawable.setBounds(bounds);
    }
    return drawable;
}

From source file:Main.java

public static int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {/*ww w. ja  v  a 2s . com*/

    float scale = (float) height / (float) image.getIntrinsicHeight();
    int width = (int) Math.round(image.getIntrinsicWidth() * scale);

    Rect oldBounds = image.getBounds();
    image.setBounds(x, y, x + width, y + height);
    image.setColorFilter(color, porterDuff);
    image.draw(canvas);
    image.setBounds(oldBounds);
    image.clearColorFilter();
    return width;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from   ww  w  .  j  a va  2  s .co m

    // We ask for the bounds if they have been set as they would be most
    // correct, then we check we are  > 0
    final int width = !drawable.getBounds().isEmpty() ? drawable.getBounds().width()
            : drawable.getIntrinsicWidth();

    final int height = !drawable.getBounds().isEmpty() ? drawable.getBounds().height()
            : drawable.getIntrinsicHeight();

    // Now we check we are > 0
    final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:Main.java

public static boolean drawDrawables(Canvas canvas, @Nonnull TextView textView) {
    final int compoundPaddingLeft = textView.getCompoundPaddingLeft();
    final int compoundPaddingTop = textView.getCompoundPaddingTop();
    final int compoundPaddingRight = textView.getCompoundPaddingRight();
    final int compoundPaddingBottom = textView.getCompoundPaddingBottom();

    final int scrollX = textView.getScrollX();
    final int scrollY = textView.getScrollY();

    final int right = textView.getRight();
    final int left = textView.getLeft();
    final int bottom = textView.getBottom();
    final int top = textView.getTop();

    final Drawable[] drawables = textView.getCompoundDrawables();
    if (drawables != null) {

        int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
        int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;

        Drawable topDr = drawables[1];
        // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
        // Make sure to update invalidateDrawable() when changing this code.
        if (topDr != null) {
            canvas.save();//from  w w  w .  j a  v a2s .  co  m
            canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2,
                    scrollY + textView.getPaddingTop() + vspace / 2);
            topDr.draw(canvas);
            canvas.restore();
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static int PutImageTargetHeight(Canvas canvas, Drawable image, int x, int y, int height) {
    // float scale = (float)height / (float)image.getBounds().height();
    // int width = (int)Math.round(image.getBounds().width() * scale);

    float scale = (float) height / (float) image.getIntrinsicHeight();
    int width = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Rect oldBounds = image.getBounds();
    image.setBounds(x, y, x + width, y + height);
    image.draw(canvas);//from  w w w  .java 2 s  . c o  m
    image.setBounds(oldBounds);

    return width;
}