Picasso image mask transform with border












1














I am trying to draw a border on an image with a squircle mask using a Picasso Transformation. The squircle mask is a VectorDrawable.



I think the easiest and most flexible way to do this is to first draw a larger squircle of a desired border color using canvas.drawPaint. Then draw a smaller squircle using the photo bitmap using canvas.drawBitmap. I can draw them both separately, I can scale the bitmap and draw it with the mask successfully, but any time I try to combine the two it loses the mask on the canvas.drawBitmap call. Any ideas on what I could be doing wrong?



I have tried loads of blend mode options but I don't think that is the issue.



override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 100

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = context.getDrawable(maskID)
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val bitmapPaint = Paint()
bitmapPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val bitmapMask = context.getDrawable(maskID)
bitmapMask.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
bitmapMask.draw(canvas)

val sourceDrawable = source.toDrawable(context.resources)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
canvas.drawBitmap(sourceDrawable.bitmap, null,
Rect(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2),
bitmapPaint)

source.recycle()

return output
}


Result:



Squircle with unmasked bitmap



If I comment out the call to drawBitmap I get this result (which is nearly there!):



Squircle with mask only










share|improve this question
























  • i think you will end up drawing 3 times. First outer region, then image with mask.
    – Rahul Kumar
    Nov 26 at 14:15
















1














I am trying to draw a border on an image with a squircle mask using a Picasso Transformation. The squircle mask is a VectorDrawable.



I think the easiest and most flexible way to do this is to first draw a larger squircle of a desired border color using canvas.drawPaint. Then draw a smaller squircle using the photo bitmap using canvas.drawBitmap. I can draw them both separately, I can scale the bitmap and draw it with the mask successfully, but any time I try to combine the two it loses the mask on the canvas.drawBitmap call. Any ideas on what I could be doing wrong?



I have tried loads of blend mode options but I don't think that is the issue.



override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 100

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = context.getDrawable(maskID)
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val bitmapPaint = Paint()
bitmapPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val bitmapMask = context.getDrawable(maskID)
bitmapMask.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
bitmapMask.draw(canvas)

val sourceDrawable = source.toDrawable(context.resources)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
canvas.drawBitmap(sourceDrawable.bitmap, null,
Rect(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2),
bitmapPaint)

source.recycle()

return output
}


Result:



Squircle with unmasked bitmap



If I comment out the call to drawBitmap I get this result (which is nearly there!):



Squircle with mask only










share|improve this question
























  • i think you will end up drawing 3 times. First outer region, then image with mask.
    – Rahul Kumar
    Nov 26 at 14:15














1












1








1







I am trying to draw a border on an image with a squircle mask using a Picasso Transformation. The squircle mask is a VectorDrawable.



I think the easiest and most flexible way to do this is to first draw a larger squircle of a desired border color using canvas.drawPaint. Then draw a smaller squircle using the photo bitmap using canvas.drawBitmap. I can draw them both separately, I can scale the bitmap and draw it with the mask successfully, but any time I try to combine the two it loses the mask on the canvas.drawBitmap call. Any ideas on what I could be doing wrong?



I have tried loads of blend mode options but I don't think that is the issue.



override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 100

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = context.getDrawable(maskID)
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val bitmapPaint = Paint()
bitmapPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val bitmapMask = context.getDrawable(maskID)
bitmapMask.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
bitmapMask.draw(canvas)

val sourceDrawable = source.toDrawable(context.resources)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
canvas.drawBitmap(sourceDrawable.bitmap, null,
Rect(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2),
bitmapPaint)

source.recycle()

return output
}


Result:



Squircle with unmasked bitmap



If I comment out the call to drawBitmap I get this result (which is nearly there!):



Squircle with mask only










share|improve this question















I am trying to draw a border on an image with a squircle mask using a Picasso Transformation. The squircle mask is a VectorDrawable.



I think the easiest and most flexible way to do this is to first draw a larger squircle of a desired border color using canvas.drawPaint. Then draw a smaller squircle using the photo bitmap using canvas.drawBitmap. I can draw them both separately, I can scale the bitmap and draw it with the mask successfully, but any time I try to combine the two it loses the mask on the canvas.drawBitmap call. Any ideas on what I could be doing wrong?



I have tried loads of blend mode options but I don't think that is the issue.



override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 100

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = context.getDrawable(maskID)
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val bitmapPaint = Paint()
bitmapPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val bitmapMask = context.getDrawable(maskID)
bitmapMask.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
bitmapMask.draw(canvas)

