I'm trying to create a "Transform" for the Android library Picasso. The problem (I think) though is more generic to how ComposeShaders work.
I'm trying to achieve a straightforward vertical linear gradient applied on top of the image, using Bitmap shaders. Here's the code:
@Override
public Bitmap transform(Bitmap source) {
Bitmap bitmap = Bitmap.createBitmap(source.getWidth(),
source.getHeight(),
source.getConfig());
shaders[0] = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
shaders[1] = new LinearGradient(0,
0,
0,
source.getHeight(),
Color.BLACK,
Color.TRANSPARENT,
Shader.TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(shaders[0],
shaders[1],
PorterDuff.Mode.DST_IN);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawPaint(paint);
source.recycle();
return bitmap;
}
When running the tranformation, I get the nice vertical gradient, but the Bitmap is not seen at all. So i basically only get a vertical gradient. Help?
Other relevant code:
Picasso.with(getActivity())
.load(myValidUrl)
.transform(new TopVignetteTransformation())
.into(myImageview);
<ImageView
android:background="@color/dark_gray"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
Arrgh. Stupid typo. Sourcing the wrong bitmap for the Bitmap shader
shaders[0] = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);