Add TopCrop option to ScaleType (#372)
This commit is contained in:
@@ -15,6 +15,13 @@ enum class ScaleType {
|
||||
* The preview will be scaled so as its one dimensions will be equal and the other one equal or
|
||||
* smaller than the corresponding dimension of the view
|
||||
*/
|
||||
CenterInside
|
||||
CenterInside,
|
||||
|
||||
|
||||
/**
|
||||
* The preview will be scaled so as its one dimensions will be equal and the other one equal or
|
||||
* larger than the corresponding dimension of the view with focus on the top part
|
||||
*/
|
||||
TopCrop,
|
||||
|
||||
}
|
||||
@@ -82,6 +82,7 @@ private fun ViewGroup.layoutTextureView(
|
||||
) = when (scaleType) {
|
||||
ScaleType.CenterInside -> previewResolution?.centerInside(this)
|
||||
ScaleType.CenterCrop -> previewResolution?.centerCrop(this)
|
||||
ScaleType.TopCrop -> previewResolution?.topCrop(this)
|
||||
else -> null
|
||||
}
|
||||
|
||||
@@ -129,6 +130,28 @@ private fun Resolution.centerCrop(view: ViewGroup) {
|
||||
view.layoutChildrenAt(rect)
|
||||
}
|
||||
|
||||
private fun Resolution.topCrop(view: ViewGroup) {
|
||||
val scale = Math.max(
|
||||
view.measuredWidth / width.toFloat(),
|
||||
view.measuredHeight / height.toFloat()
|
||||
)
|
||||
|
||||
val width = (width * scale).toInt()
|
||||
val height = (height * scale).toInt()
|
||||
|
||||
val extraX = Math.max(0, width - view.measuredWidth)
|
||||
|
||||
val rect = Rect(
|
||||
-extraX / 2,
|
||||
0,
|
||||
width - extraX / 2,
|
||||
height
|
||||
)
|
||||
|
||||
view.layoutChildrenAt(rect)
|
||||
}
|
||||
|
||||
|
||||
private fun ViewGroup.layoutChildrenAt(rect: Rect) {
|
||||
(0 until childCount).forEach {
|
||||
getChildAt(it).layout(
|
||||
|
||||
Reference in New Issue
Block a user