val sourceDrawable = source.toDrawable(context.resources)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
canvas.drawBitmap(sourceDrawable.bitmap, null,
Rect(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2),
bitmapPaint)

source.recycle()

return output
}


Result:



Squircle with unmasked bitmap



If I comment out the call to drawBitmap I get this result (which is nearly there!):



Squircle with mask only







android kotlin android-canvas picasso android-image






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 at 11:35









aminography

5,20021129




5,20021129










asked Nov 23 at 0:32









Daniel Wilson

7,95784569




7,95784569












  • i think you will end up drawing 3 times. First outer region, then image with mask.
    – Rahul Kumar
    Nov 26 at 14:15


















  • i think you will end up drawing 3 times. First outer region, then image with mask.
    – Rahul Kumar
    Nov 26 at 14:15
















i think you will end up drawing 3 times. First outer region, then image with mask.
– Rahul Kumar
Nov 26 at 14:15




i think you will end up drawing 3 times. First outer region, then image with mask.
– Rahul Kumar
Nov 26 at 14:15












1 Answer
1






active

oldest

votes


















1





+300









UPDATED 27/11/2018



I've solved the problem by drawing the picture on a temp canvas with desired mask, then drawing the result bitmap over the main canvas. The source code and visual result are included here:



MaskTransformation.kt



import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import com.squareup.picasso.Transformation


class MaskTransformation(
private val context: Context,
@DrawableRes private val maskID: Int
) : Transformation {

override fun key(): String {
return "mask"
}

override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 400

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = ContextCompat.getDrawable(context, maskID)!!
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val maskingPaint = Paint()
maskingPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val maskDrawable = ContextCompat.getDrawable(context, maskID)!!
maskDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)

val overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val overlayCanvas = Canvas(overlayBitmap)
maskDrawable.draw(overlayCanvas)

val pictureBitmap = Bitmap.createBitmap(width - borderWidth, height - borderWidth, Bitmap.Config.ARGB_8888)
val pictureCanvas = Canvas(pictureBitmap)

val sourceDrawable = BitmapDrawable(context.resources, source)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
pictureCanvas.drawBitmap(
sourceDrawable.bitmap,
null,
Rect(0, 0, width - borderWidth, height - borderWidth),
Paint()
)

overlayCanvas.drawBitmap(pictureBitmap, (borderWidth / 2).toFloat(), (borderWidth / 2).toFloat(), maskingPaint)

canvas.drawBitmap(overlayBitmap, 0f, 0f, Paint())

source.recycle()

return output
}

}


MainActivity.kt



import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Picasso.get()
.load(R.drawable.img_aminography)
.transform(MaskTransformation(this, R.drawable.ic_squircle))
.into(imageView)

}
}


ic_squircle.xml



<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path android:fillColor="#000000"
android:pathData="M31.2,14.3v3.5c0,9.8,-5.9,13.5,-13.4,13.5h-3.5c-7.7,0,-13.5,-3.4,-13.5,-13.5v-3.5c0,-10.8,6,-13.5,13.5,-13.5h3.5C25.2,0.8,31.2,4.1,31.2,14.3"/>

</vector>


.



Visual Result



enter image description here






share|improve this answer























  • Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
    – Daniel Wilson
    Nov 28 at 0:07






  • 1




    Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
    – aminography
    Nov 28 at 5:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439406%2fpicasso-image-mask-transform-with-border%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1





+300









UPDATED 27/11/2018



I've solved the problem by drawing the picture on a temp canvas with desired mask, then drawing the result bitmap over the main canvas. The source code and visual result are included here:



MaskTransformation.kt



import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import com.squareup.picasso.Transformation


class MaskTransformation(
private val context: Context,
@DrawableRes private val maskID: Int
) : Transformation {

override fun key(): String {
return "mask"
}

override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 400

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = ContextCompat.getDrawable(context, maskID)!!
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val maskingPaint = Paint()
maskingPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val maskDrawable = ContextCompat.getDrawable(context, maskID)!!
maskDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)

val overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val overlayCanvas = Canvas(overlayBitmap)
maskDrawable.draw(overlayCanvas)

val pictureBitmap = Bitmap.createBitmap(width - borderWidth, height - borderWidth, Bitmap.Config.ARGB_8888)
val pictureCanvas = Canvas(pictureBitmap)

