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:com.apptentive.android.sdk.util.image.PreviewImageView.java

@Override
public void onGlobalLayout() {
    if (once) {// w  ww.j av a 2 s.  com
        Drawable d = getDrawable();
        if (d == null)
            return;

        int viewWidth = getWidth();
        int viewHeight = getHeight();
        // Get image width and height
        int dw = d.getIntrinsicWidth();
        int dh = d.getIntrinsicHeight();
        float scale = 1.0f;
        // When image width/height is greater than the imageView
        if (dw > viewWidth && dh <= viewHeight) {
            scale = viewWidth * 1.0f / dw;
        }
        if (dh > viewHeight && dw <= viewWidth) {
            scale = viewHeight * 1.0f / dh;
        }
        // If both width and height greater than the imageView, find the smaller scale
        if (dw > viewWidth && dh > viewHeight) {
            scale = Math.min(viewWidth * 1.0f / dw, viewHeight * 1.0f / dh);
        }
        initScale = scale;
        // Center the image
        scaleMatrix.postTranslate((viewWidth - dw) / 2, (viewHeight - dh) / 2);
        scaleMatrix.postScale(scale, scale, getWidth() / 2, getHeight() / 2);
        setImageMatrix(scaleMatrix);
        once = false;
    } else {
        checkBorderAndCenterWhenScale();
        setImageMatrix(scaleMatrix);
    }
}

From source file:com.neudesic.mobile.pulse.ui.drawable.DrawableManager.java

public synchronized Drawable fetchDrawable(String urlString) {
    if (drawableMap.containsKey(urlString)) {
        Log.d(getClass().getSimpleName(), "loading image from localcache");
        return drawableMap.get(urlString);
    }// w ww. ja  v a  2s  .co  m

    Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
    try {
        InputStream is = fetch(urlString);

        Drawable drawable = Drawable.createFromStream(is, "src");
        if (drawableMap.size() > MAX_CACHE_SIZE) {
            drawableMap.clear();
        }
        drawableMap.put(urlString, drawable);
        Log.d(this.getClass().getSimpleName(),
                "got a thumbnail drawable: " + drawable.getBounds() + ", " + drawable.getIntrinsicHeight() + ","
                        + drawable.getIntrinsicWidth() + ", " + drawable.getMinimumHeight() + ","
                        + drawable.getMinimumWidth());
        return drawable;
    } catch (MalformedURLException e) {
        Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
        return null;
    } catch (IOException e) {
        Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
        return null;
    }
}

From source file:com.alex.view.loop.IndicatorView.java

/**
 * drawablebitmap/*from  w w  w .  jav a 2 s.co  m*/
 *
 * @param drawable
 * @return
 */
private Bitmap drawableToBitamp(Drawable drawable) {
    if (null == drawable) {
        return null;
    }
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:org.totschnig.myexpenses.util.Utils.java

public static Bitmap getTintedBitmapForTheme(Context context, int drawableResId, int themeResId) {
    Context wrappedContext = new ContextThemeWrapper(context, themeResId);
    Drawable d = AppCompatDrawableManager.get().getDrawable(wrappedContext, drawableResId);
    Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    d.setBounds(0, 0, c.getWidth(), c.getHeight());
    d.draw(c);//  w w w.j  a va  2  s  .  c  o  m
    return b;
}

From source file:com.shizhefei.view.largeimage.UpdateImageView.java

@Override
public void setImage(BitmapDecoderFactory factory, Drawable defaultDrawable) {
    mScale = 1;//ww w. ja v  a 2  s  .  com
    mOffsetX = 0;
    mOffsetY = 0;
    mDrawable = null;
    this.mFactory = factory;
    this.defaultDrawable = defaultDrawable;
    if (defaultDrawable != null) {
        onLoadImageSize(defaultDrawable.getIntrinsicWidth(), defaultDrawable.getIntrinsicHeight());
    }
    imageBlockLoader.load(factory);
}

From source file:com.android.incallui.CallCardFragment.java

/**
 * Converts a drawable into a bitmap./*from w  w  w.  j  a v a2s .  co m*/
 *
 * @param drawable the drawable to be converted.
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            // Needed for drawables that are just a colour.
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    Bitmap.Config.ARGB_8888);
        }

        Log.i(TAG, "Created bitmap with width " + bitmap.getWidth() + ", height " + bitmap.getHeight());

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    return bitmap;
}

