1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.wallpaper.weathereffects.utils
/** Contains offsets and scales to position image inside a surface. */
class ImageCrop(
/**
* The left start of the image relatively to the left edge of the surface that will contain
* the image.
*/
val leftOffset: Float = 0f,
/**
* The top distance start of the image relatively to the top edge of the surface that will
* contain the image.
*/
val topOffset: Float = 0f,
/** The horizontal scale applied to the image. */
val horizontalScale: Float = 1f,
/** The vertical scale applied to the image. */
val verticalScale: Float = 1f,
) {
companion object {
/**
* Calculates the [ImageCrop] that would make the image cover the surface (that is, the
* image will be scaled to the smallest possible size so it fills the container,
* preserving the aspect ratio and cropping the image vertically or horizontally if
* necessary) and center its content.
*
* @param surfaceWidth the width of the surface where the image will be displayed.
* @param surfaceWidth the height of the surface where the image will be displayed.
* @param imageWidth the width of the image that we want to display.
* @param imageHeight the height of the image that we want to display.
*
* @return the [ImageCrop] that will center cover the image into the surface.
*/
fun centerCoverCrop(
surfaceWidth: Float,
surfaceHeight: Float,
imageWidth: Float,
imageHeight: Float
): ImageCrop {
val uvScaleHeight: Float = imageHeight / surfaceHeight
val uvScaleWidth: Float = imageWidth / surfaceWidth
val uvScale = if (imageWidth / imageHeight > surfaceWidth / surfaceHeight) {
uvScaleHeight
} else {
uvScaleWidth
}
val horizontalOffset = (imageWidth - surfaceWidth * uvScale) / 2f
val verticalOffset = (imageHeight - surfaceHeight * uvScale) / 2f
return ImageCrop(horizontalOffset, verticalOffset, uvScale, uvScale)
}
}
}
|