Редактор пользовательских изображений AndroidAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Редактор пользовательских изображений Android

Сообщение Anonymous »

Функция корректирует положение прямоугольника обрезки, чтобы он вписывался в границы повернутого и масштабированного изображения.
Она учитывает целевое соотношение сторон, если оно заблокировано.
Все настройки поворота, масштабирования и соотношения сторон выполняются. во внимание, чтобы обеспечить действительный и центрированный прямоугольник обрезки.
Но не работает должным образом, не возвращается назад и полностью занимает прямоугольник обрезки.

Код: Выделить всё

fun imageBoundaryCheckForRotation(
imageOffset: Offset,
cropOffset: Offset,
currImgWidth: Int,
currImgHeight: Int,
cropWidth: Int,
cropHeight: Int,
aspectRatioLocked: Boolean,
imageScaleFactor: Float,
rotationAngle: Float,
targetAspectRatio: Float // New parameter for aspect ratio
): Offset {
// Convert rotation angle to radians
val angleInRadians = Math.toRadians(rotationAngle.toDouble())

// Calculate rotated image dimensions
val rotatedWidth = abs(currImgWidth * cos(angleInRadians)) + abs(currImgHeight * sin(angleInRadians))
val rotatedHeight = abs(currImgWidth * sin(angleInRadians)) + abs(currImgHeight * cos(angleInRadians))

// Apply scaling to rotated dimensions
val scaledWidth = rotatedWidth * imageScaleFactor
val scaledHeight = rotatedHeight * imageScaleFactor

Log.d("BoundaryCheck", "Rotated Width: $rotatedWidth, Rotated Height: $rotatedHeight")
Log.d("BoundaryCheck", "Scaled Width: $scaledWidth, Scaled Height: $scaledHeight")

// Calculate target aspect ratio
val actualAspectRatio = scaledWidth / scaledHeight
var effectiveAspectRatio = targetAspectRatio

if (effectiveAspectRatio == 1f) { // Equivalent to SOURCE_IMAGE_ASPECT_RATIO
effectiveAspectRatio = actualAspectRatio.toFloat()
}

// Adjust crop dimensions based on the target aspect ratio
var finalCropWidth = cropWidth
var finalCropHeight = (cropWidth / effectiveAspectRatio).toInt()

if (finalCropHeight > cropHeight) {
finalCropHeight = cropHeight
finalCropWidth = (cropHeight * effectiveAspectRatio).toInt()
}

// Center the crop rectangle within the image bounds
val cropViewRectLeft = (scaledWidth - finalCropWidth) / 2
val cropViewRectTop = (scaledHeight - finalCropHeight) / 2
val cropViewRectRight = cropViewRectLeft + finalCropWidth
val cropViewRectBottom = cropViewRectTop + finalCropHeight

// Ensure crop rectangle stays within image bounds
val cropLeft = max(cropViewRectLeft.toFloat() + imageOffset.x, imageOffset.x)
val cropTop = max(cropViewRectTop.toFloat() + imageOffset.y, imageOffset.y)
val cropRight = min(cropViewRectRight + imageOffset.x, imageOffset.x + scaledWidth)
val cropBottom = min(cropViewRectBottom + imageOffset.y, imageOffset.y + scaledHeight)

// Adjust for aspect ratio locking
var newOffsetX = cropLeft
var newOffsetY = cropTop

if (aspectRatioLocked) {
val deltaX = (cropRight - cropLeft) - finalCropWidth
val deltaY = (cropBottom - cropTop) - finalCropHeight

if (abs(deltaX) > abs(deltaY)) {
newOffsetX += (deltaX / 2).toFloat()
} else {
newOffsetY += (deltaY / 2).toFloat()
}
}

Log.d("BoundaryCheck", "Constrained Offset -> X: $newOffsetX, Y: $newOffsetY")
Log.d(
"BoundaryCheck",
"Final Crop -> Left: $cropLeft, Top: $cropTop, Right: $cropRight, Bottom: $cropBottom"
)

// Return the new offset ensuring the crop rectangle is fully inside the image rectangle
return Offset(newOffsetX, newOffsetY)
}
Мне нужно было найти, что мешает сразу вернуться в set go image to set

Подробнее здесь: https://stackoverflow.com/questions/792 ... age-editor
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»