changeset 5:247e03baf77c

Fix rotation.
author David Barts <n5jrn@me.com>
date Wed, 10 Feb 2021 16:31:01 -0800
parents b6a217c850fb
children e8059b166de1
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt
diffstat 2 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Tue Feb 09 08:58:17 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Wed Feb 10 16:31:01 2021 -0800
@@ -17,11 +17,12 @@
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.widget.PopupMenu
 import com.bartsent.simpleresizer.databinding.ActivityEditImageBinding
+import java.io.File
 import java.io.IOException
 import kotlin.math.roundToInt
 
 class EditImage : AppCompatActivity() {
-    object State {
+    private object State {
         var uri: Uri? = null
         var bitmap: Bitmap? = null
     }
@@ -205,14 +206,15 @@
 
     fun doRotate(deg: Int): Unit {
         val oldBitmap = State.bitmap!!
-        val newBitmap = when (deg) {
-            90, 270 -> Bitmap.createBitmap(oldBitmap.height, oldBitmap.width, oldBitmap.config)
-            180 -> Bitmap.createBitmap(oldBitmap.width, oldBitmap.height, oldBitmap.config)
-            else -> throw IllegalArgumentException("deg must be 90, 180, or 270")
+        if (deg % 90 != 0) {
+            throw IllegalArgumentException("$deg not a multiple of 90")
         }
+        val (w, h) = if (deg % 180 == 0) Pair(oldBitmap.width, oldBitmap.height) else Pair(oldBitmap.height, oldBitmap.width)
+        val newBitmap = Bitmap.createBitmap(w, h, oldBitmap.config)
         copyColorSpace(oldBitmap, newBitmap)
         val rotater = Matrix().apply {
             setRotate(deg.toFloat(), oldBitmap.width.toFloat()/2.0f, oldBitmap.height.toFloat()/2.0f)
+            postTranslate((w - oldBitmap.width).toFloat()/2.0f, (h - oldBitmap.height).toFloat()/2.0f)
         }
         Canvas(newBitmap).run {
             drawBitmap(oldBitmap, rotater, null)
@@ -245,7 +247,8 @@
             put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
             put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
             if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
-                put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
+                put(MediaStore.MediaColumns.RELATIVE_PATH,
+                    File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).getPath())
             }
         }
         try {
@@ -266,5 +269,6 @@
         } catch (ioe: IOException) {
             showError(ioe.message ?: getString(R.string.error_io))
         }
+        finish()
     }
 }
--- a/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Tue Feb 09 08:58:17 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Wed Feb 10 16:31:01 2021 -0800
@@ -41,7 +41,7 @@
                     startActivity(this)
                 }
             } else {
-                Toast.makeText(applicationContext, "Unable to get image!", Toast.LENGTH_LONG)
+                Toast.makeText(applicationContext, "Unable to get image!", Toast.LENGTH_LONG).show()
             }
         }
     }