Code example for Drawable

Methods: drawgetBoundsgetIntrinsicHeightgetIntrinsicWidthsetBounds

0
            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. 
                width = sourceWidth;
                height = sourceHeight;
            } 
        } 
 
        // no intrinsic size --> use default size 
        int textureWidth = mIconTextureWidth;
        int textureHeight = mIconTextureHeight;
 
        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = mCanvas;
        canvas.setBitmap(bitmap);
 
        final int left = (textureWidth-width) / 2;
        final int top = (textureHeight-height) / 2;
 
        if (false) { 
            // draw a big box for the icon for debugging 
            canvas.drawColor(sColors[mColorIndex]);
            if (++mColorIndex >= sColors.length) mColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left+width, top+height, debugPaint);
        } 
 
        mOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left+width, top+height);
        icon.draw(canvas);
        icon.setBounds(mOldBounds);
 
        return bitmap;
    } 
 
    private Bitmap createSelectedBitmap(Bitmap src, boolean pressed) {