prepare for rotating images in viewpager activity

This commit is contained in:
tibbi 2017-02-13 20:51:28 +01:00
parent 592c80d951
commit 83a47e9b8c
3 changed files with 74 additions and 21 deletions

View file

@ -5,6 +5,8 @@ import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.PagerAdapter
import android.util.SparseArray
import android.view.ViewGroup
import com.simplemobiletools.gallery.activities.ViewPagerActivity
import com.simplemobiletools.gallery.fragments.PhotoFragment
import com.simplemobiletools.gallery.fragments.VideoFragment
@ -13,6 +15,7 @@ import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.models.Medium
class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val media: MutableList<Medium>) : FragmentStatePagerAdapter(fm) {
private val mFragments = SparseArray<ViewPagerFragment>()
override fun getCount() = media.size
override fun getItem(position: Int): Fragment {
@ -33,4 +36,17 @@ class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val m
}
override fun getItemPosition(item: Any?) = PagerAdapter.POSITION_NONE
override fun instantiateItem(container: ViewGroup?, position: Int): Any {
val fragment = super.instantiateItem(container, position) as ViewPagerFragment
mFragments.put(position, fragment)
return fragment
}
override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
mFragments.remove(position)
super.destroyItem(container, position, `object`)
}
fun getCurrentFragment(position: Int) = mFragments.get(position)
}

View file

@ -16,12 +16,16 @@ import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisible
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.getRealPathFromURI
import com.simplemobiletools.gallery.helpers.GlideRotateTransformation
import com.simplemobiletools.gallery.helpers.MEDIUM
import com.simplemobiletools.gallery.models.Medium
import it.sephiroth.android.library.exif2.ExifInterface
import kotlinx.android.synthetic.main.pager_photo_item.*
import kotlinx.android.synthetic.main.pager_photo_item.view.*
import uk.co.senab.photoview.PhotoView
import uk.co.senab.photoview.PhotoViewAttacher
@ -86,7 +90,7 @@ class PhotoFragment : ViewPagerFragment() {
}
})
}
loadImage(medium)
loadImage()
activity.window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
listener?.systemUiVisibilityChanged(visibility)
@ -115,7 +119,7 @@ class PhotoFragment : ViewPagerFragment() {
}
}
private fun loadImage(medium: Medium) {
private fun loadImage() {
if (medium.isGif()) {
Glide.with(this)
.load(medium.path)
@ -123,9 +127,15 @@ class PhotoFragment : ViewPagerFragment() {
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(glideView)
} else {
loadBitmap()
}
}
private fun loadBitmap(degrees: Float = 0f) {
Glide.with(this)
.load(medium.path)
.asBitmap()
.transform(GlideRotateTransformation(context, degrees))
.format(if (medium.isPng()) DecodeFormat.PREFER_ARGB_8888 else DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.listener(object : RequestListener<String, Bitmap> {
@ -134,17 +144,23 @@ class PhotoFragment : ViewPagerFragment() {
}
override fun onResourceReady(bitmap: Bitmap?, model: String?, target: Target<Bitmap>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
if (degrees == 0f)
addZoomableView()
return false
}
}).into(glideView)
}
fun rotateImageViewBy(degrees: Float) {
loadBitmap(degrees)
photo_view.beVisible()
subsampling_view.beGone()
}
private fun addZoomableView() {
if (!medium.isPng()) {
subsamplingView.apply {
visibility = View.VISIBLE
beVisible()
setDoubleTapZoomScale(1.2f)
setImage(ImageSource.uri(medium.path))
orientation = SubsamplingScaleImageView.ORIENTATION_USE_EXIF
@ -154,7 +170,7 @@ class PhotoFragment : ViewPagerFragment() {
}
override fun onReady() {
glideView.visibility = View.GONE
glideView.beGone()
glideView.setImageBitmap(null)
}
@ -165,7 +181,7 @@ class PhotoFragment : ViewPagerFragment() {
}
override fun onImageLoadError(p0: Exception?) {
visibility = View.GONE
beGone()
}
override fun onPreviewLoadError(p0: Exception?) {
@ -177,7 +193,7 @@ class PhotoFragment : ViewPagerFragment() {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
loadImage(medium)
loadImage()
}
private fun photoClicked() {

View file

@ -0,0 +1,21 @@
package com.simplemobiletools.gallery.helpers
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Matrix
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
class GlideRotateTransformation(val context: Context, val rotateRotationAngle: Float) : BitmapTransformation(context) {
override fun transform(pool: BitmapPool, bitmap: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
if (rotateRotationAngle % 360 == 0f)
return bitmap
val matrix = Matrix()
matrix.postRotate(rotateRotationAngle)
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
override fun getId() = "GlideRotateTransformation $rotateRotationAngle"
}