Example usage for android.graphics.drawable Drawable getIntrinsicWidth

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

Introduction

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

Prototype

public int getIntrinsicWidth() 

Source Link

Document

Returns the drawable's intrinsic width.

Usage

From source file:Main.java

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

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:org.microg.gms.maps.bitmap.ResourceBitmapDescriptor.java

public static Bitmap drawableToBitmap(Context context, Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*w w  w.j  a  v  a 2  s .  c  o  m*/

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        return DefaultBitmapDescriptor.DEFAULT_DESCRIPTOR.loadBitmap(context);
    }

    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:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();
    else {//from   w  w  w .  ja  va  2 s .  com
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }
}

From source file:Main.java

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    if (null == drawable || w < 0 || h < 0) {
        return null;
    }//from  w  w w .j  a  va2 s.  co m

    try {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        float scaleWidth = 1;
        float scaleHeight = 1;
        if (w > 0) {
            scaleWidth = ((float) w / width);
        }
        if (h > 0) {
            scaleHeight = ((float) h / height);
        }

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
        return new BitmapDrawable(newbmp);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.jameswolfeoliver.pigeon.Utilities.Utils.java

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from  w  w  w.j a  va  2 s.  c  o m*/
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

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  www .ja  va2s  . c  o  m

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

private static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }// www. j  av  a2s  .com

    final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    final 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 int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {/*from  www  .j  av  a2 s.co  m*/

    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 asBitmap(Drawable drawable, int minWidth, int minHeight) {
    final Rect tmpRect = new Rect();
    drawable.copyBounds(tmpRect);//from  w ww . j  a  v  a2s .  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) {
    Bitmap bitmap;/*from   ww w .j  a va2s.  c o  m*/
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    return bitmap;
}