Example usage for android.graphics.drawable Drawable getIntrinsicHeight

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

Introduction

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

Prototype

public int getIntrinsicHeight() 

Source Link

Document

Returns the drawable's intrinsic height.

Usage

From source file:Main.java

public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*from  w  w w.j  a va  2s  .  c  o m*/

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, 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

/**
 * Return the {@link Bitmap} representing the {@link Drawable}.
 * @param drawable Object to convert to {@link Bitmap}.
 * @return {@link Bitmap} representing the {@link Drawable}.
 *//*  ww w. j  a  va2  s  . c om*/
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap retVal;
    if (drawable instanceof BitmapDrawable) {
        //Easy
        retVal = ((BitmapDrawable) drawable).getBitmap();
    } else {
        retVal = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);

        synchronized (canvas) {
            canvas.setBitmap(retVal);
            drawable.draw(canvas);
        }
    }
    return retVal;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof NinePatchDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        drawable.draw(canvas);//from  ww w . j av  a  2 s  . c  om
        return bitmap;
    } else {
        throw new IllegalArgumentException("can not support this drawable to bitmap now!!!");
    }
}

From source file:Main.java

public static Bitmap drawable2Bitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof NinePatchDrawable) {
        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);//  w w  w  .  j a  v a 2  s  .c o m
        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        return bitmapDrawable.getBitmap();
    } else {/*w  w w  .  ja  v  a2 s .co m*/
        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);
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) {
    final Rect tmpRect = new Rect();
    drawable.copyBounds(tmpRect);//from  ww w.  ja v  a  2  s . c  o  m
    if (tmpRect.isEmpty()) {
        tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()),
                Math.max(minHeight, drawable.getIntrinsicHeight()));
        drawable.setBounds(tmpRect);
    }
    Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888);
    drawable.draw(new Canvas(bitmap));
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) {
    Bitmap bitmap;//from w w w.  j av a  2  s . com
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        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);
    }
    if (scaleBitmap) {
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
    }
    return bitmap;
}

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();//  ww w .j a  v a  2  s .  c om
    image.setBounds(x, y, x + width, y + height);
    image.draw(canvas);
    image.setBounds(oldBounds);

    return width;
}

From source file:com.danimahardhika.android.helpers.core.DrawableHelper.java

@Nullable
public static Drawable getResizedDrawable(@NonNull Context context, @NonNull Drawable drawable,
        float sizeInDp) {
    try {//w ww .j a v a 2s . c  o  m
        int size = Math.round(UnitHelper.toPixel(context, sizeInDp));

        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 new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) {

    if (scale == 0.0)
        return 0;

    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);
    float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*from  w w  w  .j  a va2  s.  com*/

    return bmd.getIntrinsicWidth();

}