changeset 10:5626557ac542 memo

Add progress bar and a worker thread. Still way slow.
author David Barts <n5jrn@me.com>
date Wed, 17 Feb 2021 13:39:50 -0800
parents 6ae738b8a814
children 678adef4774f
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/res/layout/activity_edit_image.xml
diffstat 2 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Wed Feb 17 07:43:50 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Wed Feb 17 13:39:50 2021 -0800
@@ -13,6 +13,7 @@
 import android.view.MenuItem
 import android.view.View
 import android.widget.EditText
+import android.widget.ProgressBar
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.widget.PopupMenu
@@ -20,6 +21,7 @@
 import com.bartsent.simpleresizer.lib.getScaledInstance
 import java.io.File
 import java.io.IOException
+import kotlin.concurrent.thread
 import kotlin.math.roundToInt
 
 class EditImage : AppCompatActivity() {
@@ -142,9 +144,16 @@
         }
         val newBitmap = Bitmap.createBitmap((oldBitmap.width * factor).roundToInt(), (oldBitmap.height * factor).roundToInt(), oldBitmap.config)
         copyColorSpace(oldBitmap, newBitmap)
-        setImage(oldBitmap.getScaledInstance(
-                (oldBitmap.width.toDouble() * factor + 0.5).toInt(),
-                (oldBitmap.height.toDouble() * factor + 0.5).toInt()))
+        binding.progressBar.visibility = ProgressBar.VISIBLE
+        thread {
+            val newBitmap = oldBitmap.getScaledInstance(
+                    (oldBitmap.width.toDouble() * factor + 0.5).toInt(),
+                    (oldBitmap.height.toDouble() * factor + 0.5).toInt())
+            runOnUiThread {
+                binding.progressBar.visibility = ProgressBar.INVISIBLE
+                setImage(newBitmap)
+            }
+        }
     }
 
     // is there any way to remember the last scale value?
@@ -215,10 +224,16 @@
             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)
+        binding.progressBar.visibility = ProgressBar.VISIBLE
+        thread {
+            Canvas(newBitmap).run {
+                drawBitmap(oldBitmap, rotater, null)
+            }
+            runOnUiThread {
+                binding.progressBar.visibility = ProgressBar.INVISIBLE
+                setImage(newBitmap)
+            }
         }
-        setImage(newBitmap)
     }
 
      fun cancelClicked(view: View): Unit {
--- a/app/src/main/res/layout/activity_edit_image.xml	Wed Feb 17 07:43:50 2021 -0800
+++ b/app/src/main/res/layout/activity_edit_image.xml	Wed Feb 17 13:39:50 2021 -0800
@@ -72,4 +72,15 @@
         app:layout_constraintStart_toEndOf="@id/cancel_button"
         app:layout_constraintTop_toBottomOf="@id/rotate_button" />
 
+    <ProgressBar
+        android:id="@+id/progress_bar"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:indeterminate="true"
+        android:visibility="invisible"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file