From 1cbee7747e6c7df56b1ab493297deec6763dd29c Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 26 Oct 2017 14:15:47 -0400 Subject: Fix/pip rotation (#450) * Attempt to redraw video on orientation change * Delete file backup * Update video bounds after rotations * Update changelog --- .../pitchedapps/frost/activities/MainActivity.kt | 6 ++++ .../com/pitchedapps/frost/views/FrostVideoView.kt | 42 +++++++++++++++------- .../pitchedapps/frost/views/FrostVideoViewer.kt | 10 ++++++ app/src/main/res/xml/frost_changelog.xml | 9 +++-- 4 files changed, 53 insertions(+), 14 deletions(-) (limited to 'app') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt index e37c38e9..b347d6a0 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -5,6 +5,7 @@ import android.app.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.content.res.Configuration import android.graphics.PointF import android.graphics.drawable.ColorDrawable import android.net.Uri @@ -495,4 +496,9 @@ class MainActivity : BaseActivity(), L.d("Video view released") videoViewer = null } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + videoViewer?.updateLocation() + } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt index 9932abae..c5508a4d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt @@ -68,23 +68,42 @@ class FrostVideoView @JvmOverloads constructor( } } else { hideControls() - val scale = Math.min(height / 4f / videoDimensions.y, width / 2.3f / videoDimensions.x) - val desiredHeight = scale * videoDimensions.y - val desiredWidth = scale * videoDimensions.x - val padding = containerContract.lowerVideoPadding - val offsetX = width - MINIMIZED_PADDING - desiredWidth - val offsetY = height - MINIMIZED_PADDING - desiredHeight - val translationX = offsetX / 2 - padding.x - val translationY = offsetY / 2 - padding.y - videoBounds.set(offsetX, offsetY, width.toFloat(), height.toFloat()) - videoBounds.offset(padding.x, padding.y) - animate().scaleXY(scale).translationX(translationX).translationY(translationY).setDuration(ANIMATION_DURATION).withStartAction { + val (scale, tX, tY) = mapBounds() + animate().scaleXY(scale).translationX(tX).translationY(tY).setDuration(ANIMATION_DURATION).withStartAction { backgroundView?.animate()?.alpha(0f)?.setDuration(ANIMATION_DURATION) viewerContract.onFade(0f, ANIMATION_DURATION) } } } + /** + * Store the boundaries of the minimized video, + * and return the necessary transitions to get there + */ + private fun mapBounds(): Triple { + val portrait = height > width + val scale = Math.min(height / (if (portrait) 4f else 2.3f) / videoDimensions.y, width / (if (portrait) 2.3f else 4f) / videoDimensions.x) + val desiredHeight = scale * videoDimensions.y + val desiredWidth = scale * videoDimensions.x + val padding = containerContract.lowerVideoPadding + val offsetX = width - MINIMIZED_PADDING - desiredWidth + val offsetY = height - MINIMIZED_PADDING - desiredHeight + val tX = offsetX / 2 - padding.x + val tY = offsetY / 2 - padding.y + videoBounds.set(offsetX, offsetY, width.toFloat(), height.toFloat()) + videoBounds.offset(padding.x, padding.y) + L.v("Video bounds: fullwidth $width, fullheight $height, scale $scale, tX $tX, tY $tY") + return Triple(scale, tX, tY) + } + + fun updateLocation() { + L.d("Update video location") + val (scale, tX, tY) = if (isExpanded) Triple(1f, 0f, 0f) else mapBounds() + scaleXY = scale + translationX = tX + translationY = tY + } + init { setOnPreparedListener { start() @@ -103,7 +122,6 @@ class FrostVideoView @JvmOverloads constructor( fun jumpToStart() { pause() - videoControls?.hide() v.seekTo(0) videoControls?.finishLoading() } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt index 7ba49d3c..bf4df8fe 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt @@ -9,6 +9,7 @@ import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.view.ViewGroup +import android.view.ViewTreeObserver import android.widget.FrameLayout import android.widget.ImageView import ca.allanwang.kau.utils.* @@ -124,6 +125,15 @@ class FrostVideoViewer @JvmOverloads constructor( restarter.fadeIn() } + fun updateLocation() { + viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { + override fun onGlobalLayout() { + video.updateLocation() + viewTreeObserver.removeOnGlobalLayoutListener(this) + } + }) + } + } interface FrostVideoViewerContract { diff --git a/app/src/main/res/xml/frost_changelog.xml b/app/src/main/res/xml/frost_changelog.xml index 98441a58..65d577e0 100644 --- a/app/src/main/res/xml/frost_changelog.xml +++ b/app/src/main/res/xml/frost_changelog.xml @@ -6,7 +6,13 @@ --> - + + + + + + + @@ -15,7 +21,6 @@ - -- cgit v1.2.3