val sourceDrawable = BitmapDrawable(context.resources, source)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
pictureCanvas.drawBitmap(
sourceDrawable.bitmap,
null,
Rect(0, 0, width - borderWidth, height - borderWidth),
Paint()
)

overlayCanvas.drawBitmap(pictureBitmap, (borderWidth / 2).toFloat(), (borderWidth / 2).toFloat(), maskingPaint)

canvas.drawBitmap(overlayBitmap, 0f, 0f, Paint())

source.recycle()

return output
}

}


MainActivity.kt



import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Picasso.get()
.load(R.drawable.img_aminography)
.transform(MaskTransformation(this, R.drawable.ic_squircle))
.into(imageView)

}
}


ic_squircle.xml



<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path android:fillColor="#000000"
android:pathData="M31.2,14.3v3.5c0,9.8,-5.9,13.5,-13.4,13.5h-3.5c-7.7,0,-13.5,-3.4,-13.5,-13.5v-3.5c0,-10.8,6,-13.5,13.5,-13.5h3.5C25.2,0.8,31.2,4.1,31.2,14.3"/>

</vector>


.



Visual Result



enter image description here






share|improve this answer























  • Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
    – Daniel Wilson
    Nov 28 at 0:07






  • 1




    Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
    – aminography
    Nov 28 at 5:43
















1





+300









UPDATED 27/11/2018



I've solved the problem by drawing the picture on a temp canvas with desired mask, then drawing the result bitmap over the main canvas. The source code and visual result are included here:



MaskTransformation.kt



import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import com.squareup.picasso.Transformation


class MaskTransformation(
private val context: Context,
@DrawableRes private val maskID: Int
) : Transformation {

override fun key(): String {
return "mask"
}

override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 400

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = ContextCompat.getDrawable(context, maskID)!!
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val maskingPaint = Paint()
maskingPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val maskDrawable = ContextCompat.getDrawable(context, maskID)!!
maskDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)

val overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val overlayCanvas = Canvas(overlayBitmap)
maskDrawable.draw(overlayCanvas)

val pictureBitmap = Bitmap.createBitmap(width - borderWidth, height - borderWidth, Bitmap.Config.ARGB_8888)
val pictureCanvas = Canvas(pictureBitmap)

val sourceDrawable = BitmapDrawable(context.resources, source)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
pictureCanvas.drawBitmap(
sourceDrawable.bitmap,
null,
Rect(0, 0, width - borderWidth, height - borderWidth),
Paint()
)

overlayCanvas.drawBitmap(pictureBitmap, (borderWidth / 2).toFloat(), (borderWidth / 2).toFloat(), maskingPaint)

canvas.drawBitmap(overlayBitmap, 0f, 0f, Paint())

source.recycle()

return output
}

}


MainActivity.kt



import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Picasso.get()
.load(R.drawable.img_aminography)
.transform(MaskTransformation(this, R.drawable.ic_squircle))
.into(imageView)

}
}


ic_squircle.xml



<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path android:fillColor="#000000"
android:pathData="M31.2,14.3v3.5c0,9.8,-5.9,13.5,-13.4,13.5h-3.5c-7.7,0,-13.5,-3.4,-13.5,-13.5v-3.5c0,-10.8,6,-13.5,13.5,-13.5h3.5C25.2,0.8,31.2,4.1,31.2,14.3"/>

</vector>


.



Visual Result



enter image description here






share|improve this answer























  • Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
    – Daniel Wilson
    Nov 28 at 0:07






  • 1




    Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
    – aminography
    Nov 28 at 5:43














1





+300







1





+300



1




+300




UPDATED 27/11/2018



I've solved the problem by drawing the picture on a temp canvas with desired mask, then drawing the result bitmap over the main canvas. The source code and visual result are included here:



MaskTransformation.kt



import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import com.squareup.picasso.Transformation


class MaskTransformation(
private val context: Context,
@DrawableRes private val maskID: Int
) : Transformation {

override fun key(): String {
return "mask"
}

override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 400

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = ContextCompat.getDrawable(context, maskID)!!
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val maskingPaint = Paint()
maskingPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val maskDrawable = ContextCompat.getDrawable(context, maskID)!!
maskDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)

val overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val overlayCanvas = Canvas(overlayBitmap)
maskDrawable.draw(overlayCanvas)

val pictureBitmap = Bitmap.createBitmap(width - borderWidth, height - borderWidth, Bitmap.Config.ARGB_8888)
val pictureCanvas = Canvas(pictureBitmap)

