Skip to content

Commit

Permalink
RUM-7472: Fix center crop image is not cropped in wireframe
Browse files Browse the repository at this point in the history
  • Loading branch information
ambushwork committed Jan 9, 2025
1 parent ce2ed62 commit 4b9d3b3
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dd-sdk-android-internal/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sealed class com.datadog.android.internal.telemetry.InternalTelemetryEvent
const val REPORTING_SAMPLING_RATE_KEY: String
fun ByteArray.toHexString(): String
object com.datadog.android.internal.utils.ImageViewUtils
fun resolveParentRectAbsPosition(android.view.View): android.graphics.Rect
fun resolveParentRectAbsPosition(android.view.View, Boolean = true): android.graphics.Rect
fun calculateClipping(android.graphics.Rect, android.graphics.Rect, Float): android.graphics.Rect
fun resolveContentRectWithScaling(android.widget.ImageView, android.graphics.drawable.Drawable, android.widget.ImageView.ScaleType? = null): android.graphics.Rect
fun Int.densityNormalized(Float): Int
Expand Down
3 changes: 2 additions & 1 deletion dd-sdk-android-internal/api/dd-sdk-android-internal.api
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public final class com/datadog/android/internal/utils/ImageViewUtils {
public final fun calculateClipping (Landroid/graphics/Rect;Landroid/graphics/Rect;F)Landroid/graphics/Rect;
public final fun resolveContentRectWithScaling (Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/widget/ImageView$ScaleType;)Landroid/graphics/Rect;
public static synthetic fun resolveContentRectWithScaling$default (Lcom/datadog/android/internal/utils/ImageViewUtils;Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;Landroid/widget/ImageView$ScaleType;ILjava/lang/Object;)Landroid/graphics/Rect;
public final fun resolveParentRectAbsPosition (Landroid/view/View;)Landroid/graphics/Rect;
public final fun resolveParentRectAbsPosition (Landroid/view/View;Z)Landroid/graphics/Rect;
public static synthetic fun resolveParentRectAbsPosition$default (Lcom/datadog/android/internal/utils/ImageViewUtils;Landroid/view/View;ZILjava/lang/Object;)Landroid/graphics/Rect;
}

public final class com/datadog/android/internal/utils/IntExtKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ object ImageViewUtils {
/**
* Resolves the absolute position on the screen of the given [View].
* @param view the [View].
* @param cropToPadding if the view has cropToPadding as true.
* @return the [Rect] representing the absolute position of the view.
*/
fun resolveParentRectAbsPosition(view: View): Rect {
fun resolveParentRectAbsPosition(view: View, cropToPadding: Boolean = true): Rect {
val coords = IntArray(2)
// this will always have size >= 2
@Suppress("UnsafeThirdPartyFunctionCall")
view.getLocationOnScreen(coords)
val leftPadding = view.paddingLeft
val rightPadding = view.paddingRight
val topPadding = view.paddingTop
val bottomPadding = view.paddingBottom
val leftPadding = if (cropToPadding) view.paddingLeft else 0
val rightPadding = if (cropToPadding) view.paddingRight else 0
val topPadding = if (cropToPadding) view.paddingTop else 0
val bottomPadding = if (cropToPadding) view.paddingBottom else 0
return Rect(
coords[0] + leftPadding,
coords[1] + topPadding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,14 @@ open class ImageViewMapper : BaseAsyncBackgroundWireframeMapper<ImageView> {

val drawable = view.drawable?.current ?: return wireframes

val parentRect = imageViewUtils.resolveParentRectAbsPosition(view)
val parentRect = imageViewUtils.resolveParentRectAbsPosition(view, view.cropToPadding)
val contentRect = imageViewUtils.resolveContentRectWithScaling(view, drawable)

val resources = view.resources
val density = resources.displayMetrics.density

val clipping = if (view.cropToPadding) {
val clipping =
imageViewUtils.calculateClipping(parentRect, contentRect, density).toWireframeClip()
} else {
null
}

val contentXPosInDp = contentRect.left.densityNormalized(density).toLong()
val contentYPosInDp = contentRect.top.densityNormalized(density).toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ internal class ImageViewMapperTest {
whenever(mockBackgroundDrawable.current).thenReturn(mockBackgroundDrawable)
whenever(mockDrawableToColorMapper.mapDrawableToColor(any(), eq(mockInternalLogger))) doReturn null

whenever(stubImageViewUtils.resolveParentRectAbsPosition(any())).thenReturn(stubParentRect)
whenever(stubImageViewUtils.resolveParentRectAbsPosition(any(), any())).thenReturn(
stubParentRect
)
whenever(stubImageViewUtils.resolveContentRectWithScaling(any(), any(), anyOrNull()))
.thenReturn(stubContentRect)
whenever(stubImageViewUtils.calculateClipping(any(), any(), any())).thenReturn(stubClipping)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
"type": "shape"
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "image",
"isEmpty": false
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "image",
"isEmpty": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
"type": "shape"
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "placeholder",
"label": "Image"
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "placeholder",
"label": "Image"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
"type": "shape"
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "image",
"isEmpty": false
},
{
"clip": {
"top": 2,
"bottom": 3,
"left": 0,
"right": 0
},
"type": "image",
"isEmpty": false
}
Expand Down

0 comments on commit 4b9d3b3

Please sign in to comment.