Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
changeset 15:20da616dcda0
Add preferences.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 18 Feb 2021 22:12:19 -0800 |
parents | 73beb7c973ae |
children | 3ed74dc0e34a |
files | app/src/main/AndroidManifest.xml app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt app/src/main/java/com/bartsent/simpleresizer/SettingsActivity.kt app/src/main/java/com/bartsent/simpleresizer/lib/getScaledInstance.kt app/src/main/res/layout/settings_activity.xml app/src/main/res/menu/menu_edit.xml app/src/main/res/values/arrays.xml app/src/main/res/values/strings.xml app/src/main/res/xml/root_preferences.xml |
diffstat | 10 files changed, 148 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- 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"> + + <activity android:name=".SettingsActivity" android:label="@string/title_activity_settings"> + <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".EditImage"/> + <intent-filter> + <action android:name="android.intent.action.APPLICATION_PREFERENCES" /> + <category android:name="android.intent.category.PREFERENCE" /> + </intent-filter> + </activity> + <activity android:name=".EditImage"> <intent-filter> <action android:name="android.intent.action.SEND" /> @@ -16,12 +25,14 @@ <data android:mimeType="image/*" /> </intent-filter> </activity> + <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> + </application> </manifest> \ No newline at end of file
--- 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)) } }
--- 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() {
--- /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
--- 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)
--- /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 @@ +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <FrameLayout + android:id="@+id/settings" + android:layout_width="match_parent" + android:layout_height="match_parent" /> +</LinearLayout> \ No newline at end of file
--- /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 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <item + android:id="@+id/settings_item" + android:icon="@android:drawable/ic_menu_preferences" + android:title="@string/settings_title" + app:showAsAction="ifRoom" /> + +</menu> \ No newline at end of file
--- /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 @@ +<resources> + <!-- Scale Type Preference --> + <string-array name="scale_type_titles"> + <item>Optimize for Quality</item> + <item>Optimize for Speed</item> + </string-array> + <string-array name="scale_types"> + <item>quality</item> + <item>speed</item> + </string-array> +</resources> \ No newline at end of file
--- 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 @@ <string name="r_90_ccw">90˚ CCW</string> <string name="r_90_cw">90˚ CW</string> <string name="rotate_text">Rotate</string> - <string name="scale_text">Scale</string> + <string name="scale_text">Resize</string> <string name="selecting_message">Selecting image…</string> + <string name="title_activity_settings">Settings</string> + + <!-- Preference Titles --> + <string name="scale_type_title">Scale Type</string> + <string name="jpeg_quality_title">JPEG Output Quality</string> + <string name="settings_title">Settings</string> </resources> \ No newline at end of file
--- /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 @@ +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <ListPreference + android:id="@+id/scale_type" + android:defaultValue="speed" + android:key="scale_type" + android:title="@string/scale_type_title" + app:entries="@array/scale_type_titles" + app:entryValues="@array/scale_types" + app:title="@string/scale_type_title" + app:useSimpleSummaryProvider="true" /> + + <SeekBarPreference + android:id="@+id/jpeg_quality" + android:defaultValue="85" + android:key="jpeg_quality" + android:max="100" + android:title="@string/jpeg_quality_title" + app:defaultValue="85" + app:min="0" + app:showSeekBarValue="true" + app:title="@string/jpeg_quality_title" /> + +</PreferenceScreen> \ No newline at end of file