Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAINTROID-418 Fullscreen mode should zoom to fit #1115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.webkit.MimeTypeMap
Expand Down Expand Up @@ -300,6 +301,7 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
)
)
}
workspace.perspective.setBitmapDimensions(layerModel.width, layerModel.height)
}
else -> {
val isFullscreen = savedInstanceState.getBoolean(IS_FULLSCREEN_KEY, false)
Expand Down Expand Up @@ -349,7 +351,10 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
R.id.pocketpaint_options_open_image -> presenterMain.loadImageClicked()
R.id.pocketpaint_options_new_image -> presenterMain.newImageClicked()
R.id.pocketpaint_options_discard_image -> presenterMain.discardImageClicked()
R.id.pocketpaint_options_fullscreen_mode -> presenterMain.enterFullscreenClicked()
R.id.pocketpaint_options_fullscreen_mode -> {
perspective.mainActivity = this
presenterMain.enterFullscreenClicked()
}
R.id.pocketpaint_options_rate_us -> presenterMain.rateUsClicked()
R.id.pocketpaint_options_help -> presenterMain.showHelpClicked()
R.id.pocketpaint_options_about -> presenterMain.showAboutClicked()
Expand Down Expand Up @@ -644,8 +649,12 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {

override fun enterFullscreen() {
drawingSurface.disableAutoScroll()
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
if (VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
}
}

override fun exitFullscreen() {
Expand Down
86 changes: 74 additions & 12 deletions Paintroid/src/main/java/org/catrobat/paintroid/ui/Perspective.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.graphics.Canvas
import android.graphics.PointF
import android.graphics.Rect
import androidx.annotation.VisibleForTesting
import org.catrobat.paintroid.MainActivity
import kotlin.jvm.Synchronized
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -72,22 +73,44 @@ open class Perspective(private var bitmapWidth: Int, private var bitmapHeight: I

val scaleForCenterBitmap: Float
get() {
val screenSizeRatio = surfaceWidth.toFloat() / surfaceHeight
val bitmapSizeRatio = bitmapWidth.toFloat() / bitmapHeight
var ratioDependentScale = if (screenSizeRatio > bitmapSizeRatio) {
surfaceHeight.toFloat() / bitmapHeight
var ratioDependentScale = 0f
if (fullscreen) {
val displayHeight: Int? = mainActivity?.resources?.displayMetrics?.heightPixels
var screenSizeRatio = 0f
if (displayHeight != null) {
screenSizeRatio = surfaceWidth.toFloat() / displayHeight.toFloat()
}
val bitmapSizeRatio = bitmapWidth.toFloat() / bitmapHeight
if (screenSizeRatio > bitmapSizeRatio) {
if (displayHeight != null) {
ratioDependentScale = displayHeight.toFloat() / bitmapHeight.toFloat()
}
} else {
ratioDependentScale = surfaceWidth.toFloat() / bitmapWidth.toFloat()
}
ratioDependentScale = min(ratioDependentScale, 1f)
ratioDependentScale = max(ratioDependentScale, MIN_SCALE)
} else {
surfaceWidth.toFloat() / bitmapWidth
val screenSizeRatio = surfaceWidth.toFloat() / surfaceHeight
val bitmapSizeRatio = bitmapWidth.toFloat() / bitmapHeight
ratioDependentScale = if (screenSizeRatio > bitmapSizeRatio) {
surfaceHeight.toFloat() / bitmapHeight.toFloat()
} else {
surfaceWidth.toFloat() / bitmapWidth.toFloat()
}
ratioDependentScale = min(ratioDependentScale, 1f)
ratioDependentScale = max(ratioDependentScale, MIN_SCALE)
}
ratioDependentScale = min(ratioDependentScale, 1f)
ratioDependentScale = max(ratioDependentScale, MIN_SCALE)
return ratioDependentScale
}

private var initialTranslationX = 0f
var oldHeight = 0f
var mainActivity: MainActivity? = null

@Synchronized
fun setSurfaceFrame(surfaceFrame: Rect) {
if (surfaceHeight == 0) oldHeight = surfaceFrame.bottom.toFloat()
surfaceFrame.apply {
surfaceWidth = right
surfaceCenterX = exactCenterX()
Expand All @@ -109,15 +132,54 @@ open class Perspective(private var bitmapWidth: Int, private var bitmapHeight: I
surfaceTranslationX = 0f
surfaceTranslationY = 0f
} else {
surfaceTranslationX = surfaceWidth / 2f - bitmapWidth / 2f
initialTranslationX = surfaceTranslationX
surfaceTranslationY = surfaceHeight / 2f - bitmapHeight / 2f
initialTranslationY = surfaceTranslationY
if (fullscreen) {
val displayHeight: Int? = mainActivity?.resources?.displayMetrics?.heightPixels
surfaceTranslationX = surfaceWidth / 2f - bitmapWidth / 2f
initialTranslationX = surfaceTranslationX
if (displayHeight != null) {
surfaceTranslationY = displayHeight.toFloat() / 2f - bitmapHeight / 2f
} else {
surfaceTranslationY = bitmapHeight.toFloat() / 2f - bitmapHeight / 2f
}
initialTranslationY = surfaceTranslationY
} else {
surfaceTranslationX = surfaceWidth / 2f - bitmapWidth / 2f
initialTranslationX = surfaceTranslationX
if (oldHeight > 0) surfaceHeight = oldHeight.toInt()
surfaceTranslationY = surfaceHeight / 2f - bitmapHeight / 2f
initialTranslationY = surfaceTranslationY
}
}
val zoomFactor = if (fullscreen) {
calculateZoomFactor()
} else {
BORDER_ZOOM_FACTOR
}
val zoomFactor = if (fullscreen) 1.0f else BORDER_ZOOM_FACTOR
surfaceScale = scaleForCenterBitmap * zoomFactor
}

@Synchronized
fun calculateZoomFactor(): Float {
val displayHeight: Int? = mainActivity?.resources?.displayMetrics?.heightPixels
if (bitmapHeight > bitmapWidth) {
if (bitmapHeight > surfaceHeight) {
return 1.0f
} else {
if (displayHeight != null) {
return displayHeight.toFloat() / bitmapHeight.toFloat()
} else {
return surfaceHeight.toFloat() / bitmapHeight.toFloat()
}
}
} else {
if (bitmapWidth >= surfaceWidth) {
return 1.0f
} else {
return surfaceWidth.toFloat() / bitmapWidth.toFloat()
}
}
}

@Synchronized
fun multiplyScale(factor: Float) {
scale = surfaceScale * factor
Expand Down