From 23c786d3b968c0a75faee78296a7caa93e076c7c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 26 Jan 2019 16:58:02 +0100 Subject: [PATCH] allow increasing text editor font size with gestures --- app/build.gradle | 1 + .../pro/activities/MainActivity.kt | 2 +- .../filemanager/pro/views/GestureTextView.kt | 72 +++++++++++++++++++ .../main/res/layout/activity_read_text.xml | 7 +- 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/pro/views/GestureTextView.kt diff --git a/app/build.gradle b/app/build.gradle index 65d4a59c..b2a435dd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -53,4 +53,5 @@ android { dependencies { implementation 'com.simplemobiletools:commons:5.6.17' implementation 'com.github.Stericson:RootTools:df729dcb13' + implementation 'com.alexvasilkov:gesture-views:2.5.2' } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt index 31cf1b4c..84fe2d33 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt @@ -248,7 +248,7 @@ class MainActivity : SimpleActivity() { } private fun launchAbout() { - val licenses = LICENSE_GLIDE or LICENSE_PATTERN or LICENSE_REPRINT + val licenses = LICENSE_GLIDE or LICENSE_PATTERN or LICENSE_REPRINT or LICENSE_GESTURE_VIEWS val faqItems = arrayListOf( FAQItem(R.string.faq_3_title_commons, R.string.faq_3_text_commons), diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/views/GestureTextView.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/views/GestureTextView.kt new file mode 100644 index 00000000..0baea957 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/views/GestureTextView.kt @@ -0,0 +1,72 @@ +package com.simplemobiletools.filemanager.pro.views + +import android.content.Context +import android.util.AttributeSet +import android.util.TypedValue +import android.view.MotionEvent +import androidx.appcompat.widget.AppCompatTextView +import com.alexvasilkov.gestures.GestureController +import com.alexvasilkov.gestures.State +import com.alexvasilkov.gestures.views.interfaces.GestureView +import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor +import com.simplemobiletools.filemanager.pro.extensions.config + +// taken from +// https://github.com/alexvasilkov/GestureViews/blob/f0a4c266e31dcad23bd0d9013531bc1c501b9c9f/sample/src/main/java/com/alexvasilkov/gestures/sample/ex/custom/text/GestureTextView.java +class GestureTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : AppCompatTextView(context, attrs, defStyle), GestureView { + private val controller: GestureController = GestureController(this) + private var origSize = 0f + private var size = 0f + + init { + controller.settings.setOverzoomFactor(1f).isPanEnabled = false + controller.settings.initFromAttributes(context, attrs) + controller.addOnStateChangeListener(object : GestureController.OnStateChangeListener { + override fun onStateChanged(state: State) { + applyState(state) + } + + override fun onStateReset(oldState: State, newState: State) { + applyState(newState) + } + }) + + origSize = textSize + setTextColor(context.config.textColor) + setLinkTextColor(context.getAdjustedPrimaryColor()) + } + + override fun getController() = controller + + override fun onTouchEvent(event: MotionEvent) = controller.onTouch(this, event) + + override fun setTextSize(size: Float) { + super.setTextSize(size) + origSize = textSize + applyState(controller.state) + } + + override fun setTextSize(unit: Int, size: Float) { + super.setTextSize(unit, size) + origSize = textSize + applyState(controller.state) + } + + override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) { + super.onSizeChanged(width, height, oldWidth, oldHeight) + controller.settings.setViewport(width, height).setImage(width, height) + controller.updateState() + } + + private fun applyState(state: State) { + var size = origSize * state.zoom + val maxSize = origSize * controller.stateController.getMaxZoom(state) + size = Math.max(origSize, Math.min(size, maxSize)) + + size = Math.round(size).toFloat() + if (!State.equals(this.size, size)) { + this.size = size + super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size) + } + } +} diff --git a/app/src/main/res/layout/activity_read_text.xml b/app/src/main/res/layout/activity_read_text.xml index 8f53bdde..e0c12810 100644 --- a/app/src/main/res/layout/activity_read_text.xml +++ b/app/src/main/res/layout/activity_read_text.xml @@ -3,15 +3,16 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/read_text_holder" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="match_parent" + android:fillViewport="true"> -