val sourceDrawable = BitmapDrawable(context.resources, source)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
pictureCanvas.drawBitmap(
sourceDrawable.bitmap,
null,
Rect(0, 0, width - borderWidth, height - borderWidth),
Paint()
)

overlayCanvas.drawBitmap(pictureBitmap, (borderWidth / 2).toFloat(), (borderWidth / 2).toFloat(), maskingPaint)

canvas.drawBitmap(overlayBitmap, 0f, 0f, Paint())

source.recycle()

return output
}

}


MainActivity.kt



import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Picasso.get()
.load(R.drawable.img_aminography)
.transform(MaskTransformation(this, R.drawable.ic_squircle))
.into(imageView)

}
}


ic_squircle.xml



<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path android:fillColor="#000000"
android:pathData="M31.2,14.3v3.5c0,9.8,-5.9,13.5,-13.4,13.5h-3.5c-7.7,0,-13.5,-3.4,-13.5,-13.5v-3.5c0,-10.8,6,-13.5,13.5,-13.5h3.5C25.2,0.8,31.2,4.1,31.2,14.3"/>

</vector>


.



Visual Result



enter image description here






share|improve this answer














UPDATED 27/11/2018



I've solved the problem by drawing the picture on a temp canvas with desired mask, then drawing the result bitmap over the main canvas. The source code and visual result are included here:



MaskTransformation.kt



import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import com.squareup.picasso.Transformation


class MaskTransformation(
private val context: Context,
@DrawableRes private val maskID: Int
) : Transformation {

override fun key(): String {
return "mask"
}

override fun transform(source: Bitmap): Bitmap {
val width = source.width
val height = source.height
val borderWidth = 400

val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)

//Draw a full size, red squircle
val paint = Paint()
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
paint.color = Color.RED
val mask = ContextCompat.getDrawable(context, maskID)!!
mask.setBounds(0, 0, width, height)
mask.draw(canvas)
canvas.drawPaint(paint)

//Draw a masked, scaled down bitmap of the photo on top
val maskingPaint = Paint()
maskingPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val maskDrawable = ContextCompat.getDrawable(context, maskID)!!
maskDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)

val overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val overlayCanvas = Canvas(overlayBitmap)
maskDrawable.draw(overlayCanvas)

val pictureBitmap = Bitmap.createBitmap(width - borderWidth, height - borderWidth, Bitmap.Config.ARGB_8888)
val pictureCanvas = Canvas(pictureBitmap)

val sourceDrawable = BitmapDrawable(context.resources, source)
sourceDrawable.setBounds(borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2)
pictureCanvas.drawBitmap(
sourceDrawable.bitmap,
null,
Rect(0, 0, width - borderWidth, height - borderWidth),
Paint()
)

overlayCanvas.drawBitmap(pictureBitmap, (borderWidth / 2).toFloat(), (borderWidth / 2).toFloat(), maskingPaint)

canvas.drawBitmap(overlayBitmap, 0f, 0f, Paint())

source.recycle()

return output
}

}


MainActivity.kt



import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Picasso.get()
.load(R.drawable.img_aminography)
.transform(MaskTransformation(this, R.drawable.ic_squircle))
.into(imageView)

}
}


ic_squircle.xml



<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path android:fillColor="#000000"
android:pathData="M31.2,14.3v3.5c0,9.8,-5.9,13.5,-13.4,13.5h-3.5c-7.7,0,-13.5,-3.4,-13.5,-13.5v-3.5c0,-10.8,6,-13.5,13.5,-13.5h3.5C25.2,0.8,31.2,4.1,31.2,14.3"/>

</vector>


.



Visual Result



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 at 14:28

























answered Nov 26 at 20:56









aminography

5,20021129




5,20021129












  • Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
    – Daniel Wilson
    Nov 28 at 0:07






  • 1




    Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
    – aminography
    Nov 28 at 5:43


















  • Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
    – Daniel Wilson
    Nov 28 at 0:07






  • 1




    Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
    – aminography
    Nov 28 at 5:43
















Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
– Daniel Wilson
Nov 28 at 0:07




Great job! I was getting close but already spent probably 3 hours trying to figure it out :D
– Daniel Wilson
Nov 28 at 0:07




1




1




Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
– aminography
Nov 28 at 5:43




Thanks dude :) The behavior of masking was strange and I spent about 3 hours to overcome it as you said :D
– aminography
Nov 28 at 5:43


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439406%2fpicasso-image-mask-transform-with-border%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)