# HG changeset patch # User David Barts # Date 1613715139 28800 # Node ID 20da616dcda08b62a47cc540e571099d9ca0c24f # Parent 73beb7c973ae060317a238810ddf6ef13dd8092e Add preferences. diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/AndroidManifest.xml --- a/app/src/main/AndroidManifest.xml Thu Feb 18 15:19:07 2021 -0800 +++ b/app/src/main/AndroidManifest.xml Thu Feb 18 22:12:19 2021 -0800 @@ -9,6 +9,15 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.SimpleResizer"> + + + + + + + + + @@ -16,12 +25,14 @@ + + \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/java/com/bartsent/simpleresizer/EditImage.kt --- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Thu Feb 18 15:19:07 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Thu Feb 18 22:12:19 2021 -0800 @@ -1,6 +1,8 @@ package com.bartsent.simpleresizer +import android.annotation.SuppressLint import android.content.ContentValues +import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Canvas @@ -10,6 +12,8 @@ import android.os.Environment import android.provider.MediaStore import android.provider.OpenableColumns +import android.util.Log +import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.EditText @@ -22,7 +26,7 @@ import java.io.File import java.io.IOException import kotlin.concurrent.thread -import kotlin.math.roundToInt +import androidx.preference.PreferenceManager class EditImage : AppCompatActivity() { private object State { @@ -40,6 +44,22 @@ super.onCreate(savedInstanceState) binding = ActivityEditImageBinding.inflate(layoutInflater) setContentView(binding.root) + PreferenceManager.setDefaultValues(applicationContext, R.xml.root_preferences, false) + } + + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.menu_edit, menu) + return super.onCreateOptionsMenu(menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == R.id.settings_item) { + startActivity( + Intent(Intent.ACTION_APPLICATION_PREFERENCES, null, this, + SettingsActivity::class.java)) + return true + } + return false } // Cribbed from: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content @@ -142,11 +162,17 @@ if (factor >= 1.0) { throw IllegalArgumentException("can only scale down") } + val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString( + "scale_type", "speed" ) + Log.d("EditImage", "scaling, scale_type = $scaleType") binding.progressBar.visibility = ProgressBar.VISIBLE thread { - val newBitmap = oldBitmap.getScaledInstance( - (oldBitmap.width.toDouble() * factor + 0.5).toInt(), - (oldBitmap.height.toDouble() * factor + 0.5).toInt()) + val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt() + val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt() + val newBitmap = if (scaleType == "quality") + oldBitmap.getScaledInstance(newWidth, newHeight) + else + Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true) runOnUiThread { binding.progressBar.visibility = ProgressBar.INVISIBLE setImage(newBitmap) @@ -272,9 +298,12 @@ if (stream == null) { throw IOException(getString(R.string.error_get_output)) } - // fixme: use quality from settings + 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, 85, it)) { + if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) { throw IOException(getString(R.string.error_save_bitmap)) } } diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt --- a/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt Thu Feb 18 15:19:07 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt Thu Feb 18 22:12:19 2021 -0800 @@ -1,10 +1,13 @@ package com.bartsent.simpleresizer +import android.annotation.SuppressLint import android.app.Activity import android.content.Intent import android.os.Bundle +import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.preference.PreferenceManager import com.bartsent.simpleresizer.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/java/com/bartsent/simpleresizer/SettingsActivity.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/src/main/java/com/bartsent/simpleresizer/SettingsActivity.kt Thu Feb 18 22:12:19 2021 -0800 @@ -0,0 +1,28 @@ +package com.bartsent.simpleresizer + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.preference.PreferenceFragmentCompat +import androidx.preference.PreferenceManager + +class SettingsActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.settings_activity) + if (savedInstanceState == null) { + supportFragmentManager + .beginTransaction() + .replace(R.id.settings, SettingsFragment()) + .commit() + } + supportActionBar?.setDisplayHomeAsUpEnabled(true) + PreferenceManager.setDefaultValues(applicationContext, R.xml.root_preferences, false) + } + + class SettingsFragment : PreferenceFragmentCompat() { + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.root_preferences, rootKey) + } + } +} \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt --- a/app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt Thu Feb 18 15:19:07 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt Thu Feb 18 22:12:19 2021 -0800 @@ -1,21 +1,23 @@ package com.bartsent.simpleresizer.lib import android.graphics.Bitmap -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Matrix -import kotlin.math.ceil -import kotlin.math.floor private data class IndexWeight(var index: Int, var weight: Double) +/** + * A quality scaler, rather simpler than Image.getScaledInstance in that it has only + * one (slow, high-quality) option. + * @param newWidth Width of new bitmap + * @param newHeight Height of new bitmap + * @return New bitmap + */ fun Bitmap.getScaledInstance(newWidth: Int, newHeight: Int): Bitmap { if (newWidth <= 0) throw IllegalArgumentException("invalid width: $newWidth") if (newHeight <= 0) throw IllegalArgumentException("invalid height: $newHeight") if (width == newWidth && height == newHeight) - return Bitmap.createBitmap(this) + return this return if (width != newWidth) { Resizer.fromBitmap(this).horizontal(newWidth).let { if (height == newHeight) it else it.vertical(newHeight) diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/res/layout/settings_activity.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/src/main/res/layout/settings_activity.xml Thu Feb 18 22:12:19 2021 -0800 @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/res/menu/menu_edit.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/src/main/res/menu/menu_edit.xml Thu Feb 18 22:12:19 2021 -0800 @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/res/values/arrays.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/src/main/res/values/arrays.xml Thu Feb 18 22:12:19 2021 -0800 @@ -0,0 +1,11 @@ + + + + Optimize for Quality + Optimize for Speed + + + quality + speed + + \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/res/values/strings.xml --- a/app/src/main/res/values/strings.xml Thu Feb 18 15:19:07 2021 -0800 +++ b/app/src/main/res/values/strings.xml Thu Feb 18 22:12:19 2021 -0800 @@ -17,6 +17,12 @@ 90˚ CCW 90˚ CW Rotate - Scale + Resize Selecting image… + Settings + + + Scale Type + JPEG Output Quality + Settings \ No newline at end of file diff -r 73beb7c973ae -r 20da616dcda0 app/src/main/res/xml/root_preferences.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/src/main/res/xml/root_preferences.xml Thu Feb 18 22:12:19 2021 -0800 @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file