Merge cherrypicks of ['googleplex-android-review.googlesource.com/28269884'] into 24Q3-release.

Change-Id: Iba6a609e507c25456618bfe53ebe035ecd1ede6d
diff --git a/libraries/screenshot/src/main/java/platform/test/screenshot/matchers/AlmostPerfectMatcher.kt b/libraries/screenshot/src/main/java/platform/test/screenshot/matchers/AlmostPerfectMatcher.kt
index 2098612..dcf7c57 100644
--- a/libraries/screenshot/src/main/java/platform/test/screenshot/matchers/AlmostPerfectMatcher.kt
+++ b/libraries/screenshot/src/main/java/platform/test/screenshot/matchers/AlmostPerfectMatcher.kt
@@ -24,9 +24,17 @@
  * Matcher for differences not detectable by human eye. The relaxed threshold allows for low quality
  * png storage.
  */
-class AlmostPerfectMatcher(
+class AlmostPerfectMatcher
+private constructor(
     private val acceptableThreshold: Double = 0.0,
+    private val acceptableThresholdCount: Int = -1
 ) : BitmapMatcher() {
+    constructor() : this(0.0, -1)
+
+    constructor(acceptableThreshold: Double) : this(acceptableThreshold, -1)
+
+    constructor(acceptableThresholdCount: Int) : this(0.0, acceptableThresholdCount)
+
     override fun compareBitmaps(
         expected: IntArray,
         given: IntArray,
@@ -51,7 +59,10 @@
             }
         }
 
-        val matches = different <= (acceptableThreshold * width * height)
+        val threshold =
+            if (acceptableThresholdCount >= 0) acceptableThresholdCount
+            else (acceptableThreshold * width * height).toInt()
+        val matches = different <= threshold
         val diffBmp =
             if (different <= 0) null
             else Bitmap.createBitmap(diffArray.value, width, height, Bitmap.Config.ARGB_8888)
@@ -100,7 +111,10 @@
             }
         }
 
-        val matches = different <= (acceptableThreshold * width * height)
+        val threshold =
+            if (acceptableThresholdCount >= 0) acceptableThresholdCount
+            else (acceptableThreshold * width * height).toInt()
+        val matches = different <= threshold
         val diffBmp =
             if (different <= 0) null
             else Bitmap.createBitmap(diffArray.value, width, height, Bitmap.Config.ARGB_8888)