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:me.zhang.bingo.Utility.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;//ww  w . j  av  a 2  s .c o m

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        // Single color bitmap will be created of 1x1 pixel.
        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, 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;/*from ww w .  j  a v a  2s. c  o m*/

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        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 fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * @param context context used to get resources
 * @param resourceID drawable resource ID to an InsetDrawable
 * @param fillColor fill color to apply to drawable
 * @param strokeColor stroke color to apply to drawable
 * @param strokePx width of stroke/*from   w  w w.  j a v a2  s  . c  o m*/
 * @return a Bitmap of the drawable
 */
public static Bitmap insetDrawableToBitmap(Context context, int resourceID, int fillColor, int strokeColor,
        int strokePx) {
    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), resourceID, null);
    InsetDrawable inset = (InsetDrawable) drawable;

    int w = 1, h = 1;
    if (inset != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Drawable wrapped = inset.getDrawable();
            if (wrapped != null) {
                w = wrapped.getIntrinsicWidth();
                h = wrapped.getIntrinsicHeight();
            }
        } else {
            w = inset.getIntrinsicWidth();
            h = inset.getIntrinsicHeight();
        }
    }

    Drawable tinted = tintDrawable(inset, fillColor, strokeColor, strokePx);
    return drawableToBitmap(context, tinted, w, h, true);
}

From source file:in.shick.diode.common.Common.java

/**
 * Helper function to display a list of URLs.
 * @param theContext The current application context.
 * @param settings The settings to use regarding the browser component.
 * @param theItem The ThingInfo item to get URLs from.
 *///ww  w  .  j  a va 2  s . c o m
