Created
August 24, 2016 13:36
-
-
Save KKorvin/d2ef4cac316633355ff7b145f82ee979 to your computer and use it in GitHub Desktop.
Scaling Bitmap on the fly. For Android, including main thread, background thread and Glide examples.
This file contains 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
//Scale function: | |
/** | |
* Creates new bitmap by scaling given one | |
* | |
* @param scaleFactor | |
* @param bitmap | |
* @return new bitmap | |
*/ | |
public static Bitmap createScaledBitmap(Bitmap bitmap, float scaleFactor) { | |
Matrix m = new Matrix(); | |
m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), | |
new RectF(0, 0, bitmap.getWidth() * scaleFactor, bitmap.getHeight() * scaleFactor), Matrix.ScaleToFit.CENTER); | |
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); | |
} | |
// Example of usage im main thread: | |
Bitmap scaledBitmap = UIHelper.createScaledBitmap(letterTile, 2f); | |
// Example of usage in background thread | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
final Bitmap scaledBitmap = createScaledBitmap(letterTile, 2f); | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
anyImageView.setImageBitmap(scaledBitmap); | |
} | |
}); | |
} | |
}).run(); | |
//Example of usage with Glide: | |
Glide.with(getActivity()).load(playableItem.getCoverImageUrl()).crossFade().into(new GlideDrawableImageViewTarget(imgDetailedTopCover) { | |
@Override | |
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) { | |
super.onResourceReady(drawable, anim); | |
Bitmap bitmap = ((GlideBitmapDrawable) drawable).getBitmap(); | |
anyImageView.setImageBitmap(createScaledBitmap(bitmap, 2f)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment