Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Created November 14, 2017 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolyall/ba0194fd77eca953ec8a0621f482cad9 to your computer and use it in GitHub Desktop.
Save Kolyall/ba0194fd77eca953ec8a0621f482cad9 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.FloatRange;
import android.util.Log;
import com.google.common.base.MoreObjects;
import com.squareup.picasso.Transformation;
import java.util.Arrays;
import static android.graphics.Color.TRANSPARENT;
import static android.graphics.Shader.TileMode.CLAMP;
/**
* Overlays a gradient on the bitmap to be transformed.
*
* @since 2.1.0
*/
public class GradientTransformation implements Transformation {
private static final String TAG = GradientTransformation.class.getSimpleName();
private static final int[] sDefColors = {TRANSPARENT, TRANSPARENT}; // [0] set in constructor
private static final float[] sDefPositions = {0.5f, 1.0f};
private final Orientation mOrient;
private final int[] mColors;
private final float[] mPositions;
/**
* Use the default colors and positions. The first half of the overlay will be dark and
* transparent. The second half will fade from dark and transparent to completely transparent.
*/
public GradientTransformation(Context context, Orientation orientation) {
this(orientation, sDefColors, sDefPositions);
if (sDefColors[0] == TRANSPARENT) {
sDefColors[0] = context.getResources().getColor(R.color.text_protection);
}
}
/**
* Use the color resource and default positions. The first half of the overlay will be the
* color. The second half will fade from the color to transparent.
*
* @since 2.4.0
*/
public GradientTransformation(Context context, Orientation orientation, @ColorRes int colorId) {
this(orientation, new int[]{context.getResources().getColor(colorId), TRANSPARENT},
sDefPositions);
}
/**
* Apply a gradient of the colors at the positions in the direction of the orientation.
*
* @see LinearGradient
*/
public GradientTransformation(Orientation orientation, @ColorInt int[] colors,
@FloatRange(from = 0.0, to = 1.0) float[] positions) {
mOrient = orientation;
mColors = colors;
mPositions = positions;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap bm = Bitmaps.mutable(source);
if (bm == null) {
Log.e(TAG, "bitmap could not be copied, returning untransformed");
return source;
}
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
int width = bm.getWidth();
int height = bm.getHeight();
float x0 = 0.0f, y0 = 0.0f, x1 = 0.0f, y1 = 0.0f;
switch (mOrient) {
case BOTTOM_TOP:
y0 = height;
break;
case BL_TR:
y0 = height;
x1 = width;
break;
case LEFT_RIGHT:
x1 = width;
break;
case TL_BR:
x1 = width;
y1 = height;
break;
case TOP_BOTTOM:
y1 = height;
break;
case TR_BL:
x0 = width;
y1 = height;
break;
case RIGHT_LEFT:
x0 = width;
break;
case BR_TL:
x0 = width;
y0 = height;
break;
}
paint.setShader(new LinearGradient(x0, y0, x1, y1, mColors, mPositions, CLAMP));
canvas.drawRect(0.0f, 0.0f, width, height, paint);
return bm;
}
@Override
public String key() {
return "GradientTransformation";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment