Skip to content

Commit

Permalink
Add image scale
Browse files Browse the repository at this point in the history
  • Loading branch information
enotniy committed Jul 19, 2024
1 parent 45616df commit 0085a9d
Showing 1 changed file with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.app.Notification.DEFAULT_ALL
import android.app.Notification.VISIBILITY_PRIVATE
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.os.Build
import androidx.annotation.DrawableRes
Expand All @@ -20,6 +22,7 @@ import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import cloud.mindbox.mobile_sdk.pushes.handler.MessageHandlingState
import cloud.mindbox.mobile_sdk.pushes.handler.MindboxMessageHandler
import cloud.mindbox.mobile_sdk.pushes.handler.image.ImageRetryStrategy
import cloud.mindbox.mobile_sdk.px
import cloud.mindbox.mobile_sdk.services.BackgroundWorkManager
import cloud.mindbox.mobile_sdk.utils.Generator
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
Expand Down Expand Up @@ -647,15 +650,49 @@ internal object PushNotificationManager {
): NotificationCompat.Builder {
setLargeIcon(imageBitmap)

val resizedBitmap = createCenterInsideBitmap(imageBitmap, Resources.getSystem().displayMetrics.widthPixels - 100.px, 168.px)
//val resizedBitmap = createCenterInsideBitmapHorizontalPadding(imageBitmap, 311.px, 168.px)

val style = NotificationCompat.BigPictureStyle()
.bigPicture(imageBitmap)
.bigPicture(resizedBitmap)
.bigLargeIcon(null)
.setBigContentTitle(title)
text?.let(style::setSummaryText)

return setStyle(style)
}


// private fun resizeBitmapToFitHeight(bitmap: Bitmap, targetHeightDp: Float): Bitmap {
// // Конвертация dp в пиксели
// val targetHeightPx = targetHeightDp.toInt().px
//
// // Изменение размера битмапа до нужной высоты
// val aspectRatio = bitmap.width.toFloat() / bitmap.height.toFloat()
// val scaleWidthPx = (targetHeightPx * aspectRatio).toInt()
//
// val scaledBitmap = bitmap.scale(scaleWidthPx, targetHeightPx, false)
//
// // Создание нового битмапа с прозрачными полосами, если ширина меньше, чем 256dp в пикселях
// val targetWidthPx = Resources.getSystem().displayMetrics.widthPixels - 64.px
//
// if (scaleWidthPx < targetWidthPx) {
// val paddedBitmap = Bitmap.createBitmap(targetWidthDpPx, targetHeightPx, Bitmap.Config.ARGB_8888)
// val canvas = Canvas(paddedBitmap)
// val paint = Paint()
// paint.color = Color.TRANSPARENT
// canvas.drawBitmap(
// scaledBitmap,
// ((targetWidthDpPx - targetWidthPx) / 2).toFloat(),
// 0f,
// null
// )
// return paddedBitmap
// }
//
// return scaledBitmap
// }

private fun NotificationCompat.Builder.setText(
text: String?,
) = LoggingExceptionHandler.runCatching {
Expand Down Expand Up @@ -694,4 +731,25 @@ internal object PushNotificationManager {
`package` = context.packageName
}

private fun createCenterInsideBitmap(src: Bitmap, targetWidth: Int, targetHeight: Int): Bitmap {
val srcWidth = src.width
val srcHeight = src.height

val scale = minOf(targetWidth.toFloat() / srcWidth, targetHeight.toFloat() / srcHeight)

val scaledWidth = (srcWidth * scale).toInt()
val scaledHeight = (srcHeight * scale).toInt()

val result = Bitmap.createBitmap(targetWidth, scaledHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(result)
canvas.drawColor(Color.TRANSPARENT)

val left = (targetWidth - scaledWidth) / 2

val scaledBitmap = Bitmap.createScaledBitmap(src, scaledWidth, scaledHeight, true)
canvas.drawBitmap(scaledBitmap, left.toFloat(), 0f, null)

return result
}

}

0 comments on commit 0085a9d

Please sign in to comment.