Example usage for android.graphics.drawable Drawable setBounds

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

Introduction

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

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

Usage

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);// w  w  w  .  j a  v a  2  s  .  c  o  m
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:Main.java

/**
 * Extract an underlying bitmap from a drawable
 *
 * @param sourceDrawable The source drawable
 * @return The underlying bitmap//  w  w w. j a  v a2s  .  c om
 */
public static Bitmap getBitmapFromDrawable(Drawable sourceDrawable) {
    if (sourceDrawable == null) {
        return null;
    }

    if (sourceDrawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) sourceDrawable).getBitmap();
    } else {
        //copying drawable object to not manipulate on the same reference
        Drawable.ConstantState constantState = sourceDrawable.getConstantState();
        if (constantState == null) {
            return null;
        }
        Drawable drawable = constantState.newDrawable().mutate();

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                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:com.achep.acdisplay.graphics.IconFactory.java

@NonNull
private static Bitmap createIcon(@NonNull Drawable drawable, int size) {
    Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(icon);

    drawable = drawable.mutate();//from w w w  .  java2 s .com
    drawable.setBounds(0, 0, size, size);
    drawable.draw(canvas);

    return icon;
}

From source file:Main.java

public static int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {//from w  ww.  ja va  2 s.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) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);//from   w  w w.j a v a2 s  .  c  om
    return bitmap;
}

From source file:com.google.android.apps.santatracker.presentquest.util.VectorUtil.java

public static BitmapDescriptor vectorToBitmap(Context context, @DrawableRes int id) {
    Drawable vectorDrawable = ResourcesCompat.getDrawable(context.getResources(), id, null);
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from   w  w  w .j  av a  2s . com*/
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}

From source file:Main.java

public static Bitmap bitmapFromDrawable(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas();
    canvas.setBitmap(bitmap);//from   w w w .j a  v a 2  s.co  m
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

private static Drawable recolor(Resources res, Drawable drawable, int color) {
    if (drawable == null) {
        return null;
    }//from   ww w  .  ja va 2  s . c  o  m

    Bitmap outBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    drawable.draw(canvas);
    drawable.setColorFilter(null);
    drawable.setCallback(null); // free up any references
    return new BitmapDrawable(res, outBitmap);
}

From source file:Main.java

public static ImageButton makeImageButton(Context context, int id, int resId, int width, int height,
        OnClickListener onClickListener, OnTouchListener onTouchListener) {
    Drawable icon;
    ImageButton button = new ImageButton(context);
    button.setId(id);/*  w  w w.j  a v  a 2  s  .  c  o m*/
    if (onClickListener != null)
        button.setOnClickListener(onClickListener);
    button.setBackgroundColor(Color.TRANSPARENT);
    icon = context.getResources().getDrawable(resId);
    icon.setBounds(0, 0, width, height);

    Bitmap iconBitmap = ((BitmapDrawable) icon).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(iconBitmap, width, height, false);
    button.setImageBitmap(bitmapResized);
    button.setVisibility(View.VISIBLE);
    if (onTouchListener != null)
        button.setOnTouchListener(onTouchListener);
    return button;
}

From source file:Main.java

public static Bitmap DrawableToBitmap(Drawable drawable) {

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);

    //canvas.setBitmap(bitmap);

    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

    drawable.draw(canvas);//from  w  w  w.j  a  v a2  s.  c om

    return bitmap;

}