I am trying to add dynamic shortcuts to send messages to specific users in my app. I want to set the shortcut icon to the users' profile photos, but no matter what I do, my shortcut icons have a white border around them.
val contactCategories = setOf(SHARE_CATEGORY)
val icon = if (friend.photo != null) IconCompat.createWithBitmap(run {
val imageDecoded = Base64.decode(friend.photo, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(imageDecoded, 0, imageDecoded.size)
// From diesel - https://stackoverflow.com/a/15537470/7484693
// Licensed under CC BY-SA 3.0
val output: Bitmap = if (bitmap.width > bitmap.height) {
Bitmap.createBitmap(bitmap.height, bitmap.height, Bitmap.Config.ARGB_8888)
} else {
Bitmap.createBitmap(bitmap.width, bitmap.width, Bitmap.Config.ARGB_8888)
}
Log.d(MainViewModel::class.java.name, "${bitmap.width} x ${bitmap.height}")
val canvas = Canvas(output)
val color: UInt = 0xff424242u
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val r = if (bitmap.width > bitmap.height) {
(bitmap.height / 2).toFloat()
} else {
(bitmap.width / 2).toFloat()
}
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color.toInt()
canvas.drawCircle(r, r, r, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
output
}) else null
ShortcutManagerCompat.pushDynamicShortcut(
context,
ShortcutInfoCompat.Builder(context, friend.id).setShortLabel(friend.name).setPerson(
Person.Builder().setName(friend.name).setKey(friend.id).setImportant(true)
.setIcon(icon).build()
).setIcon(icon).setIntent(Intent(
context, MainActivity::class.java
).apply {
action = Intent.ACTION_SENDTO
putExtra(EXTRA_RECIPIENT, friend.id)
}).setLongLived(true).setCategories(contactCategories).setPerson(
Person.Builder().setName(friend.name).build()
).build()
)
Most of the code is to get a circular icon, but even without the crop, the icon is scaled "to fit", so the corners are still visible. I'd like to scale it "to fill", so that none of the background is visible. The icon is a 128x128 bitmap, so it should be large enough, but scaling it up doesn't make a difference.
Here's a screenshot of what it currently looks like (notice the white border around the icon):
And here is what I would like it to look like:
I know this is possible because several of Google's first-party apps (Messages, Meet, Contacts) and some 3rd-party apps (Whatsapp, Discord, eBay, Snapchat) all support this. Is there some API I'm missing? Some method of creating the icon?
Any help is much appreciated!