Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
diff app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 18:eedf995462d9 concur2
Parallalized, but ConstraintLayout started hosing the edit window.
author | David Barts <n5jrn@me.com> |
---|---|
date | Mon, 22 Feb 2021 07:04:31 -0800 |
parents | 3ed74dc0e34a |
children | 7e7e71724770 |
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Sun Feb 21 22:14:07 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Mon Feb 22 07:04:31 2021 -0800 @@ -28,6 +28,7 @@ import java.io.IOException import kotlin.concurrent.thread import androidx.preference.PreferenceManager +import com.bartsent.simpleresizer.lib.ThreadPools class EditImage : AppCompatActivity() { private object State { @@ -108,21 +109,27 @@ // Being stateful stops data loss when the phone gets rotated. if (imageUri != State.uri) { State.uri = imageUri - State.bitmap = contentResolver.openInputStream(imageUri).use { - BitmapFactory.decodeStream(it) + binding.progressBar.visibility = ProgressBar.VISIBLE + ThreadPools.WORKERS.execute { + State.bitmap = contentResolver.openInputStream(imageUri).use { + BitmapFactory.decodeStream(it) + } + runOnUiThread { + binding.progressBar.visibility = ProgressBar.INVISIBLE + if (State.bitmap == null) + showFatalError(getString(R.string.error_bad_image)) + else + setImage(State.bitmap!!) + } } } - if (State.bitmap == null) { - showFatalError(getString(R.string.error_bad_image)) - return - } - setImage(State.bitmap!!) } - fun setImage(image: Bitmap): Unit { + private fun setImage(image: Bitmap): Unit { binding.imageSize.text = getString(R.string.image_size_text, image.width, image.height) binding.image.setImageBitmap(image) State.bitmap = image + binding.root.invalidate() } private val CUSTOM = 999998 @@ -147,7 +154,7 @@ } } - fun scaleMenuItemClicked(item: MenuItem) : Boolean = + private fun scaleMenuItemClicked(item: MenuItem) : Boolean = when (item.itemId) { CUSTOM -> { showCustomScaleDialog(); true } CANCEL -> true @@ -163,7 +170,7 @@ } } - fun doScale(newMax: Int): Unit { + private fun doScale(newMax: Int): Unit { val oldBitmap = State.bitmap!! val factor = newMax.toDouble() / maxOf(oldBitmap.width, oldBitmap.height).toDouble() if (factor >= 1.0) { @@ -173,7 +180,7 @@ "scale_type", "speed" ) Log.d("EditImage", "scaling, scale_type = $scaleType") binding.progressBar.visibility = ProgressBar.VISIBLE - thread { + ThreadPools.WORKERS.execute { val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt() val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt() val newBitmap = if (scaleType == "quality") @@ -187,7 +194,7 @@ } } - fun showCustomScaleDialog(): Unit { + private fun showCustomScaleDialog(): Unit { val image = State.bitmap!! val curMaxDim = maxOf(image.width, image.height) val dialogView = layoutInflater.inflate(R.layout.dialog_custom_scale, null) @@ -234,7 +241,7 @@ } } - fun doRotate(deg: Int): Unit { + private fun doRotate(deg: Int): Unit { val oldBitmap = State.bitmap!! if (deg % 90 != 0) { throw IllegalArgumentException("$deg not a multiple of 90") @@ -245,7 +252,7 @@ postTranslate((w - oldBitmap.width).toFloat()/2.0f, (h - oldBitmap.height).toFloat()/2.0f) } binding.progressBar.visibility = ProgressBar.VISIBLE - thread { + ThreadPools.WORKERS.execute { val newBitmap = Bitmap.createBitmap(w, h, oldBitmap.config) copyColorSpace(oldBitmap, newBitmap) Canvas(newBitmap).run { @@ -287,27 +294,37 @@ File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).getPath()) } } - try { - val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) - if (myUri == null) { - throw IOException(getString(R.string.error_create_mediastore)) - } - val stream = contentResolver.openOutputStream(myUri) - if (stream == null) { - throw IOException(getString(R.string.error_get_output)) + binding.progressBar.visibility = ProgressBar.VISIBLE + ThreadPools.WORKERS.execute { + var errorMessage: String? = null + try { + val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) + if (myUri == null) { + throw IOException(getString(R.string.error_create_mediastore)) + } + val stream = contentResolver.openOutputStream(myUri) + if (stream == null) { + throw IOException(getString(R.string.error_get_output)) + } + val quality = maxOf(0, minOf(100, + PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt( + "jpeg_quality", 85))) + Log.d("EditImage", "saving, jpeg_quality = $quality") + stream.use { + if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) { + throw IOException(getString(R.string.error_save_bitmap)) + } + } + } catch (ioe: IOException) { + errorMessage = ioe.message ?: getString(R.string.error_io) } - val quality = maxOf(0, minOf(100, - PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt( - "jpeg_quality", 85))) - Log.d("EditImage", "saving, jpeg_quality = $quality") - stream.use { - if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) { - throw IOException(getString(R.string.error_save_bitmap)) - } + runOnUiThread { + binding.progressBar.visibility = ProgressBar.INVISIBLE + if (errorMessage == null) + finish() + else + Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show() } - } catch (ioe: IOException) { - Toast.makeText(applicationContext, ioe.message ?: getString(R.string.error_io), Toast.LENGTH_LONG).show() } - finish() } }