public static void showLinksDialog(final Context theContext, final RedditSettings settings,
        final ThingInfo theItem) {
    assert (theContext != null);
    assert (theItem != null);
    assert (settings != null);
    final ArrayList<String> urls = new ArrayList<String>();
    final ArrayList<MarkdownURL> vtUrls = theItem.getUrls();
    for (MarkdownURL vtUrl : vtUrls) {
        urls.add(vtUrl.url);
    }
    ArrayAdapter<MarkdownURL> adapter = new ArrayAdapter<MarkdownURL>(theContext,
            android.R.layout.select_dialog_item, vtUrls) {
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (convertView == null) {
                tv = (TextView) ((LayoutInflater) theContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                        .inflate(android.R.layout.select_dialog_item, null);
            } else {
                tv = (TextView) convertView;
            }

            String url = getItem(position).url;
            String anchorText = getItem(position).anchorText;
            //                        if (Constants.LOGGING) Log.d(TAG, "links url="+url + " anchorText="+anchorText);

            Drawable d = null;
            try {
                d = theContext.getPackageManager()
                        .getActivityIcon(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            } catch (PackageManager.NameNotFoundException ignore) {
            }
            if (d != null) {
                d.setBounds(0, 0, d.getIntrinsicHeight(), d.getIntrinsicHeight());
                tv.setCompoundDrawablePadding(10);
                tv.setCompoundDrawables(d, null, null, null);
            }

            final String telPrefix = "tel:";
            if (url.startsWith(telPrefix)) {
                url = PhoneNumberUtils.formatNumber(url.substring(telPrefix.length()));
            }

            if (anchorText != null)
                tv.setText(Html.fromHtml("<span>" + anchorText + "</span><br /><small>" + url + "</small>"));
            else
                tv.setText(Html.fromHtml(url));

            return tv;
        }
    };

    AlertDialog.Builder b = new AlertDialog.Builder(
            new ContextThemeWrapper(theContext, settings.getDialogTheme()));

    DialogInterface.OnClickListener click = new DialogInterface.OnClickListener() {
        public final void onClick(DialogInterface dialog, int which) {
            if (which >= 0) {
                Common.launchBrowser(settings, theContext, urls.get(which),
                        Util.createThreadUri(theItem).toString(), false, false, settings.isUseExternalBrowser(),
                        settings.isSaveHistory());
            }
        }
    };

    b.setTitle(R.string.select_link_title);
    b.setCancelable(true);
    b.setAdapter(adapter, click);

    b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public final void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    b.show();
}

From source file:org.catnut.util.CatnutUtils.java

/**
 * ?//w  ww  . ja  v  a  2 s .c o  m
 *
 * @param boundPx the icon' s rectangle bound, if zero, use the default
 */
public static SpannableString text2Emotion(Context context, String key, int boundPx) {
    SpannableString spannable = new SpannableString(key);
    InputStream inputStream = null;
    Drawable drawable = null;
    try {
        inputStream = context.getAssets().open(TweetImageSpan.EMOTIONS_DIR + TweetImageSpan.EMOTIONS.get(key));
        drawable = Drawable.createFromStream(inputStream, null);
    } catch (IOException e) {
        Log.e(TAG, "load emotion error!", e);
    } finally {
        closeIO(inputStream);
    }
    if (drawable != null) {
        if (boundPx == 0) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        } else {
            drawable.setBounds(0, 0, boundPx, boundPx);
        }
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spannable.setSpan(span, 0, key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    return spannable;
}

From source file:com.jaspersoft.android.jaspermobile.util.resource.viewbinder.CustomBitmapProcessor.java

public Bitmap convertToBitmap(Drawable drawable) {
    int widthPixels = drawable.getIntrinsicWidth();
    int heightPixels = drawable.getIntrinsicHeight();

    Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mutableBitmap);
    drawable.setBounds(0, 0, widthPixels, heightPixels);
    drawable.draw(canvas);//from   w w  w  .jav a 2 s.co m

    return mutableBitmap;
}

From source file:com.fuzz.indicator.CutoutImageCell.java

/**
 * Call this whenever the view's bounds might have changed, or the layout params are different.
 * <p>//from  w w w.  ja  v a  2 s . c o  m
 *     This method delegates the choice of Drawable to
 *     {@link #chooseDrawable(CutoutViewLayoutParams)}, which it then vets before setting
 *     on the {@link #itemView}.
 * </p>
 *
 * @param lp    the LayoutParams {@link #itemView} should be assumed to have
 */
public void updateDrawable(@NonNull CutoutViewLayoutParams lp) {
    Drawable chosen = chooseDrawable(lp);

    if (chosen != null && (chosen.getIntrinsicWidth() <= 0 || chosen.getIntrinsicHeight() <= 0)
            && chosen instanceof ColorDrawable) {
        ResizeableColorDrawable rCD = new ResizeableColorDrawable((ColorDrawable) chosen);
        rCD.setIntrinsicWidth(lp.width);
        rCD.setIntrinsicHeight(lp.height);
        chosen = rCD;
    }
    itemView.setImageDrawable(chosen);
}

From source file:com.freshdigitable.udonroad.MediaImageView.java

public MediaImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setContentDescription(getResources().getString(R.string.tweet_media_descs));
    setVisibility(GONE);/*from   w ww. j a  va2  s . c  om*/

    if (playIcon == null) {
        final Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.ld_play_icon);
        playIcon = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(playIcon);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
}

From source file:com.webonise.gardenIt.MyGcmListenerService.java

private Bitmap getBitmapDrawable() {
    Drawable d = getResources().getDrawable(R.drawable.drawable_notification_icon);
    Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.drawable_notification_icon);
    ld.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ld.draw(new Canvas(b));

    return b;// ww w.ja  v  a 2  s .  com
}

From source file:com.creationgroundmedia.nytimessearch.adapters.ArticlesAdapter.java

private Bitmap bitmapFromResource(int resId) {
    Drawable sendIcon = ContextCompat.getDrawable(mContext, resId);
    Bitmap bitmap = Bitmap.createBitmap(sendIcon.getIntrinsicWidth(), sendIcon.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from  w w w. java 2s  .  c  o m*/
    Canvas canvas = new Canvas(bitmap);
    sendIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    sendIcon.draw(canvas);
    return bitmap;
}