From b9d5c004e0cd894b8ccf4688620275b94567d609 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 11 Dec 2016 21:22:07 +0100 Subject: [PATCH] allow changing grid column count by pinching --- .../gallery/activities/MediaActivity.kt | 19 ++++++ .../gallery/views/MyScalableRecyclerView.kt | 58 +++++++++++++++++++ app/src/main/res/layout/activity_media.xml | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/views/MyScalableRecyclerView.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 3cbd20d17..36ef96b18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.graphics.Bitmap import android.net.Uri import android.os.Bundle +import android.support.v7.widget.GridLayoutManager import android.util.Log import android.view.Menu import android.view.MenuItem @@ -20,6 +21,7 @@ import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.extensions.getHumanizedFilename import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.views.MyScalableRecyclerView import kotlinx.android.synthetic.main.activity_media.* import java.io.File import java.io.IOException @@ -54,6 +56,23 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mShowAll = mConfig.showAll if (mShowAll) supportActionBar?.setDisplayHomeAsUpEnabled(false) + + val layoutManager = media_grid.layoutManager as GridLayoutManager + MyScalableRecyclerView.mListener = object : MyScalableRecyclerView.ZoomListener { + override fun zoomIn() { + if (layoutManager.spanCount > 1) { + layoutManager.spanCount-- + MediaAdapter.actMode?.finish() + } + } + + override fun zoomOut() { + if (layoutManager.spanCount < 10) { + layoutManager.spanCount++ + MediaAdapter.actMode?.finish() + } + } + } } override fun onResume() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyScalableRecyclerView.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyScalableRecyclerView.kt new file mode 100644 index 000000000..c69e2930d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/views/MyScalableRecyclerView.kt @@ -0,0 +1,58 @@ +package com.simplemobiletools.gallery.views + +import android.content.Context +import android.support.v7.widget.RecyclerView +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.ScaleGestureDetector + +class MyScalableRecyclerView : RecyclerView { + var mScaleDetector: ScaleGestureDetector + + companion object { + var mListener: ZoomListener? = null + var mCurrScaleFactor = 1.0f + } + + constructor(context: Context) : super(context) { + } + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { + } + + init { + mScaleDetector = ScaleGestureDetector(context, GestureListener()) + } + + override fun dispatchTouchEvent(ev: MotionEvent): Boolean { + super.dispatchTouchEvent(ev) + if (ev.action == MotionEvent.ACTION_UP) { + mCurrScaleFactor = 1.0f + } + + return mScaleDetector.onTouchEvent(ev) + } + + class GestureListener : ScaleGestureDetector.SimpleOnScaleGestureListener() { + val ZOOM_IN_THRESHOLD = -0.7f + val ZOOM_OUT_THRESHOLD = 0.3f + + override fun onScale(detector: ScaleGestureDetector): Boolean { + val diff = mCurrScaleFactor - detector.scaleFactor + if (diff < ZOOM_IN_THRESHOLD && mCurrScaleFactor == 1.0f) { + mListener?.zoomIn() + mCurrScaleFactor = detector.scaleFactor + } else if (diff > ZOOM_OUT_THRESHOLD && mCurrScaleFactor == 1.0f) { + mListener?.zoomOut() + mCurrScaleFactor = detector.scaleFactor + } + return false + } + } + + interface ZoomListener { + fun zoomOut() + + fun zoomIn() + } +} diff --git a/app/src/main/res/layout/activity_media.xml b/app/src/main/res/layout/activity_media.xml index 10a646e64..a5de62425 100644 --- a/app/src/main/res/layout/activity_media.xml +++ b/app/src/main/res/layout/activity_media.xml @@ -6,7 +6,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> -