Drawable getIntrinsicWidth - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Drawable getIntrinsicWidth method.

This code example shows how to use the following methods:getIntrinsicHeight, getIntrinsicWidth, setBounds
 
    final int width = bounds.width();
    final int height = bounds.height();
    final float ratio = (float) width / height;
 
    final int drawableWidth = drawable.getIntrinsicWidth();
    final int drawableHeight = drawable.getIntrinsicHeight();
    final float drawableRatio = (float) drawableWidth / drawableHeight;
 
    if (drawableRatio < ratio) {
      final float scale = (float) height / drawableHeight;
      final int scaledDrawableWidth = (int) (drawableWidth * scale);
      final int drawableLeft = bounds.left - (scaledDrawableWidth - width) / 2;
      final int drawableRight = drawableLeft + scaledDrawableWidth;
      drawable.setBounds(drawableLeft, bounds.top, drawableRight, bounds.bottom);
    } else { 
      final float scale = (float) width / drawableWidth;
      final int scaledDrawableHeight = (int) (drawableHeight * scale);
      final int drawableTop = bounds.top - (scaledDrawableHeight - height) / 2;
      final int drawableBottom = drawableTop + scaledDrawableHeight;

9
CodeRank
This code example shows how to use the following methods:draw, getBounds, getIntrinsicHeight, getIntrinsicWidth, setBounds
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(mDisplayMetrics);
            } 
        } 
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
 
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes. 
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down. 
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                } 
            } else if (sourceWidth < width && sourceHeight < height) {
                // It's small, use the size they gave us. 

9
CodeRank
This code example shows how to use the following methods:getIntrinsicHeight, getIntrinsicWidth
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, setBounds, setState
	@Override 
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
 
		if (image != null) {
			image.setState(getDrawableState());
 
			// scale image to fit inside button 
 
			int imgHeight = image.getIntrinsicHeight();
			int imgWidth = image.getIntrinsicWidth();
			int btnWidth = getWidth();
			int btnHeight = getHeight();
			float scale;
 
			if (imgWidth <= btnWidth && imgHeight <= btnHeight) {
                scale = 1.0f;
            } else { 
                scale = Math.min((float) btnWidth / (float) imgWidth,
                        (float) btnHeight / (float) imgHeight);

8
CodeRank
This code example shows how to use the following methods:draw, getBounds, getIntrinsicHeight, getIntrinsicWidth, getOpacity
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            } 
        } 
        int iconWidth = icon.getIntrinsicWidth();
        int iconHeight = icon.getIntrinsicHeight();
 
        if (width > 0 && height > 0) {
            if (width < iconWidth || height < iconHeight || scale != 1.0f) {
                final float ratio = (float) iconWidth / iconHeight;
 
                if (iconWidth > iconHeight) {
                    height = (int) (width / ratio);
                } else if (iconHeight > iconWidth) {
                    width = (int) (height * ratio);
                } 
 
                final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;

8
CodeRank
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, setBounds
            final int h = getHeight();
 
            frameBounds.set(0, 0, w, h);
            mFrame.setBounds(frameBounds);
            if (mOverlay != null) {
                mOverlay.setBounds(w - mOverlay.getIntrinsicWidth(),
                        h - mOverlay.getIntrinsicHeight(), w, h);
            } 
        } 
 
        mFrame.draw(canvas);
        if (mOverlay != null) {
            mOverlay.draw(canvas);
        } 
    } 
 
 
    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
8
CodeRank
This code example shows how to use the following methods:getIntrinsicHeight, getIntrinsicWidth, setBounds
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
 
        mDrawable = drawable;
        if (mDrawable != null) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        } 
    } 
 
    public void setCenter(float x, float y) {
        mCenterX = x;
        mCenterY = y;
    } 
 
    public void makePointCloud(float innerRadius, float outerRadius) {
        if (innerRadius == 0) {
            Log.w(TAG, "Must specify an inner radius");
            return; 
        } 
        mOuterRadius = outerRadius;
8
CodeRank
This code example shows how to use the following methods:getIntrinsicHeight, getIntrinsicWidth, setBounds
 
        if (marker == null) {
            return null; 
        } 
 
        final int width = marker.getIntrinsicWidth();
        final int height = marker.getIntrinsicHeight();
 
        if (width < 0 || height < 0) {
            throw new IllegalStateException("The given Drawable has no intrinsic width or height");
        } 
 
        int left, top;
 
        switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.LEFT:
                left = 0;
                break; 
            case Gravity.RIGHT:
                left = -width;

8
CodeRank
This code example shows how to use the following methods:getIntrinsicHeight, getIntrinsicWidth, setCallback, setState, setVisible
This code example shows how to use the following methods:draw, getIntrinsicHeight, getIntrinsicWidth, getOpacity, setBounds
     * 将Drawable转化为Bitmap 
     * @param drawable 
     * @return 
     */ 
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
 
    } 
 
    /** 
     * 获得圆角图片的方法 
     * @param bitmap 
8
CodeRank

Related Drawable getIntrinsicWidth Questions & Answers:

getIntrinsicWidth Method Overview
Return the intrinsic width of the underlying drawable object.
See Code Examples for other Android Drawable Methods: