Created
December 6, 2012 12:52
-
-
Save jdamcd/4224231 to your computer and use it in GitHub Desktop.
Thumbnail scaling for Android notfications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NotificationThumbnailHelper { | |
private float targetWidth; | |
private float targetHeight; | |
public NotificationThumbnailHelper(Context context) { | |
targetWidth = getTargetWidth(context); | |
targetHeight = getTargetHeight(context); | |
} | |
public Bitmap scaleBitmap(Bitmap input) { | |
if (input.getWidth() >= input.getHeight()) { | |
return scaleLandscapeThumbnail(input); | |
} else { | |
return scalePortraitThumbnail(input); | |
} | |
} | |
private Bitmap scaleLandscapeThumbnail(Bitmap input) { | |
float scale = targetHeight / input.getHeight(); | |
int scaledWidth = (int) (input.getWidth() * scale); | |
Bitmap scaled = Bitmap.createScaledBitmap(input, scaledWidth, (int) targetHeight, true); | |
input.recycle(); | |
return scaled; | |
} | |
private Bitmap scalePortraitThumbnail(Bitmap input) { | |
float scale = targetWidth / input.getWidth(); | |
int scaledHeight = (int) (input.getHeight() * scale); | |
Bitmap scaled = Bitmap.createScaledBitmap(input, (int) targetWidth, scaledHeight, true); | |
input.recycle(); | |
return scaled; | |
} | |
private float getTargetWidth(Context context) { | |
return context.getResources().getDimensionPixelSize(R.dimen.notification_large_icon_width); | |
} | |
private float getTargetHeight(Context context) { | |
return context.getResources().getDimensionPixelSize(R.dimen.notification_large_icon_height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah. Absolutely. This code was based on an assumption that the input
Bitmap
is throw-away. Thanks for making that clear!