Skip to content

Commit

Permalink
PAINTROID-418
Browse files Browse the repository at this point in the history
Made distinction between different display resolutions (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) to determine notfication/status bar height. Now when user clicks fullscreen, the bitmap is maxed out as good as possible. Sometimes the behaviour is not optimal, this mostly happens when bitmapheight > surfaceheight, then the scale is not always fully zoomed but I was not able to find a proper solution for this. Also fixed bug where the bitmap was not centered after exiting fullscreen mode, this happened due to the fact that the surfaceheight to determine the middle of the screen was still too small (actual screen size - top-/bottom bar).
  • Loading branch information
Electronix1337 committed Jul 1, 2022
1 parent 09a5666 commit d7fa5ed
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 15 deletions.
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

0 comments on commit d7fa5ed

Please sign in to comment.