PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(targetWidth); painter.setIntrinsicHeight(targetHeight); } else if (icon instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; if (bitmapDrawable.getBitmap().getDensity() == 0) bitmapDrawable.setTargetDensity(sSystemResource.getDisplayMetrics()); } int width = targetWidth; int height = targetHeight; int sourceWidth = icon.getIntrinsicWidth(); int sourceHeight = icon.getIntrinsicHeight(); if (sourceWidth > 0 && sourceWidth > 0) { if (width < sourceWidth || height < sourceHeight) { float ratio = (float) sourceWidth / (float) sourceHeight; if (sourceWidth > sourceHeight) height = (int) ((float) width / ratio); else if (sourceHeight > sourceWidth) width = (int) (ratio * (float) height);