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:com.xxxifan.devbox.core.util.ViewUtils.java

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

    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.google.samples.apps.ourstreets.view.ViewUtils.java

/**
 * Creates a {@link BitmapDescriptor} from  a drawable.
 * This is particularly useful for {@link GoogleMap} {@link Marker}s.
 *
 * @param drawable The drawable that should be a {@link BitmapDescriptor}.
 * @return The created {@link BitmapDescriptor}.
 *//*from   w w  w  .  j  av  a  2  s .c  o  m*/
@NonNull
public static BitmapDescriptor getBitmapDescriptorFromDrawable(@NonNull Drawable drawable) {
    BitmapDescriptor bitmapDescriptor;
    // Usually the pin could be loaded via BitmapDescriptorFactory directly.
    // The target map_pin is a VectorDrawable which is currently not supported
    // within BitmapDescriptors.
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    drawable.setBounds(0, 0, width, height);
    Bitmap markerBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(markerBitmap);
    drawable.draw(canvas);
    bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(markerBitmap);
    return bitmapDescriptor;
}

From source file:com.destin.sehaikun.DrawableUtils.java

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/* w  w w  . ja v  a 2 s.c om*/

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    try {
        Bitmap bitmap;

        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        } 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);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.Utils.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;/* ww w .ja  va  2 s  .  com*/
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }
    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.tr4android.support.extension.widget.CircleImageView.java

/**
 * Helper for creating a bitmap from a drawable
 *
 * @param drawable The drawable which should be converted to a bitmap
 * @return the bitmap containing the drawable
 *///from   www.  j  ava 2 s . c o m
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null)
        return null;
    if (drawable instanceof BitmapDrawable) {
        Log.w(LOG_TAG, "For better performance consider using setImageBitmap() instead!");
        return ((BitmapDrawable) drawable).getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(Math.max(2, drawable.getIntrinsicWidth()),
                Math.max(2, 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:de.gebatzens.sia.SettingsActivity.java

public static CustomTabsIntent createCustomTab(Activity activity, String url) {
    Drawable d = ContextCompat.getDrawable(activity, R.drawable.ic_arrow_back);
    Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    d.draw(canvas);//  w  w w .  j a v a2 s  .  c o m
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .setToolbarColor(SIAApp.SIA_APP.school.getColor()).setSecondaryToolbarColor(Color.RED)
            .setCloseButtonIcon(bitmap).setShowTitle(true).build();
    customTabsIntent.launchUrl(activity, Uri.parse(url));
    return customTabsIntent;
}

From source file:com.battlelancer.seriesguide.util.Utils.java

/**
 * Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the
 * text.  Use null if you do not want a Drawable there. The Drawables' bounds will be set to
 * their intrinsic bounds./*from  w  ww . j  a v  a 2s. c  om*/
 */
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(Button button, Drawable left, Drawable top,
        Drawable right, Drawable bottom) {
    if (left != null) {
        left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
    }
    if (right != null) {
        right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
    }
    if (top != null) {
        top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
    }
    if (bottom != null) {
        bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
    }
    button.setCompoundDrawables(left, top, right, bottom);
}

From source file:org.bottiger.podcast.utils.UIUtils.java

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }/*from  w  w w  .  j  a v  a 2s . c o  m*/

    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:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*from   w  w  w .  ja  v a  2s.c  om*/
    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

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*w  w  w.ja v a2  s .  c o m*/

    switch (gravity) {
    case Gravity.END:
        ratio = -1;
    case Gravity.START:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        total = fontMetrics.descent - fontMetrics.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}