Skip to content

Instantly share code, notes, and snippets.

@chedabob
Last active August 29, 2015 13:58
Show Gist options
  • Save chedabob/9994080 to your computer and use it in GitHub Desktop.
Save chedabob/9994080 to your computer and use it in GitHub Desktop.
Transparency utils
/**
* Adds a transparent gradient to the right edge of a bitmap
*
* Released into the public domain
*
* Created by chedabob on 05/04/2014.
* https://gist.github.com/chedabob/9994080
*
*/
/**
* Adds a transparent gradient to the right edge of the image
* @param input The input bitmap
* @param fadeStartPercentage The percentage of the image to start the fade at (expressed as 0.0f >= x <= 1.0f)
* @return Bitmap with gradient applied
*/
private static Bitmap transFadeRightEdge(Bitmap input, float fadeStartPercentage) {
int width = input.getWidth();
int height = input.getHeight();
int[] outPixels = new int[width * height];
input.getPixels(outPixels, 0, width, 0, 0, width, height);
int widthFadePixel = (int) (width * fadeStartPercentage);
float fadeWidth = (float) (width - widthFadePixel);
int xAlpha = 0;
int index = 0;
int indexStart = (height - 1) * width;
for (int x = widthFadePixel; x < width; ++x) {
xAlpha = ((int) ((1.0f - ((float) (x - widthFadePixel) / fadeWidth)) * 255)) << 24;
index = indexStart + x;
// Internal loop runs faster when run in reverse
for (; index >= 0; index -= width) {
outPixels[index] = (outPixels[index] & 0x00ffffff) | xAlpha;
}
}
return Bitmap.createBitmap(outPixels, width, height, Bitmap.Config.ARGB_8888);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment