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 int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {// w ww.  ja v a 2  s . c o 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 drawable2Bitmap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }//from w  ww . j  a va 2  s . c  om
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();

    //        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
    //                : Bitmap.Config.RGB_565;
    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable != null) {
        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  va2  s  .c o m
        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Extract an underlying bitmap from a drawable
 *
 * @param sourceDrawable The source drawable
 * @return The underlying bitmap//from w ww. j a v  a  2 s . c o  m
 */
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:Main.java

public static Bitmap drawableToBitmap(Drawable drawable, float ratio) {
    int width = (int) Math.ceil(drawable.getIntrinsicWidth() * ratio);
    int height = (int) Math.ceil(drawable.getIntrinsicHeight() * ratio);
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    canvas.scale(ratio, ratio);/*ww w .j a v  a 2  s  .  com*/
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/*from www.java2s  .co m*/

    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static android.graphics.Bitmap drawableToBitmap(Drawable drawable) {
    android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap(

            drawable.getIntrinsicWidth(),

            drawable.getIntrinsicHeight(),

            drawable.getOpacity() != PixelFormat.OPAQUE ? android.graphics.Bitmap.Config.ARGB_8888

                    : android.graphics.Bitmap.Config.RGB_565);

    Canvas canvas = new Canvas(bitmap);

    //canvas.setBitmap(bitmap);

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

    drawable.draw(canvas);/*from   www.  j  ava  2 s. co m*/

    return bitmap;
}

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);//from   w  w  w  .  ja  v  a  2 s .c om
    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: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 www .j a  v a  2 s. c  o m*/
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}

From source file:com.nextgis.woody.util.UiUtil.java

public static Drawable tintIcon(Drawable drawable, int color) {
    Drawable result = DrawableCompat.wrap(drawable);
    result.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    DrawableCompat.setTint(result, color);
    return result;
}