From source file:com.darly.im.ui.CCPActivityBase.java

/**
 *
 * @param padding//from w  w  w .j a va  2 s.co  m
 * @param iconRes
 * @return
 */
private VerticalImageSpan getTitleIconTips(int padding, int iconRes) {
    Drawable drawable = mActionBarActivity.getResources().getDrawable(iconRes);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    VerticalImageSpan imageSpan = new VerticalImageSpan(drawable);
    imageSpan.setPadding((drawable.getIntrinsicHeight() - padding) / 2);
    return imageSpan;
}

From source file:com.craftingmobile.alertdialogusage.LoginDialogFragment.java

/**
 * Returns an error icon for use with//from w  w w .j  ava 2s  .c  o  m
 * {@link EditText#setError(CharSequence)}
 * 
 * @return A {@link Drawable} of the error icon
 */
public Drawable getErrorDrawable() {
    Resources r = getActivity().getResources();
    Drawable drawable = r.getDrawable(R.drawable.custom_indicator_input_error);

    /**
     * We have to set the bounds here because they will default
     * to zero for all values, meaning our icon will not display
     */
    drawable.setBounds(0, // Left 
            0, // Top
            drawable.getIntrinsicWidth(), // Right
            drawable.getIntrinsicHeight()); // Bottom

    return drawable;

}

From source file:org.mozilla.gecko.toolbar.SiteIdentityPopup.java

private void setSecurityStateIcon(int resource, int factor) {
    final Drawable stateIcon = ContextCompat.getDrawable(mContext, resource);
    stateIcon.setBounds(0, 0, stateIcon.getIntrinsicWidth() / factor, stateIcon.getIntrinsicHeight() / factor);
    mSecurityState.setCompoundDrawables(stateIcon, null, null, null);
    mSecurityState//from   w  w w .j a v a  2  s .co m
            .setCompoundDrawablePadding((int) mResources.getDimension(R.dimen.doorhanger_drawable_padding));
}

From source file:com.resonos.apps.library.tabviewpager.TabPageIndicator.java

/**
 * Does a bit of calculations to better measure the size of tabs.
 *  This is to fix a bug where text would occasionally get cut off.
 *///  w  w w. ja  v  a2 s . com
private void doPreMeasure() {
    TabView tabView = (TabView) mInflater.inflate(R.layout.vpi__tab, null);
    TextView textView = (TextView) tabView.findViewById(android.R.id.text1);
    ImageView imageView = (ImageView) tabView.findViewById(R.id.img);
    mTextPaint = textView.getPaint();
    mTextPadding = textView.getTotalPaddingLeft() + textView.getTotalPaddingRight() + tabView.getPaddingLeft()
            + tabView.getPaddingRight();
    int mImagePadding = imageView.getPaddingLeft() + imageView.getPaddingRight();

    int t = 0;
    int count = getTitleProvider().getCount();
    mAllMinWidths = new int[count];
    String text;
    Drawable d;
    for (int i = 0; i < count; i++) {
        d = getTitleProvider().getIcon(i);
        text = getTitleProvider().getTitle(i);
        if (d != null) {
            mAllMinWidths[i] = d.getIntrinsicWidth() + mImagePadding;
        } else {
            StaticLayout layout = new StaticLayout(text, mTextPaint, MAX_TAB_WIDTH, Alignment.ALIGN_NORMAL,
                    1.0f, 0.0f, false);
            int w = (int) (layout.getLineWidth(0) + 1);
            mAllMinWidths[i] = mTextPadding + w;
        }
        if (getTitleProvider().isVisible(i))
            t += mAllMinWidths[i];
    }
    mTotalMinWidth = t;
}