src/main/java/ru/mail/dondokidon/extensions/BitmapCreator.java
/**
     * Create new Bitmap with scaled according ScaleType param Bitmap from resources within
     *
     * @param res The resources object containing the image data
     * @param resourceId The resource id of the image data
     * @param width Returned bitmap's width
     * @param height Returned bitmap's height
     * @param scaleType Scale type to decoded bitmap to fit into created bitmap
     * @return Created Bitmap, or null if the image could not be decoded from resources
     *
     * @throws OutOfMemoryError if resource is too large to decode
     */


@throws OutOfMemoryError if resource is too large to decode
@throws OutOfMemoryError if resource is too large to decode
@Nullable
    public static Bitmap createScaleTyped(@NotNull Resources res, int resourceId, int width, int height, ScaleType scaleType) {
        BitmapFactory.Options Options = new BitmapFactory.Options();
        Options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resourceId, Options);
        int scale;
        if (width == SIZE_PROPORTIONAL && height == SIZE_PROPORTIONAL)
            scale = 1;
        else if (width == SIZE_PROPORTIONAL){
            scale = Math.max(1, Options.outHeight / height);
        } else if (height == SIZE_PROPORTIONAL){
            scale = Math.max(1, Options.outWidth / width);
        } else {
            scale = Math.max(1, Math.min(Options.outWidth / width, Options.outHeight / height));
        }
        Options.inJustDecodeBounds = false;
        Options.inSampleSize = scale;
        Bitmap tempBitmap = BitmapFactory.decodeResource(res, resourceId, Options);
        if (tempBitmap == null){
            return null;
        }
        Bitmap result = createScaleTyped(tempBitmap, width, height, scaleType);
        tempBitmap.recycle();
        return result;
    }
@ Nullable public static Bitmap createScaleTyped ( @ NotNull Resources res , int resourceId , int width , int height , ScaleType scaleType ) { BitmapFactory . Options Options = new BitmapFactory . Options ( ) ; Options . inJustDecodeBounds = true ; BitmapFactory . decodeResource ( res , resourceId , Options ) ; int scale ; if ( width == SIZE_PROPORTIONAL && height == SIZE_PROPORTIONAL ) scale = 1 ; else if ( width == SIZE_PROPORTIONAL ) { scale = Math . max ( 1 , Options . outHeight / height ) ; } else if ( height == SIZE_PROPORTIONAL ) { scale = Math . max ( 1 , Options . outWidth / width ) ; } else { scale = Math . max ( 1 , Math . min ( Options . outWidth / width , Options . outHeight / height ) ) ; } Options . inJustDecodeBounds = false ; Options . inSampleSize = scale ; Bitmap tempBitmap = BitmapFactory . decodeResource ( res , resourceId , Options ) ; if ( tempBitmap == null ) { return null ; } Bitmap result = createScaleTyped ( tempBitmap , width , height , scaleType ) ; tempBitmap . recycle ( ) ; return result ; }
Create new Bitmap with scaled according ScaleType param Bitmap from resources within
 @param res The resources object containing the image data
 @param resourceId The resource id of the image data
 @param width Returned bitmap's width
 @param height Returned bitmap's height
 @param scaleType Scale type to decoded bitmap to fit into created bitmap
 @return Created Bitmap, or null if the image could not be decoded from resources
Create new Bitmap with scaled according ScaleType param Bitmap from resources within @param res The resources object containing the image data @param resourceId The resource id of the image data @param width Returned bitmap ' s width @param height Returned bitmap ' s height @param scaleType Scale type to decoded bitmap to fit into created bitmap @return Created Bitmap or null if the image could not be decoded from resources