ImageViwer: retrieve screen size with Resources.getSystem().displayMetrics

This commit is contained in:
Matéo Duparc 2022-01-23 12:56:37 +01:00
parent 822aba9481
commit f546e64c34
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
1 changed files with 8 additions and 25 deletions

View File

@ -1,17 +1,14 @@
package sushi.hardcore.droidfs.file_viewers package sushi.hardcore.droidfs.file_viewers
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Matrix import android.graphics.Matrix
import android.graphics.Point
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Handler import android.os.Handler
import android.util.Size
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.WindowInsets
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -155,20 +152,6 @@ class ImageViewer: FileViewerActivity() {
handler.postDelayed(hideUI, hideDelay) handler.postDelayed(hideUI, hideDelay)
} }
private fun getDisplaySize(): Size {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val insets = windowManager.currentWindowMetrics.windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout()
)
Size(insets.right + insets.left, insets.top + insets.bottom)
} else {
val point = Point()
@Suppress("Deprecation")
windowManager.defaultDisplay.getSize(point)
Size(point.x, point.y)
}
}
private fun loadImage(){ private fun loadImage(){
bitmap = null bitmap = null
requestBuilder = null requestBuilder = null
@ -180,16 +163,16 @@ class ImageViewer: FileViewerActivity() {
if (bitmap == null) { if (bitmap == null) {
true true
} else { } else {
val displaySize = getDisplaySize() val displayMetrics = Resources.getSystem().displayMetrics
if (displaySize.width < bitmap!!.width || displaySize.height < bitmap!!.height) { if (displayMetrics.widthPixels < bitmap!!.width || displayMetrics.heightPixels < bitmap!!.height) {
val newWidth: Int val newWidth: Int
val newHeight: Int val newHeight: Int
if (displaySize.width > displaySize.height) { if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
newWidth = displaySize.width newWidth = displayMetrics.widthPixels
newHeight = bitmap!!.height*displaySize.width/bitmap!!.width newHeight = bitmap!!.height*displayMetrics.widthPixels/bitmap!!.width
} else { } else {
newHeight = displaySize.height newHeight = displayMetrics.heightPixels
newWidth = bitmap!!.width*displaySize.height/bitmap!!.height newWidth = bitmap!!.width*displayMetrics.heightPixels/bitmap!!.height
} }
bitmap = Bitmap.createScaledBitmap(bitmap!!, newWidth, newHeight, false) bitmap = Bitmap.createScaledBitmap(bitmap!!, newWidth, newHeight, false)
} }